PHP Variables declaration | What is variables

PHP Variables

PHP Summer Training/Internship with 2 Live Projects in Noida Fee is only 5500/-Click here to Book Your Seat


PHP Variables

Variable is nothing it is just name of the memory location.
A Variable is simply a container i.e used to store both numeric and non-numeric information.

Rules for Variable declaration

  • Variables in PHP starts with a dollar($) sign, followed by the name of the variable.
  • The variable name must begin with a letter or the underscore character.
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • A variable name should not contain space

Assigning Values to Variables

Assigning a value to a variable in PHP is quite east: use the equality(=) symbol, which also to the PHP’s assignment operators.
This assign value on the right side of the equation to the variable on the left.
A variable is created the moment you assign a value to it:

Eg i


In the above example
Create a variable ($mycar)containing a string with value=”Honda”.
To print the carname pass $mycar inside echo statement.

PHP Concatenation

Eg ii (concatenate variable with string)

Honda City is riding

In the above example
Variable($mycar) hold value=”honda city”. Now we wants to concatenate variable with string.
pass this variable($mycar) inside echo statement.
To concatenate this with a string(“is riding”) use dot(.) between variable name and string.
The output will be displayed : Honda City is riding

Eg iii (Sum of two numbers)

Sum = 300

In the above example
Declare $first , $second variable with value=100, 200 respectively.
Now add these two numbers using arithmetic operator (“+”).
sum of these two variable result stored in a third variable($sum).
Now print the sum passing ($third) with echo statement with a string.

Eg iv (Subtraction of two numbers)

Subtraction = 500

In the above example
We perform subtraction using variables( $first, $second) with vale=1000,500.
Subtract second variable from first, result is hold by third variable($third) .
Print this third variable passing with echo statement.

Destroying PHP Variables

To destroy a variable, pass the variable to PHP’s unset( ) function.
as in the following example:
Eg v


In the above example
declare variable $name hold value=”steve”. In this program we used unset() function to delete a particular variable.
first it show the output: “steve”, because we pass unset function after echo statement.
Now pass variable name inside unset($name) function output will show an Notice error(Variable is undefined).

Eg vi

Sum = 300

Sum = Notice error undefined third variable

Note : Trying to access or use a variable that’s been unset( ), as in the preceding script, will result in a PHP “undefined variable” error message.
This message may or may not be visible in the output page, depending on how your PHP error reporting level is configured.

Variable names in PHP are case-sensitive


Variable names in PHP are case-sensitive. As a result, $name refers to a different variable than does $NAME.

Inspecting Variable Contents(Variable Property)

PHP offers the var_dump( ) function, which accepts a variable and X-rays it for you.
Here’s an example
Eg vii ( For String value )

string ‘Fiona’ (length=5)

int 25

In the above example
We use var_dump( ) function to check the Contents(property) of variable, $name hold a string value =”Fiona” while $age hold an integer value = 25.
Now pass this variable inside var_dump($name,$age) function .
It show all information related this(data type of variable, length(only string), value)

Eg viii ( For Integer values )

int 300

Eg ix ( For Floating value )

float 300.7

In the above example
variables first hold $first=100.5, second hold $second=200.2.
Now add these two values, result is stored in third variable($third).
Pass this variable inside var_dump($third) to check the content.
output will float 300.7

Eg x ( For Boolean value )

Boolean true

In the above variable
$bool with value=”true”. var_dump($bool) function is used to display the result
so output will display Boolean true, because variable holds a Boolean value

Latest Trending Technologies