why do php variables need to be identified by $? [duplicate]
Possible Duplicate:
Why does PHP have a $ sign in front of variables?
In languages like bash and Perl, strings need not be quoted and that is why variable access开发者_开发问答 needs to be identified by using $. Why does PHP need the similar mechanism?
It's a historical decision, probably because it allows to include variables in a string literal:
$variable = "handle to data storage";
echo "a $variable";
Because PHP is influenced by Perl. Back then, when it was conceived, PHP was just a set of Perl scripts.
PHP Constants are a seperate type, but behave much like variables (except they can't be changed, of course... that's what they're constants for) and look a lot like 'em too. For readability, it's nicer to have an identifier. (<- random guess, not)
Besides:
$lol = abcdef;
$lol === 'abcdef'; // true
Undefined constants will throw a notice and will be interpreted as a string.
ohyes, and inside strings, variables can also be used, so an identifier is absolutely necessary (thanks to phihag)
I think simply because without it mixing variable inside string will not be possible
$name = "bond";
echo "My name is $name" ;
Now without $ name will act as string .
精彩评论