Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

PHP
PHP
0/12 completed
Beginner
Hello PHPVariables and arraysControl flowFunctions
Intermediate
Strings, dates and JSONForms, requests and sessionsDatabases with PDO
Advanced
Object-oriented PHPErrors, exceptions and typesSecurity essentials
Pro
Composer, autoloading and testingLaravel basics
Tracks / PHP / Beginner

Variables and arrays

PHP values have types (integers, strings, booleans…) even though you don't declare them. And PHP's array is unusually versatile: the same type is a list, a dictionary, a stack and a queue. Most PHP code is about moving data in and out of arrays, so this lesson spends most of its time there.

Types

PHP · editable
  • intdiv(7, 2) is whole-number division; 7 / 2 gives a float 3.5.
  • PHP integers are 64-bit; going past PHP_INT_MAX turns them into floats.

Type juggling (and how to avoid surprises)

PHP converts types automatically in many places. Know the rules, and use strict comparison ===:

PHP · editable

💡 Always use === and !== unless you specifically want loose comparison. And note (bool) "0" is false but (bool) "false" is true!

Falsy values in PHP: false, 0, 0.0, "", "0", [] and null. Everything else is truthy.

Constants

PHP · editable

Strings, briefly

PHP · editable

strlen counts bytes; for text with accents or other scripts use the mb_ versions (mb_strlen, mb_substr). More in Strings, dates and JSON.

Arrays as lists

PHP · editable

print_r shows arrays readably; var_dump also shows types.

Arrays as dictionaries (associative arrays)

PHP · editable

?? (the null coalescing operator) returns the right side when the left is missing or null. It's the safe way to read keys that might not exist.

Destructuring

PHP · editable

Transforming arrays

PHP · editable
Function Does
array_map(fn, $a) Transform every value
array_filter($a, fn) Keep values where fn returns true (keys are kept)
array_reduce($a, fn, $start) Combine into one value
sort / rsort Sort values, re-number keys
asort / arsort Sort by value, keep keys
ksort / krsort Sort by key
usort($a, fn) Sort with your own comparison (<=> is the "spaceship" operator)
array_column($rows, 'name') Pull one field out of a list of rows

⚠️ The sort functions change the array in place and return true, so write sort($a);, not $a = sort($a);.

fn($p) => … is an arrow function: a short anonymous function that can use variables from the surrounding code. More in Functions.

Practice

  1. Create an array of five numbers and print the largest, the smallest and the average (max, min, array_sum, count).
  2. Given ["apple" => 3, "banana" => 0, "cherry" => 7], print only the fruits that are in stock, sorted by quantity.
  3. Count how often each word appears in "the cat and the hat" with explode and array_count_values.
  4. Sort a list of people by name with usort.
✎ Check your understanding

1.What does "5" === 5 return?

2.How do you safely read $data['email'] when the key might not exist?

3.What does sort($numbers) return?

4.Which function keeps keys while sorting by value?

← Hello PHP