Link to home
Start Free TrialLog in
Avatar of LB1234
LB1234

asked on

Why doesn't this code work?

Don't understand why this isn't working.  When run, I get nothing in the browser.


<?php


function add ($x, $y) {
	$num = $x + $y;
	return $num;
}

add(3,4);

echo $num;


?>

Open in new window

Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

$num is only available within your function. You need something like this:


$answer = add(3,4);
echo $answer;

Open in new window

If you don't need to assign it, then:

echo add(3,4);

Open in new window

Avatar of LB1234
LB1234

ASKER

I thought the whole point of "return" was to make sure something could be used OUTSIDE the function?
Avatar of LB1234

ASKER

If I return "$num" shouldn't $num be available outside the variable?
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial