Skip to content

Instantly share code, notes, and snippets.

@qwhex
Created June 7, 2023 08:38
Show Gist options
  • Save qwhex/f19241efda88d21ba9c20f205aef27b4 to your computer and use it in GitHub Desktop.
Save qwhex/f19241efda88d21ba9c20f205aef27b4 to your computer and use it in GitHub Desktop.

PHP 7.0

  • Scalar type declarations: Allows functions to require certain types of arguments.
    function sum(int $a, int $b) {
        return $a + $b;
    }
  • Return type declarations: Allows functions to declare the type of the return value.
    function sum($a, $b): int {
        return $a + $b;
    }
  • Null coalescing operator (??): Returns its first operand if it exists and is not NULL; otherwise it returns its second operand.
    $username = $_GET['user'] ?? 'nobody';
  • Spaceship operator (<=>): A three-way comparison operator. It returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b.
    echo 1 <=> 2;  // outputs -1
  • Constant arrays using define(): Defines an array as a constant.
    define('ANIMALS', ['dog', 'cat', 'bird']);

PHP 7.1

  • Nullable types: Allows a type to be either a specified type or NULL.
    function test(?string $name) {
        var_dump($name);
    }
    test(null);  // outputs NULL
  • Void functions: Specifies that a function should not return a value.
    function test(): void {
        echo 'Hello, World!';
    }

PHP 7.2

  • Object type: Allows for functions to accept any object as a parameter, or to return an object.
    function test(object $obj): object {
        return new SplQueue();
    }

PHP 7.3

  • Flexible Heredoc and Nowdoc Syntaxes: Allows for indenting of closing markers.
    echo <<<END
        A indented heredoc syntax example.
    END;

PHP 7.4

  • Typed Properties: Declares the type of a class property.
    class User {
        public int $id;
        public string $name;
    }
  • Arrow Functions: Allows for cleaner one-liner functions with implicit variable scope.
    $factor = 10;
    $nums = array_map(fn($n) => $n * $factor, [1, 2, 3, 4]);
  • Null coalescing assignment operator (??=): It's a shorthand for the common case of needing to use a ternary in conjunction with isset().
    $data['date'] ??= '2023-01-01';

PHP 8.0

  • Named arguments: Allows passing arguments to a function based on the parameter name, instead of the parameter position.
    htmlspecialchars($string, double_encode: false);
  • Constructor Property Promotion: Combines class properties and constructor parameters.
    class Point {
        public function __construct(
            public float $x = 0.0,
            public float $y = 0.0,
            public float $z = 0.0,
        ) {}
    }
  • Match expression: A switch-like expression that can return values.
    echo match (1) {
        0 => 'zero',
        1 => 'one',
        default => 'more',
    };
  • Nullsafe Operator (?->): Allows for the chaining of method calls and property accesses even if one of the elements in the chain returns null.
    $country = $session?->user?->getAddress()?->country;
  • Attributes (Annotations): Provides a way to add metadata to classes, methods, properties, parameters, and constants.
    #[ExampleAttribute]
    public function hello() {}
  • Union types: Allows a function to require arguments or return values of multiple types.
    class Number {
        private int|float $number;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment