Learn PHP Variable

A variable is a memory location that hold data.Variable used to store data inside temporary .In PHP ,we declare variable using ($).
Syntax of desclaring a variable:

<?php    
$variable="Any Value";   
?>  

PHP Variable to store string, integer and float

See the diffrent examples of variable decration:

<?php  
$str="I am Akshay";  
$a=220;  
$b=41.6;  
echo "string is: $str 
"
; echo "integer value is: $a
"
; echo "float value is : $b
"
; ? >

Output:

string is: I am Akshay
integer value is: 220 
float value is: 41.6 

PHP add the values inside variables

<?php  
$a=10;  
$b=20;  
$c=$a+$b;  
echo $c;  
?>  

 Output:

30

PHP case senstive Variables

Inside PHP variables are case sensitive that mean  $Name  and $NAME are two diffrent variables . I you use diffrent name or not the same variable name ,it will show you error . Shown below:

<?php  
$name="akshay";  
echo "My name is " . $color . "
"
; echo "My name is " . $NAME . "
"
; echo "My name is " . $naME. "
"
; ?>

Output:

My car is akshay
Notice: Undefined variable: NAME in C:\wamp\www\variable.php on line 4
My name is 
Notice: Undefined variable: naME in C:\wamp\www\variable.php on line 5
My name is 

PHP invalid Variables

Some variables are invalid inside PHP .That means PHP dont accept them and show error.

Shown Below:

<?php 
$12c="hi there";//number in begining (invalid)  
$c12="hi there again ";//number after text (valid)  
$*data="hi there";//special symbol (invalid)  
  
echo "$12c 
$*data
"
; echo $c12; ?>

Output:

Parse error: syntax error, unexpected '4' (T_LNUMBER), expecting variable (T_VARIABLE)
 or '$' in C:\wamp\www\variableinvalid.php on line 2
hi there


 I Hope you understand the concept behind Variables . If yes continue to next lesson :)



Last Updated: 2018-07-06 12:32:44 By akshay

SHARE THIS

share_facebook share_twitter share_google_plus

Please Comment Here :