Simple Custom Function Fail

I wanted to experiment with the differences between echo, print, and return. I managed to get one custom function to work, but not this one. I’m honestly not sure why. It tells me I have an undefined function val2ue on line 8, which is where I try to echo it. It looks defined to me.
[php]<?php
function add2($a,$b)
{
$val2ue=$a+$b;
return “$val2ue”;
};
add2(7,6);
echo “$val2ue”;
?>[/php]

When a function returns something, that means that you need to put that value into something when you run the function. In your code, either:[php]echo add2(5, 7);[/php]

Or assign it to a variable:[php]$var = add2(5, 7);
echo $var;[/php]

Hope this helps.

do u know that your function is returning a string ?

use this way
[php]
function add2($a,$b)
{
$newval = $a + $b;
return $newval;
}
[/php]
and you can use it this way
[php]
echo add2(3,20);
[/php]
or
[php]
$calculated = add(100,140);
echo $calculated;
[/php]

<?php global $var2ue; function add2($a,$b) { $val2ue=$a+$b; return "$val2ue"; }; add2(7,6); echo $val2ue; ?>

Hope this won’t give you error and the output you desire

Sponsor our Newsletter | Privacy Policy | Terms of Service