Variable and Constant in PHP

22

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?


 
Tags

Post a Comment

22Comments

POST Answer of Questions and ASK to Doubt

  1. shadab shah

    $a=8;
    $b=14;
    echo $a,",",$b,"
    ";
    $a=$b+$a;
    $b=$a-$b;
    $a=$a-$b;
    echo $a,",",$b;

    ReplyDelete
  2. Aamir Uddin
    $a=16;
    $b=4;
    echo $a,",",$b,"
    ";
    $a=$a+$b; //20
    $b=$a-$b; //16
    $a=$a-$b; //4
    echo $a,",",$b;

    ReplyDelete
  3. Aamir Shaikh
    How to calculate area of tringle
    <?php
    $base=21;
    $height=23;
    echo "area with base $base and height $height=". ($base*$height)/2;



    ?>

    ReplyDelete
  4. Shadab Shah
    How to Convert Celsius to Fahrenheit


    ReplyDelete
  5. Aamir Shaikh
    How to Calculate the area of Circle


    ReplyDelete
  6. Mansi Dubey

    <?php
    $r=5;
    $area_of_circle=(3.14*$r*$r);
    echo "Area of circle is = ".$area_of_circle;
    ?>

    ReplyDelete
  7. Mansi Dubey

    <?php
    $r=5;
    $area_of_circle=(3.14*$r*$r);
    echo "Area of circle is = ".$area_of_circle;
    ?>

    ReplyDelete
  8. Mansi Dubey

    <?php
    $r=5;
    $area_of_circle=(3.14*$r*$r);
    echo "Area of circle is = ".$area_of_circle;
    ?>

    ReplyDelete
  9. Mansi Dubey

    <?php
    $base=100;
    $height=50;
    $area_of_triangle=(1/2*$base*$height);
    echo "Area of triangle is = ".$area_of_triangle;
    ?>

    ReplyDelete
  10. Mansi Dubey

    <?php
    $C=32;
    $F=$C*1.8+32;
    echo "Temperature in Fahrenheit = ".$F;
    ?>

    ReplyDelete
  11. Kritika Barod

    Calculate Area of Circle--

    Area of Circle is A=$A";
    ?>

    ReplyDelete
  12. Kritika Barod

    convert temperature from Celsius to Fahrenheit

    ";
    $f=($t*9/5)+32;
    echo "F is=$f";
    ?>

    ReplyDelete
  13. ";
    echo "area of circle is - $A";

    ?>

    ReplyDelete
  14. Calculate Area of Triangle

    $b=10;
    $h=12;


    $at = ($b*$h)/1/2;

    echo "Area of Triangle is ".$at;
    echo ("
    ") ;
    echo ("
    ") ;
    echo"The Formula of Area of Triangle is =(b*h)/1/2";

    ReplyDelete
  15. Area of Circle

    define('a',3.142);
    $num=5;
    $sq=$num*$num;
    $a= a*$sq;
    echo ("
    ");

    echo "Area of Circle is " .$a;

    ReplyDelete
  16. Celsius To Fahrenheit Converter

    $c=6;
    $f=(($c*9)/5)+32;

    echo "Celcius to Fahrenheit Converter";
    echo ("
    ");
    echo ("
    ");
    echo ("
    ");
    echo "$c Degree Celcius Temperature is Equal To ". $f , "", ' Fahrenheit';

    ReplyDelete
  17. //area of rectangle
    $l=10;
    $b=20;
    $area=$l*$b;
    echo("Area of rectangle is:-" .$area);

    ReplyDelete
  18. // area of triangle
    $base=20;
    $height=10;
    $t_area=($base*$height)/2;
    echo("
    ");
    echo("Area of triangle:-" .$t_area);

    ReplyDelete
  19. // area of circle
    $r=5;
    define('pi',3.14);
    $c_area=pi*$r*$r;
    echo("
    ");
    echo("Area of circle:-" .$c_area);

    ReplyDelete
  20. // program to convert temperature from Celsius to Fahrenheit fahrenheit = (celsius * 1.8) + 32
    $c=37;
    $f=$c*1.8+32;
    echo("
    ");
    echo("Temperature in celsius:-" .$f);

    ReplyDelete
  21. WAP to find greater number using if else condition
    $b)
    {
    echo "a is greater";
    }
    else
    {
    echo "b is greater";
    }
    ?>

    ReplyDelete
Post a Comment