Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

 Variable and Constant in PHP:-

Variable:-  It is the special identifier in PHP whose value can be changed. variable always will be declared using alphabets.

PHP variable will be started from $ symbol.

Syntax of Variable Declaration in PHP


$a=10;       //Integer Type Variable

$b="10";    //Numeric String

$c = '10';   //Numeric String

$d = 10.0;     //Float Value

PHP provides a loosely coupled declaration for variable means no need to write data type on the variable declaration, it will be managed by assigned data.
 using this concept we can use a single name variable for multiple values.


$a = "abc";

$a =True;

mobile no = "9812311111"  //incorrect

mobile_no= "981233444"  //correct

1a = 10   // incorrect
a1=10  //correct

note:-  space,numeric,keyword,capital letter,special char not allowed in variable declaration.

 WAP to calculate Simple Interest in PHP?

echo "<h1>SI Program</h1>";
echo "<hr>";
$si = ($p*$r*$t)/100;

echo "p=$p,r=$r,t=$t,result=$si ";

//echo $p,$r,$t,$si;


Constant in PHP:-

we can not change the value of the identifier in the program using constant.

in PHP constant will be defined by define() and access by without $ symbol.

Syntax of Constant in PHP:-



define(a,20)  //error because we can not change value of constant
echo a;
echo b;

Example of Constant in PHP?

echo a+b;


WAP to swap two number with using the third variable:-


echo $a,$b,"<br>";
echo $a,$b;

Variable and Constant in PHP

Assignment of PHP:-

1) WAP to Swap Two Number Without Using Third Variable?


echo $a,", ",$b,"<br>";
$a=$a+$b;  //120
$b=$a-$b;  //100
$a=$a-$b;  //20
echo $a," ," ,$b;

2) WAP to Calculate Square and Cube of any number?



$sq = $num*$num;
$cb = $num*$num*$num;
echo "Square is ",$sq;
echo "Cube is ",$cb;

3) WAP to Calculate Area of Circle?

4) WAP to Calculate Area of Triangle?

5) WAP to convert temperature from Celsius to Fahrenheit?


Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post