Page 1 of 1

php function

Posted: Wed Mar 10, 2004 11:53 am
by after_shox
ok this may seem stupid but i just spent about an hour trying to get php to pass a variable to a function. Can someone tell me why this doesnt work ?

<?php

$num ="10";


function square ($num)
{
return $num * $num;
}
echo "$num";

?>

Posted: Wed Mar 10, 2004 1:10 pm
by Weirdan
you have to call the function:

Code: Select all

$num ="10"; 
function square ($num) 
{ 
  return $num * $num; 
} 
echo square($num);

Posted: Wed Mar 10, 2004 1:39 pm
by after_shox
Thanks but how to i get the value back so i can use it in any statement as a variable ? The "Gobal" option ?

Posted: Wed Mar 10, 2004 2:02 pm
by TheBentinel.com
after_shox wrote:how to i get the value back
Assign it to a variable, or just use the function call wherever you'd use a variable:

Code: Select all

$num ="10"; 
function square ($num) 
{ 
  return $num * $num; 
} 
$returned_value =  square($num);

echo $returned_value;

if (square(5) == 25)
{
  echo "yep";
}
Hope it helps!

Posted: Wed Mar 10, 2004 2:36 pm
by after_shox
Yeah think i got it. Thanks for your help. :)