Jump to content

Understanding a function($var);


JREAM

Recommended Posts

Hey I have always had trouble with this but I think I might be getting it, maybe you could clear me up :)

 

function abc ($foo) {

 

echo "5 + " . $foo  .";

return $foo;

 

}

 

so If I say

$num = 4;

echo abc($num);

 

will that be 9, because inside the function(backets) its just replacing a specific variable inside the function?

 

 

Then would it be the same for like:

function abc($foo, $foo1, $foo2) {

 

echo "5 + " . $foo  ."+". $foo2 ."+". $foo3 .";

return $foo, $foo2, $foo3;

 

}

 

$num = 4;

$num2 = 22;

$num3 = 244;

echo abc($num, $num2, $num3);

 

 

Does it place $num2 for where $foo2 is? Because it's in the 2nd placeholder (or 1st if its like an array)?

 

Link to comment
Share on other sites

The part in question ($var) is what is called a function parameter and it is allowing you to pass a value into that function so it can be processed with a unique value added to it

 

some functions do not require a parameter such as phpinfo(); it can have a parameter added or not.

 

The parameter is what makes a function really a functional because without them a function would be limited to 1 thing with the parameters the limits of the function are infinite really.

Link to comment
Share on other sites

The value you pass to the function argument replaces that variable inside the function scope. The defined function argument variable name does not need to be the same as the one you pass to it.

 

<?php
function abc($var) {
    return $var+5;
}

$num = 7;
$var = 9;
echo abc(4); //returns 9
echo abc($num); //returns 12
echo abc($var); //returns 14
?>

 

They all work.

 

Now, for multiple arguments, they get replaced in order and by name.

 

<?php
function abc($var1, $var2, $var3) {
   return 5 + $var1 + $var2 + $var3;
}

$num1 = 2
$num2 = 3
$num3 = 4

$var5 = 1
$goo = 4
$puppy = 7

echo abc(1,2,3); //inside the function, $var1=1, $var2=2, $var3=3, so, 5+1+2+3 = 11
echo abc($num1, $num2, $num3); // now $var1=$num1, $var2=$num2, $var3=$num3, so 5+2+3+4 = 14
echo abc($num2, $num3, $num1); // now $var1=$num2, $var2=$num3, $var3=$num1, so 5+3+4+2 = 14
echo abc($puppy, $num2, $goo); // now $var1=$puppy, $var2=$num2, $var3=$goo, so 5+7+3+4 = 19
echo abc($var5, $var5, $var5); // now $var1, $var2 and $var3 all are equal to $var5, so 5+1+1+1 = 8
?>

 

Notice a few things:

For numeric operations there are no double quotes. You are using string operations in your functions... you would print "5+2+3+4" instead of the total result.

Also, you are echoing the values twice, once inside the function and once outside when you call it.

And finally, you can't return multiple values, as in: return $var1,$var2,$var3. To do something like that you have to set the variables inside an array or list and then return that array or list.

 

Cheers.

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.