reformed reformed - 1 month ago 6
PHP Question

Is constant name DEFAULT not allowed in PHP?

I'm getting an error when I try to define a constant named

DEFAULT
. Why is this?

<?php
define('DEFAULT', true);
var_dump(DEFAULT);
// or
class Test {
const DEFAULT = true;
}
var_dump(Test::DEFAULT);


This results in the following error:


Parse error: syntax error, unexpected 'DEFAULT' (T_DEFAULT) in ... on line X

Answer

It's not allowed, because default is a reserved word. It is used in switch blocks.

switch( $x )
{
    case 1: echo $x; break;
    case 2: echo $x; break;
    case 3: echo $x; break;
    default: echo $x; break;
}

Here is a list of the reserved words in PHP

Comments