Dollar symbols in front of a variable name.

A

Anonymous

Guest
The www is teaching me PHP. I am a beginner!
I wanted to know why does php use '$' in front of variable names?
It certainly makes them easier to spot, but is there a syntatical reason?

Are there any laws about variable names?
(I know this is a base question :sleep: , but every answered question helps!)

<?php
$Lyndsey = 'confused';

If ($Lyndsey=='confused')
{
Echo "Lyndsey understands, but is mostly confused!";
}

else
{
Echo "Lyndsey is not confused.";
}
?>
 
trufla said:
I wanted to know why does php use '$' in front of variable names?
It certainly makes them easier to spot, but is there a syntatical reason?

No, not really. It's a decision the designers made early on, probably influenced by Perl. In my opinion it makes things a little easier for newbies, since they can tell more easily what things are variables and which are functions, etc. It would be possible to redesign PHP to work without the $s, but the designers chose not to do that.

Are there any laws about variable names?

Variable naming rules are described by the documentation like so:

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

Beyond that, there are no "laws", and there's no truly standard convention. It's my personal preference (like many others) that all normal variable names are lower-case with words separated by underscores (e.g. $record_count). The closest PHP has to a standard convention is the PEAR Coding Standards, which are a good starting point if you're beginning a large project and want to have unified conventions that everyone can follow.
 
Back
Top