Home > Software engineering >  Can you give me a hand understanding why my PHP array is throwing errors?
Can you give me a hand understanding why my PHP array is throwing errors?

Time:08-11

My code:

 <?php
 $myArray = ("Emily", "Meg", "Mary", "Bob");
  print_r ($myArray);
?> 

Is throwing the following error:

PHP Syntax Check: Parse error: syntax error, unexpected token "," in your code on line 4$myArray = ("Emily", "Meg", "Mary", "Bob");

(It wasn't working when I uploaded the index.php file on my website so I used https://phpcodechecker.com/ to see what was wrong. )

CodePudding user response:

You should change it to

$myArray=array("Emily", "Meg", "Mary", "Bob");

or

$myArray=["Emily", "Meg", "Mary", "Bob"];

CodePudding user response:

Please use array(....) to assign elements to an array

Change the line

$myArray = ("Emily", "Meg", "Mary", "Bob");

to

$myArray = array("Emily", "Meg", "Mary", "Bob");

CodePudding user response:

this is a syntax error you have to make the variable an array when you want to print multiple values in the array format the syntax would be something like

$variable_name = ("value1", "value2", "value3", "value4");

to display values simply write like this -

print_r($variable_name);

Hope this answer will be helpful for you.

  •  Tags:  
  • php
  • Related