Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Syntax

Syntax refers to the set of rules that defines the structure of valid PHP code.

For example, there is the object syntax, around the -> and :: operators, and the array syntax, around [index] operator.

Syntax must be lintable for a PHP script to be executable. Otherwise, it creates a syntax error. Yet, a lintable syntax does not always mean the code is executable: it may yield an execution error. It is not always obvious which syntax is not executable.

<?php

    A = B C; // syntax error, 'C' is not valid. 
    
    clone 1; // not a syntax error
    
    $array = ['a' => 1, 'b' => 2, 'c' => 3];
    echo $array['a']; // 1
    
    $object = (object) $array;
    echo $object->a; // 1
    echo $object['a']; // no syntax error, error at execution
    
    //valid syntax
    function trumpet($trunk) {
        print $trunk;
    }

    //valid call
    trumpet(3);

    //invalid call
    trumpet([]);

?>

Documentation

See Also