Come Join our Internship Program with Us

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Join Us

How to declare a variable in PHP:

Lorem Ipsum is slechts een proeftekst uit het drukkerij- en zetterijwezen. Lorem Ipsum is de standaard proeftekst in deze bedrijfstak sinds.

How to declare a variable in PHP

A variable is a symbol or a term that represents a value. Variables are used to store variables such as numeric numbers, characters, character strings, or memory locations that may be utilised elsewhere in the program.

PHP Variable Declaration:

In PHP, all variables begin with a $ (dollar) symbol and are followed by the variable's name.
An acceptable variable name begins with a letter (A-Z, a-z) or an underscore (_), and can contain any number of letters, numerals, or underscores.
If a variable name contains more than one word, use an underscore to separate them (for example, $employee_code instead of $employeecode).
'$' is a non-assignable special variable.

Example : Valid and invalid PHP variables

PHP variable name is case-sensitive

Consider the following example :
Value of abc : WelcomeValue of ABC :
Copy Output of the above example

Value of ABC: Welcome
Value of ABC:

In the above example, the different capitalization schemes make for different

PHP is a general-purpose programming language.

Before using a variable in a language such as C, C++, or Java, the programmer must define its name and type. Because types are connected with values rather than variables, they do not need to be defined before usage in PHP. As a result, we may modify the type of a variable's value as often as we like.

As previously stated, variables and their types do not need to be declared before being used in PHP. None of the variables in the following example are defined before they are used; nonetheless, $height is a floating number and $width is an integer.

PHP variables: Assigning by Reference

PHP (from PHP4) offers another way to assign values to variables: assign by reference. This means that the new variable simply points the original variable. Changes to the new variable affect the original, and vice vers

PHP variable

You know how to declare variables in PHP. But what if you want the name your variable is a variable itself? In PHP, you have Variable Variables, so you may assign a variable to another variable.

In the following example at line no. 2, we declared a variable called $v which stores the value 'var1' and in line no. 4, "var1" is used as the name of a variable by using two dollar signs. i.e. $$v.

Therefore there are two variables now. $v which stores the value "var1" where as $$v which stores the value var2. At this point $$v and $var1 are equal, both store the value "var2".

Line 2 of the following example declares a variable named $v that contains the value 'var1', while line 4 uses "var1" as the name of a variable by using two dollar signs. i.e. $$v.

As a result, there are now two variables. $v stores the value "var1" whereas $$v stores the value "var2." At this moment, $$v and $var1 are equal and both store the value "var2".

PHP Variables Scope

In PHP, variables can be declared anywhere in the script. We declare the variables for a particular scope. There are two types of scope, the local scope where variables are created and accessed inside a function and global scope where variables are created and accessed outside a function.

In the above script there are two variables $x and $y and a function var_scope(). $x is a global variable since it is declared outside the function and $y is a local variable as it is created inside the function var_scope(). At the end of the script var_scope() function is called, followed by two echo statements. Lets see the output of the script :

The value of x is:
The value of y is: 20,
The value of x is: 10
The value of y is:

whatsapp