0
votes

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.

1
string content doesn't count in the same way.Scuzzy
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. where did you get this statement?rai
@Scuzzy : Thanks for sharing the link. But even after the manual says that "Using them as variable names is generally OK, but could lead to confusion", then why they are making the same practice in the examples on array page?PHPFan
for the word array: array() is a keyword, $array and 'array' are not "These words have special meaning in PHP. Some of them represent things which look like functions, some look like constants, and so on - but they're not, really: they are language constructs."Scuzzy

1 Answers

2
votes

$ 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.