Mark Segal
Mark Segal

Reputation: 5550

Arrays with PHP wont evaluate

I have a config.php file which has some constants and methods.
I have a test.php file which calls one of the methods in config.php.
The relevant code is:

$Questions = array(
    1 => "Is he/she nice?1",
    2 => "Is he/she sweet?2",
    3 => "Is he/she nice?3",
    4 => "Is he/she sweet?4",
    5 => "Is he/she nice?5",
    6 => "Is he/she sweet?6",
    7 => "Is he/she nice?7",
    8 => "Is he/she sweet?8",
    9 => "Is he/she nice?9",
    10 => "Is he/she sweet?10"
);


function PrintAnswersOnMe($uid)
{
    $uid = antiSQLi($uid);
    $query = "SELECT * FROM AnsAns WHERE fid='".$uid."'";
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result))
    {
        $rrr = $row[2];
        echo $Questions[1];
        echo $rrr . ' ' . $Questions[$rrr];
        echo "You'r friend <img src='http://graph.facebook.com/".$row['uid']."/picture/' /> answered " . (($row['answer'] == 1) ? "yes" : "no") . " about wether you're ". $rrr.": " . $Questions[$rrr];
        echo "<br /> " . $Questions['2'] . "<br/>";
    }
}


The test file only calls PrintAnswersOnMe. (it includes it)
Everything works file, excpet that no $Question[...] evaluates to actual HTML output!
To check that, I've added $Questions[2] - and also $Questions['2'] - and none of them produce HTML output. The loop does execute because everything else is getting to the HTML.
The funny thing is that inside test.php it does works - echo $Questions[...] is actually products to HTML output. Does anyone have any idea about this mysterious behavior?

Upvotes: 1

Views: 78

Answers (2)

Paul Dessert
Paul Dessert

Reputation: 6389

function PrintAnswersOnMe($uid, $questions) {
//Code goes here
}

Then you'll have access

Upvotes: 2

dririan
dririan

Reputation: 305

You need to add global $Questions; to the beginning of your function, before $uid = antiSQLi($uid);.

Upvotes: 1

Related Questions