Gniew Posted August 3, 2011 Share Posted August 3, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/243671-calling-function-variable/ Share on other sites More sharing options...
xyph Posted August 3, 2011 Share Posted August 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243671-calling-function-variable/#findComment-1251086 Share on other sites More sharing options...
trq Posted August 3, 2011 Share Posted August 3, 2011 An even better option would be to simply have your function return $x. Quote Link to comment https://forums.phpfreaks.com/topic/243671-calling-function-variable/#findComment-1251097 Share on other sites More sharing options...
Gniew Posted August 3, 2011 Author Share Posted August 3, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/243671-calling-function-variable/#findComment-1251263 Share on other sites More sharing options...
Muddy_Funster Posted August 3, 2011 Share Posted August 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243671-calling-function-variable/#findComment-1251279 Share on other sites More sharing options...
kickstart Posted August 3, 2011 Share Posted August 3, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/243671-calling-function-variable/#findComment-1251301 Share on other sites More sharing options...
xyph Posted August 3, 2011 Share Posted August 3, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/243671-calling-function-variable/#findComment-1251410 Share on other sites More sharing options...
Gniew Posted August 3, 2011 Author Share Posted August 3, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/243671-calling-function-variable/#findComment-1251526 Share on other sites More sharing options...
trq Posted August 4, 2011 Share Posted August 4, 2011 $x = Get(); Quote Link to comment https://forums.phpfreaks.com/topic/243671-calling-function-variable/#findComment-1251566 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.