Codectionary / Developer documentation / PHP

PHP Arrays

PHP arrays are ordered maps: they can use integer indexes or string keys. Array literals use square brackets.

Syntax

$tags = ["php", "web"];
$user = ["name" => "Ada"];

Examples

Iterating key-value data

A small, runnable example of this syntax.

<?php
$user = ["name" => "Ada", "active" => true];
foreach ($user as $key => $value) {
  echo "$key: $value\n";
}
?>

Best practices

  • Use associative keys consistently and check that expected external keys exist before reading them.
  • Keep examples small while learning, then combine the idea with a real project.

At a glance

Purpose
Server-side web applications
File extension
.php
Runs in
PHP runtime on a server or CLI
Usually used with
HTML, databases and web servers

Specifications & further reading

Related PHP documentation