Jump to content

Calling Function Variable


Gniew

Recommended Posts

  function Get($col, $row) {
  $x = ($_GET['x'] + $col);
  @$worked = mysql_query("SELECT * FROM `map` WHERE `x`=('".$_GET['x']."' + $col) AND `y`=('".$_GET['y']."' + $row)");
  @$result = mysql_fetch_object($worked); if($result->x != ""){Map::GetWork($result->Number);} else {Map::GetFail();}}

 

One of several functions inside class "Map". Now,  "$x = ($_GET['x'] + $col);" is new code, can't figure out if it's proper, since I can't call the variable $x outside of the function. How do I go about doing that? So that somewhere down the line I can go <?=$x?> and get the value of $x to appear?

Link to comment
Share on other sites

ideally.. you'd do something like

 

  function Get($col, $row, &$x = FALSE) {
  $x = ($_GET['x'] + $col);
  @$worked = mysql_query("SELECT * FROM `map` WHERE `x`=('".$_GET['x']."' + $col) AND `y`=('".$_GET['y']."' + $row)");
  @$result = mysql_fetch_object($worked); if($result->x != ""){Map::GetWork($result->Number);} else {Map::GetFail();}}

 

Then if you wanted access to whatever $x was you'd call

 

 Get( $colVar, $rowVar, $reference ); 

 

and $reference would hold whatever value $x does. If you don't want to use it, call it without the 3rd argument.

Link to comment
Share on other sites

Okay, now, I'd like to understand functions more, to help me in the future, so...with

function Get($col, $row, &$x = FALSE) {

What does the "&$x = FALSE" mean?

 

and how would I use the $reference variable then? You said to call it without the 3rd argument?

 

And, with regards to the second option, how would I have the function return $x?

Link to comment
Share on other sites

Okay, now, I'd like to understand functions more, to help me in the future, so...with

function Get($col, $row, &$x = FALSE) {

What does the "&$x = FALSE" mean?

& tells the function that it is going to refference a variable, in this case $x, that has been initialised somewhere in the main body of the code, ie. Not within a function. Using the = FALSE then skips looking up the variable and assignes it a boolian value of FALSE.  This is just a method of making the function treat the $x variable as an external entity, so that it can then be accessed outside the function.

 

Thorpe's method is cleaner, in that you would process your function as before, and then include a statement that will return $x from the function into the main body of the code, as follws:

  function Get($col, $row) {
  $x = ($_GET['x'] + $col);
  @$worked = mysql_query("SELECT * FROM `map` WHERE `x`=('".$_GET['x']."' + $col) AND `y`=('".$_GET['y']."' + $row)");
  @$result = mysql_fetch_object($worked); if($result->x != ""){Map::GetWork($result->Number);} else {Map::GetFail();}
return ($x);
}

 

Hope that makes sense - and is accurate (not too sure about the refferenceing stuff).  You should spend time looking into functions in deapth so that you can get a proper understanding of how they operate.

Link to comment
Share on other sites

Okay, now, I'd like to understand functions more, to help me in the future, so...with

function Get($col, $row, &$x = FALSE) {

What does the "&$x = FALSE" mean?

 

Normally parameters are passed to functions by value. This way if the value of the parameter is changed within the function it has no effect on the variable outside of the function.

 

The alternative is that you pass it by reference. Not sure how php works internally, but in older languages this pretty much meant that the function received the address in memory of the variable that was being passed, so changed the actual original variable. This is what you want to do in this case.

 

The "= FALSE" is just passing a default value for that parameter. So you could call the function with only the first 2 parameters and the function would just make up the 3rd and give it the default value. You could also call the function with all 3 parameters, passing the value of the 3rd one instead of using the default value.

 

All the best

 

Keith

Link to comment
Share on other sites

An even better option would be to simply have your function return $x.

 

Ideally, that's the solution. But if he also wants the function to return, say a boolean value if things worked or not, a reference is a great way to access that variable.

 

I'm just glad no one has said anything about globals :D

Link to comment
Share on other sites

  function Get($col, $row) {
  $x = ($_GET['x'] + $col);
  @$worked = mysql_query("SELECT * FROM `map` WHERE `x`=('".$_GET['x']."' + $col) AND `y`=('".$_GET['y']."' + $row)");
  @$result = mysql_fetch_object($worked); if($result->x != ""){Map::GetWork($result->Number);} else {Map::GetFail();}
return ($x);
}

So, how do I call $x from inside the code? Trying <?$x;?> and <? Map::ImageResult($x);?> aren't working...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.