This article may contain affiliate links. If you buy some products using those links, I may receive monetary benefits. See affiliate disclosure here
Video
The variable names in PHP must adhere to a few rules:
Variable Rules
- Use the
$
before a variable to denote it - A variable name should contain only alphabets [A-Z][a-z], digits [0-9], and the underscore (_) character
- It should not start with a digit
Based on that here is an example of a PHP variable:
$name = "Abhinav";
Here are a few more examples of valid variable names:
$first_name = "Abhinav"; // a string variable
$_x = 123; // integer
$ghs_7_gGH____53945_ = "bla bla"; // string
echo $first_name . "<br>" . $_x . "<br>" . $ghs_7_gGH____53945_;
However, the following is an invalid variable:
$5name = "I am not valid";
Variable Naming Practices
- Try to use meaningful variable names
- Avoid using single character names like the following:
$c = 5;
$r = "manager";
Instead, it could be:
$person_count = 5;
$person_role = "manager";
The above style is an example of snake case variable names. You can also use camel case like:
$personCount = 5;
$personRole = "manager";
Try to stick to the same style while naming variables and functions to maintain consistency.
Variable Scope
Consider the following code snippet:
$count = 10;
echo $count;
function showCount() {
$count = 20;
echo $count;
}
showCount();
echo $count;
The output will be:
10
20
10
$count
outside the function has a global scope, while the the $count
inside the function has a local scope.
To access the global variable inside the function, use the global keyword:
$count = 10;
echo $count;
function showCount() {
global $count;
$count = 20;
echo $count;
}
showCount();
echo $count;
Now the output will be:
10
20
20
Or you can also try the $GLOBALS array:
$count = 10;
echo $count;
function showCount() {
echo $GLOBALS['count'];
}
showCount();
echo $count;
The output will be:
10
10
10
Static Variables
Static variables prove useful when you want to maintain a local variable’s even after the function has finished executing.
function increment() {
static $count = 0;
$count = $count + 1;
echo $count . "<br>";
}
increment();
increment();
It will produce:
1
2
The assignment ($count = 0
) gets executed only in the first function call.
PHP Constants
define('SITE_URL', 'https://codingreflections.com');
echo SITE_URL;
Things to note:
- use the define() function to create a new constants
- value of a constant cannot change
- no need to prefix with $ sign
- follows same naming rules as variables
- however, it’s a common practice to use all caps for constants to distinguish from variables
- constants have global scope, works inside and outside functions
The following will cause an error:
define('SITE_URL', 'https://codingreflections.com');
echo SITE_URL;
define('SITE_URL', 'https://example.com');
Constants have same value inside and outside functions.
define('SITE_URL', 'https://codingreflections.com');
echo SITE_URL;
function showUrl() {
echo SITE_URL;
define('ANOTHER_URL', 'https://example.com');
}
showUrl();
echo ANOTHER_URL;