PHP 7 also introduced some other new features with small changes, such as new syntax for array constants, multiple default cases in switch
statement, options array in session_start
, and so on. Let's have a look at these too.
Starting with PHP 5.6, constant arrays can be initialized using the const
keyword, as follows:
const STORES = ['en', 'fr', 'ar'];
Now, starting with PHP 7, constant arrays can be initialized using the define
function, as follows:
define('STORES', ['en', 'fr', 'ar']);
Prior to PHP 7, multiple default cases in a switch statement were allowed. Check out the following example:
switch(true) { default: echo 'I am first one'; break; default: echo 'I am second one'; }
Before PHP 7, the preceding code was allowed, but in PHP 7, this will result in a fatal error similar to the following:
Fatal error: Switch statements may only contain one default clause in…