Search code examples
phparraysfunctionkeywordbuilt-in

How does the variable name $array work out in PHP though it's a keyword and built-in function?


I was reading PHP manual and I come across following code :

<?php
$array = array(
    "foo" => "bar",
    42    => 24,
    "multi" => array(
         "dimensional" => array(
             "array" => "foo"
         )
    )
);

var_dump($array["foo"]);
var_dump($array[42]);
var_dump($array["multi"]["dimensional"]["array"]);
?>

You can see in the above example that the name of array variable is also $array

As per my knowledge, keywords and buit-in function names should never be used as variable names in PHP. Actually, it shouldn't have work but it's working fine.

How this is possible?

Like array can I use other keywords and built-in function names, built-in class-names as variable name in my code?

Is my understanding of concept wrong that keywords and built-in function names can not be used as variables names as they will not work and give error?

Please clear my these doubts.

Thanks.


Solution

  • $ sign makes it a variable and not as a type of array or any keyword. In other languages such as c++ / python you cant do that or you may overload the built-ins.