Search This Blog



Language Constructions

PHP language, like any programming language, is composed by language constructions. Beyond the basic programming structures (conditional, loop, classes syntax, functions syntax, etc), the language has some built-in functions. They are functions compiled in the core language.

Many programmers do not know how to distinguish some of these language constructions from conventional functions (from a library or PHP module or created by user). The PHP language constructions can be seen at tokens list. A token is a character sequence that features a basic unit of source-code in relation to a language syntax.

The language constructions often have high performance. Look at some examples of language constructions:

  • isset, unset, empty
  • exit
  • return
  • eval
  • include, include_once, require, require_once
  • declare
  • echo, print

Language constructions do not work with the PHP feature of "variable function", where a function is called by its name kept in a variable, as in the example:

// Keep the function name in a variable
$function = 'strlen';

// Calling the function

Syntax Error

Many of errors of PHP programmers often see are syntax errors. Sometimes they occurs by language unknown, by typing, or by lack of atention. The PHP interpreter often expose a syntax error using the language tokens, as the example below:

$x = 1
$a = 2;

Note that, purposely, I wrote a code with a syntax error, that is shown as follow:

PHP Parse error: syntax error, unexpected T_VARIABLE in ... on line 3

It happens because after the "1" (or before "$a"), the PHP interpreter was expecting some operator (add, sub, concat, etc) or a semicolon to indicate the instruction end. But, instead, it found the variable "$a". A variable is a PHP token whose code is T_VARIABLE (look at tokens list). So, the syntax error happens. Note that the correction must be done at line 2, and not at line 3 where the wrong token was found. It often happens with strings that was opened but the end was missing. The parser identifies the problem when the next string was opened with the same quote, and this line is often other than the missing quote.

It is fundamental to know the tokens of a language to get domain of the language (and do not allow the language domains you). The assurance a programmer has about a language came from many places, but, probably, the most fundamental is the domain of the tokens.

Reading Tokens

To understand well the tokens, you do not need to implement a PHP parser. The PHP has a module for that called "Tokenizer". This module has two functions:

  • token_get_all: to explode a string that keep a PHP code into a tokens array.
  • token_name: to obtain the token name by its numeric code.

With this module, you can create a script that receives other script as parameter and shows the code with syntax highlight. For exemple, to get all "FOREACH" in one color and "IF" in other.


The language construnction isset can be used to improve the performance of a PHP code that needs to verify if an array has, at least, N elements.

Instead of using count, that returns the number of elements, like that:

if (count($array) >= 10) {

you can use isset to verify if the position 9 exists:

if (isset($array[9])) {

It is valid for arrays that has numeric indexation starting by zero and without null positions.

1 comment:

  1. awesome blog.i appreciate this blog.nice written about php.Thanks for this blog.