Variable in PHP

Variable in php is very simple to define and uses only you need a $ attach with name of variable to define php variables. If you want to use php variable just use the name as define with $ sign e.g.

$username= "Ali Raza";
$age = 10;

in above example i have declare two variables. The first one has string value and 2nd one has int value. You can use these variables easily e.g., if you want to print these.

echo($username); // will print Ali Raza
echo($age); // will print 10

You may use print command to work same as echo.

print($username); // will print Ali Raza
print($age); // will print 10

if you run above code, it will print both in one line as there should be no line breaker between name and age if you want to give breaker, you can use html <br /> tag

print($username);
print("<br />");
print($age);

Now output will be in two lines. PHP variable is really easy as most languages we need to tell type but in PHP no worry if you give string value it will consider as string variable but if you give number value then it will work as number e.g.

$age=10;
echo($age); // will print 10
$age++;
ehco($age); // this will print 11

If you see we don't tell variable is number type but when we add one in this it will increase by one plus and print out next number.

In php mixing each other

We can mix string variable and number variables with each other for printing purposes e.g.

$firstName="Ali";
$secondName= "Raza"
$age=15;
echo("User Name is ". $firstName. " ". $secondName. " and age is : ". $age);
// output: User Name is Ali Raza and age is : 15

Variable is case sensitive, most of new students make this mistake and when they are defining variable e.g. $firstName but when they are going to use it they type $firstname. This is wrong variable will not fine and nothing print in this case. So, we should be careful variable should be same name what we define case and what we use case.

Variables in PHP update with new value if we assign new value. e.g.

$username="Ali Raza"; 
echo $username ; // print Ali Raza
$username= "Qadeer Minhas";
echo $username; // print Qadeer Minhas

if you note after changing value it print new value and forget previous value.