Creating (Declaring) PHP Variables
In PHP, a variable is a storage location that holds a value. Variables are used to store data such as numbers, strings, and arrays, and they can be used in expressions and assignments.
PHP variables are denoted by a dollar sign ($) followed by the variable name. The variable name can consist of letters, numbers, and underscores, but it must start with a letter or underscore.
Here are a few examples of PHP variables:
<?php
$name = "John";
$age = 30;
$is_admin = true;
echo "Name: $name"; # Outputs "Name: John"
echo "Age: $age"; # Outputs "Age: 30"
echo "Is admin: $is_admin"; # Outputs "Is admin: 1"
?>
In the above example, $name is a string variable that holds the value “John”, $age is an integer variable that holds the value 30, and $is_admin is a boolean variable that holds the value true.
PHP variables are case-sensitive, so $name and $NAME are considered to be different variables.
Rules for PHP variables:
In PHP, there are a few rules that you should follow when naming variables:
A variable name must start with a letter or underscore.
A variable name can only contain letters, numbers, and underscores.
A variable name cannot contain spaces or special characters other than underscores.
A variable name is case-sensitive, so $myVariable and $myvariable are considered to be different variables.
It is a good idea to use descriptive and meaningful names for your variables, as this can make your code easier to read and understand. Many PHP developers use lowercase letters and separate words with underscores, like this: $my_variable.
Here are a few examples of valid and invalid variable names in PHP:
<?php
$name = "John"; # Valid
$_name = "John"; # Valid
$Name = "John"; # Valid
$name123 = "John"; # Valid
$123name = "John"; # Invalid (starts with a number)
$name# = "John"; # Invalid (contains a special character)
?>
PHP Variables Scope
In PHP, a variable’s scope refers to the context in which it is defined and can be accessed. There are two types of variable scope in PHP: global and local.
A global variable is defined outside of any function and is available to all parts of the program. It can be accessed and modified from any function, class, or file in the program.
A local variable is defined inside a function and is only available within the function in which it is defined. It cannot be accessed or modified from outside the function.
Here is an example of global and local variables in PHP:
<?php
$global_variable = "I am a global variable."; # Global variable
function myFunction() {
$local_variable = "I am a local variable."; # Local variable
echo $global_variable; # Outputs "I am a global variable."
echo $local_variable; # Outputs "I am a local variable."
}
echo $global_variable; # Outputs "I am a global variable."
echo $local_variable; # Causes an error (undefined variable)
?>
In the above example, $global_variable is defined outside of any function and is therefore a global variable. It can be accessed and modified from any function, class, or file in the program.
$local_variable is defined inside the myFunction function and is therefore a local variable. It can only be accessed and modified within the myFunction function and is not available outside of it.