Why we declare some variables in PHP as like $_variablename
?
Does _
define something?
Please help me to clear this up, thanks.
Why we declare some variables in PHP as like $_variablename
?
Does _
define something?
Please help me to clear this up, thanks.
It's a naming convention.
From the pear manual on naming conventions:
Private class members are preceded by a single underscore. For example:
$_status
The underscore does not mean that the variable is private. It is not necessary to use the underscore. It’s simply a naming convention that reminds the developer that the variable/property/method is either private or protected
For Example
// This variable is not available outside of the class
private $_someVariable;
class MyClass {
// This method is only available from within this class, or
// any others that inherit from it.
protected function __myFunction() {}
}
In the code above, the underscore is not what makes the variable or method private; the private/protected keyword does that.
It is not mandatory to use '_' or '__' in your code but it helps you to understand the variable/function access type by just seeing the name.
In some PHP frameworks you may see:
_ for protected variable/function
__ for private variable/function
this is mainly used to declare global variable and to in function.. generally it is not compulsory to use '_' this
http://php.net/ check this