Syntax

PHP Create an Array Syntax

In PHP, create an array: creates an indexed array — $nums = [1, 2, 3]; $nums[] = 4;

Quick Answer

Syntax
$nums = [1, 2, 3]; $nums[] = 4;
PHPSyntax

Create an Array

Code example
$nums = [1, 2, 3]; $nums[] = 4;

Creates an indexed array.

Learn More

How to create an array in PHP

  1. Open your PHP file or editor.
  2. Copy the example, then replace its sample names and values for your code.

PHP Create an Array Syntax

Use $nums = [1, 2, 3]; $nums[] = 4;.

Related Syntax References

Frequently asked questions

What is the PHP syntax for create an array?

Use this pattern: $nums = [1, 2, 3]; $nums[] = 4;

Can I copy this PHP code example?

Yes. Use Copy code and adapt the example to your program.

Is this PHP syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify