Zend Certified PHP Engineer (ZCPE) Practice Test

1 / 400

How do you declare an array in PHP?

By using the create_array() function

By using the array() function or shorthand syntax []

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:

```php

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

```

With the shorthand syntax, it can be written as:

```php

$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.

Get further explanation with Examzify DeepDiveBeta

By using the list() function

By using the new Array() syntax

Next Question
Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy