What is the Definition of Variable in PHP with Example ?

Definition of Variable

Definition of Variable: The variable is a name or symbol that stands for a value, such as variables that are the"containers" for storing the information. we can say that variable is used for storing values such as characters, a string of text characters strings, numeric values or memory addresses so that they can be used in the program, therefore, we can change variable values over the course of a script.
There are the Important points for Definition of Variable.>
A variable starts with $ a sign, it is followed by the name of the variable.
the variable does not need to be declared before adding a value to it because PHP automatically converts the variable to the correct data type, depending on its value.
therefore, a variable can be reused throughout the code after declaring a variable.
the assignment operator (=) can be used to assign the value to a variable.
In PHP the variable can be expressed or declared such as $var_name = value;

Definition of Variable: Declaring PHP variables

A valid variable name starts with a letter (A-Z or a-z) or underscore (_), followed by any numbers, underscores or numbers of letters.
In PHP all variables start with a $ (dollar) sign, followed by the name of the variable.
here, a $ sign is a special variable that can not be assigned.
suppose that if the variable name is more than one word, then it can be separated with an underscore such as for example ($employee_code instead of $employeecode).
PHP Variable names are case-sensitive such as ($ram and $RAM are two different variables not single)

In the above example as you have seen we have created two variables, the first variable has assigned
with a string value and the second has assigned with a number. when displayed the variable values on
the browser using the echo statement. Here PHP echo statement is often used to output data to the browser.

.
Note: when you assign the text value to a variable, then put text value in the quotes.
PHP has no command for declaring a variable, it is created the moment you first assign a value to it and always think that variables as containers for storing data.

Definition of Variable Example: Valid and invalid PHP variables

PHP variable name is case-sensitive


PHP is a Loosely Typed Language

In a language such as C++, C, and Java, a programmer must declare the name and type of the variable before
using it. PHP generally automatically associates a data type to the variable, it depends on its value.

therefore, in PHP the type of the variable no need to be declared before using it because the types are
associated with values rather than variables.

In PHP you can add a string to an integer without causing an error because the data types are not set in
a strict sense. as a result, we can say that a variable can change the type of its value as much as we want.

As previously, In the definition of Variable you have seen that you don’t need to declare their type or
variable before using them. therefore, the following example none of the variables are declared before
they are used, the fact is $height is an integer and $width is a floating number.

PHP Variables Scope

The variable of scope played an important role in PHP as you have seen, so the variable can be declared
anywhere in the script. furthermore, we can say that The scope of a variable is the part of the script,
where the variable can be used.
There are three different variable scopes in PHP

  • local
  • global
  • static

Local and Global Scope

A variable declared within a function has a LOCAL SCOPE, we can access only within that function:

A variable declared outside a function has a GLOBAL SCOPE, we can access only outside that function:

PHP static Keyword

Generally, when a function is executed/completed, all of its variables are deleted but sometimes according to need we want a local variable NOT to be deleted because we need it for a further job.

therefore at that condition use the static keyword when you first declare the variable:

PHP global Keyword

As you have seen above, the global keyword is used to access a global variable from within a function. therefore to do this, you will be used the global keyword before the variables (inside the function):

therefore, PHP also stores all global variable in an array which called $GLOBALS[index]. here, the index
holds the name of the variable. furthermore, you can also the access this array from within functions
and it can be used to update global variables directly.
the above example you can written in this way:

Definition of Variable: Assigning by Reference

PHP allows another way to assign the values to a variable like as assign by reference.therefore, the new variable simply points the original variable. you can change to the new variable affect the original and vice verse.
There are the following example:

PHP variable variables

I hope you know how to declare a variable in PHP, if you don’t know, see the post Definition of Variable which we have discussed the above but what if you want the name your variable is a variable itself? so we can say in that condition you have Variable Variables, so you may assign a variable to another variable.

if you see in the given below example at line number 2, we have declared a variable called $V which stores the value ‘Var1’ and if you see in line number 4, “Var1” is used as the name of a variable by using two dollar sign such as $$V.

therefore we can say that there are two variables now. $V which stores the value “Var1” whereas $$V which stores the value Var2. therefore, we can say that at this point $$V and $Var1 both are equal, both store the value “Var2”.

Definition of Variable: PHP Variables Scope

In the script, the variable can be declared anywhere, there are the following two types of variable scope, the first is the local scope, in the local scope where the variable is created and accessed inside a function and another is the global scope, in the global scope where the variable is created and accessed outside a function.

therefore, in the above example, you have seen that there are two variables $X and $Y and a function
Var_scope(). here $X is a global variable because if you have seen in the example it declared outside
the function and $Y is a local variable because, in the example, it is created inside the function
Var_scope(). therefore at the end of the script Var_scope() function is called, which his followed by two echo statements.

there are two statements Inside the Var_scope() function as you will see the above example, it prints the
value of $Y as it is the locally declared and it can not prints the value of $X since the function is created outside. therefore, the next statement print for script value $X since it is a global variable because
it’s not created inside any function. the value of $Y the last echo statement can’t print, it is created
insidethe function Var_scope() function, it is a local variable.