typestar

Arrays in PHP

Building, growing, slicing, and sorting PHP's list-shaped arrays.

<?php
$langs = ["php", "ruby", "go"];
$langs[] = "rust";

echo $langs[0], " through ", $langs[count($langs) - 1], PHP_EOL;
echo count($langs), " languages", PHP_EOL;

// array_slice copies; sort reorders the array in place
print_r(array_slice($langs, 1, 2));
sort($langs);
echo implode(", ", $langs), PHP_EOL;

How it works

  1. $langs[] = appends without naming an index; count sizes the array.
  2. array_slice copies a range and leaves the original untouched.
  3. sort reorders in place; implode joins the result into one string.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
309
Tokens
80
Three-star pace
80 tpm

At the three-star pace of 80 tokens a minute, this run takes about 60 seconds.

Type this snippet

Step 1 of 3 in Arrays, step 7 of 27 in Language basics.

← Previous Next →

Arrays in other languages