skip to Main Content
$address = [" Street name"= >" Rozenlaan",  "House number" = > 10,  "Post code"= > 3945, " City"= >" Ham"];
echo $address ["Street name"];

I get syntax error but I can’t see where I made mistake:

(syntax error, unexpected token "=", expecting "]")

I’m a student learning so tyhank for the help.

I created an array, I expected to call the (key "Street name, and get the value Rozenlaan",
instead I get a syntax error, I’m not sure where I went wrong.

2

Answers


  1. You have extra spaces in your definition of array.

    You have:

    $address = [" Street name"= >" Rozenlaan",  "House number" = > 10,  "Post code"= > 3945, " City"= >" Ham"];
    echo $address ["Street name"];
    

    and it should be:

    $address = ["Street name" => "Rozenlaan", 
                "House number" => 10,
                "Post code" => 3945, 
                "City" => "Ham"];
    echo $address ["Street name"];
    

    Reference

    Login or Signup to reply.
  2. It’s the space between the = and the > that is the problem.

    Correct:

    $address = ["Street name" => "Rozenlaan", "House number" => 10, "Post code" => 3945, "City" => "Ham"]; echo $address ["Street name"];
    

    And although it’s not necessary, I would suggest not having spaces in your array keys:

    $address = ["Street_name"=>"Rozenlaan", "House_number"=> 10,"Post_code"=>3945, "City"=>"Ham"]; echo $address ["Street_name"];
    

    You could also use "camel case", like streetName, houseNumber, etc.

    Also, you noticed that I removed stray spaces from within the array values. Try to avoid those, as well, as they can give you grief if you’re not accounting for them by trimming them off.

    Hope this helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search