开发者

why do php variables need to be identified by $? [duplicate]

This question already has answers here: Closed 11 years ago.

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 .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜