1

I have the following code:

<html>
<body>
    <h1><?isFoo();?></h1>
</body>
</html>
<?php
$foo_app_cookie_val = "foo";

function isFoo() {
    global $foo_app_cookie_val;
    echo "in isFoo: '$foo_app_cookie_val'<br/>";
    return isApp($foo_app_cookie_val);
}
?>

The output I'm getting is:

isFoo: ''

Why aren't I seeing the actual value of $foo_app_cookie_val?

ThaDon
  • 7,826
  • 9
  • 52
  • 84

2 Answers2

10

$foo_app_cookie_val is being set to foo after <h1><?isFoo();?></h1> is output. Change the script to:

<?php
$foo_app_cookie_val = "foo";

function isFoo() {
    global $foo_app_cookie_val;
    echo "in isFoo: '$foo_app_cookie_val'<br/>";
    return isApp($foo_app_cookie_val);
}
?>
<html>
<body>
    <h1><?isFoo();?></h1>
</body>
</html>
Michael
  • 11,912
  • 6
  • 49
  • 64
  • `**facepalm**` I'm too used to languages that don't take declaration order into consideration :( – ThaDon Jan 16 '14 at 17:19
4

isFoo() is being called before you've set the value of $foo_app_cookie_val. Try moving the large block of PHP code to the top of the file, like this:

<?php
$foo_app_cookie_val = "foo";

function isFoo() {
    global $foo_app_cookie_val;
    echo "in isFoo: '$foo_app_cookie_val'<br/>";
    return isApp($foo_app_cookie_val);
}
?>
<html>
<body>
    <h1><?isFoo();?></h1>
</body>
</html>

Where the function is actually declared doesn't matter. What matters is where you call the function in relation to setting the global variable.

Peter Bloomfield
  • 5,578
  • 26
  • 37