phparraysmultidimensional-arrayassociative-arrayerror-reporting

How can I declare/define an associative array properly in PHP?


I wrote a program to define an associative array titled $aFilter and tried to print it, but I'm not able to. I tried two ways to achieve this, but I couldn't succeed. Following are the two ways I tried.

Way 1:

<!DOCTYPE html>
<html>
<body>

<?php
$aFilter = Array
        (
            ['pages'] => 1,
            ['photo'] => 1,
            ['link'] => 1,
            ['event'] => 1,
            ['friend'] => 1,
            ['user_status'] => 1,
            ['poll'] => 1,
            ['quiz'] => 1,
            ['market'] => 1,
            ['apps'] => 1
        )
        print_r($aFilter);
?>

</body>
</html>

Way 2:

<!DOCTYPE html>
<html>
<body>

<?php
$aFilter = Array
        (
            ['pages'] => 1
            ['photo'] => 1
            ['link'] => 1
            ['event'] => 1
            ['friend'] => 1
            ['user_status'] => 1
            ['poll'] => 1
            ['quiz'] => 1
            ['market'] => 1
            ['apps'] => 1
        )
        print_r($aFilter);
?>

</body>
</html>

After executing both of the above pieces code, I'm getting a blank white screen. No any error or warning. Why does it happen? How can I get errors and warnings displayed on my webpage without making any change to php.ini file settings?

What is the mistake I'm making and how can I fix it?


Solution

  • print_r() displays information about a variable in a way that's readable by humans. It is not the code you need to write. Here is how arrays are defined in PHP:

    $aFilter = [
        'pages' => 1,
        'photo' => 1,
        'link' => 1,
        'event' => 1,
        'friend' => 1,
        'user_status' => 1,
        'poll' => 1,
        'quiz' => 1,
        'market' => 1,
        'apps' => 1
    ];
    print_r($aFilter);
    

    And your white screen is because of syntax errors in the code and you don't have display_errors configuration option set to on, so PHP didn't display the error.