php function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
after_shox
Forum Newbie
Posts: 13
Joined: Wed Mar 10, 2004 11:53 am

php function

Post 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";

?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

you have to call the function:

Code: Select all

$num ="10"; 
function square ($num) 
{ 
  return $num * $num; 
} 
echo square($num);
after_shox
Forum Newbie
Posts: 13
Joined: Wed Mar 10, 2004 11:53 am

Post 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 ?
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post 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!
after_shox
Forum Newbie
Posts: 13
Joined: Wed Mar 10, 2004 11:53 am

Post by after_shox »

Yeah think i got it. Thanks for your help. :)
Post Reply