Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, 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

$identifier=value

$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=10;

$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?

<?php
echo "<h1>SI Program</h1>";
echo "<hr>";
$p=12000;
$r=2.2;
$t=2;
$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('identifier',value)


define(a,100)   
define(b,200)

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


Example of Constant in PHP?
<?php

define('a',100);
define('b',200);
#define('a',10);
echo a+b;

?>

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

<?php

$a=10;
$b=20;
echo $a,$b,"<br>";
$temp=$a;
$a=$b;
$b=$temp;
echo $a,$b;
?>

Variable and Constant in PHP

Assignment of PHP:-

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

<?php

$a=100;
$b=20;
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?

<?php

$num=5;

$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