codegurukool.com · Free coding tutorials & online compiler

Home › PHP Tutorial › PHP arrays

PHP arrays

Indexed and associative arrays.

An indexed array uses numbers as keys; an associative array uses names. [] appends, count counts, sort orders and implode joins items into a string.

Example code

<?php
$f = ["pear", "apple"];
$f[] = "plum";
echo count($f) . PHP_EOL;
sort($f);
echo implode(", ", $f) . PHP_EOL;
$p = ["name" => "Ada", "age" => 36];
$p["city"] = "London";
foreach ($p as $k => $v) {
  echo "$k: $v" . PHP_EOL;
}
?>

Run this code in the online editor

← PHP functionsPHP superglobals and form handling →