Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does PHP have a $ sign in front of variables? [closed]

Tags:

In PHP and some other scripting languages have the $var syntax while Java and other languages we can do just var.

Is there any theory behind it? Does it help them to parse. If not why would they choose to tack on an extra character in front?

like image 741
fastcodejava Avatar asked Feb 13 '10 12:02

fastcodejava


People also ask

What does & in front of variable mean in PHP?

An ampersand just before the function name will return a reference to the variable instead of returning its value.

Why does PHP start with variables?

Rules for PHP variables: A variable starts with the $ sign, followed by the name of the variable. A variable name must start with a letter or the underscore character. A variable name cannot start with a number.

What does the sign mean in PHP?

Rasmus Lerdorf, the father of the PHP language, explains the $ sign as an ability to insert variables inside literal string values (interpolation), so that the variables are distinguished from the rest of the string.

Why is PHP so cool?

Compatible with several OS and platform In addition, the programming language's interface is perfect with MySQL and Apache servers. Thus, it covers all major operating systems existing in various platforms. Likewise, PHP is also great for creating cross-platform web applications.


2 Answers

It prevents variable names conflicting with language keywords, and allows them to be interpolated in strings.

like image 134
Quentin Avatar answered Sep 24 '22 14:09

Quentin


My theory is that scripting languages such as php would need some way to continue to run even if a new reserved word is introduced, such as php4 -> php5 got catch added. Since its a scripting language any webpages that had catch as a variable name would not die, due to the change in the language.

This is not an issue with compiled languages since everything is converted to a binary and any changes in the language would not affect already compiled programs

like image 9
sinrtb Avatar answered Sep 25 '22 14:09

sinrtb