ballhogjoni Posted April 11, 2008 Share Posted April 11, 2008 I am trying to understand functions and oop better. What is the order of the variables in functions and classes? I have looked around php.net, and google and there is no clear cut explanation. Lets say I have a function that looks like this: test($test1,$test2) { //do this code echo "$test1.'<br>'$test2"; } where would the values of these variables come from? When you pass the values to the function do the variable's values get put into the test($test1,$test2) part first or do they get put into the echo "$test1.'<br>'$test2"; part first? Link to comment https://forums.phpfreaks.com/topic/100699-functions-variables-order/ Share on other sites More sharing options...
kenrbnsn Posted April 11, 2008 Share Posted April 11, 2008 Order matters. Taking your example: <?php function test($test1,$test2) { echo $test1 . ' --- ' . $test2; }?> and you do: <?php $test1 = 1; $test2 = 'two'; test($test1,$test2); echo '<br>'; test($test2,$test1); ?> The results will be: 1 --- two two --- 1 Ken Link to comment https://forums.phpfreaks.com/topic/100699-functions-variables-order/#findComment-515014 Share on other sites More sharing options...
p2grace Posted April 11, 2008 Share Posted April 11, 2008 Parameters and arguments. <?php function showSum($num1,$num2){ $sum = $num1 + $num2; return $sum; } echo "sum = ".showSum(4,6); ?> Link to comment https://forums.phpfreaks.com/topic/100699-functions-variables-order/#findComment-515018 Share on other sites More sharing options...
ballhogjoni Posted April 11, 2008 Author Share Posted April 11, 2008 Parameters and arguments. <?php function showSum($num1,$num2){ $sum = $num1 + $num2; return $sum; } echo "sum = ".showSum(4,6); ?> I understand that this is a dumbed down function but from this example wouldn't it be jsut as easy to do this: <?php echo "sum = ".(4+6); ?> or will this work?: <?php echo "sum = ".showSum($_POST['num1fromform'],$_POST['num2fromform']); //getting num1fromform and num2fromform from a form on the page ?> Link to comment https://forums.phpfreaks.com/topic/100699-functions-variables-order/#findComment-515099 Share on other sites More sharing options...
p2grace Posted April 11, 2008 Share Posted April 11, 2008 Your second option should work. Link to comment https://forums.phpfreaks.com/topic/100699-functions-variables-order/#findComment-515121 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.