php version guide

PHP 7.1 Changes

PHP 7.1 expanded the type system and made several everyday declarations clearer. Nullable types, void, iterable, multi-catch handling, and class constant visibility are the changes most likely to appear in application code.

Changes Worth Recognising

  • Use ?Type when a value may deliberately be null.
  • Use void when a function performs work but does not return a value.
  • iterable accepts arrays and Traversable objects.
  • A multi-catch handles related exception types without duplicating the body.

Representative Code

PHP example
<?php

function printTags(iterable $tags): void
{
    foreach ($tags as $tag) {
        echo $tag . PHP_EOL;
    }
}

printTags(['php', 'backend']);

// Prints:
// php
// backend

Upgrade Review

  • Look for functions documented as nullable but typed as non-nullable.
  • Check callers before adding a void return declaration.
  • Use multi-catch only where the recovery action really is the same.

These declarations improve maintenance because they make contracts readable without opening every function body.

Practice

Tighten a Collection Contract

Write a function that accepts an iterable of product names and prints each name. Call it once with an array and once with a generator.

Show solution

Declare the parameter as iterable, loop with foreach, and use a generator function containing yield. Both callers satisfy the same contract.