How do you declare an array in PHP?

Prepare for the Zend Certified PHP Engineer Exam with our comprehensive test, featuring flashcards and multiple choice questions. Each question comes with detailed hints and explanations. Ensure you're ready for your exam!

In PHP, declaring an array can be accomplished using the array() function or the shorthand syntax, which is the square brackets []. This syntax has been available since PHP 5.4 and offers a more concise way to create arrays.

Using the array() function looks like this:

$myArray = array("value1", "value2", "value3");

With the shorthand syntax, it can be written as:

$myArray = ["value1", "value2", "value3"];

Both methods effectively create an array with the specified values. The shorthand syntax is now preferred for its brevity and clarity.

The other options do not represent valid array declaration techniques in PHP. The create_array() function is not a built-in PHP function, the list() function is used for assigning values from an array to variables rather than creating an array, and new Array() syntax is not valid in PHP for declaring arrays; instead, it derives from JavaScript, which can lead to confusion. Thus, utilizing either the array() function or the shorthand syntax is the correct approach to declare an array in PHP.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy