Jump to content

functions variables order


ballhogjoni

Recommended Posts

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

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

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 ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.