Skip to main content

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?


 

Comments

  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

POST Answer of Questions and ASK to Doubt

Popular posts from this blog

DSA in C# | Data Structure and Algorithm using C#

  DSA in C# |  Data Structure and Algorithm using C#: Lecture 1: Introduction to Data Structures and Algorithms (1 Hour) 1.1 What are Data Structures? Data Structures are ways to store and organize data so it can be used efficiently. Think of data structures as containers that hold data in a specific format. Types of Data Structures: Primitive Data Structures : These are basic structures built into the language. Example: int , float , char , bool in C#. Example : csharp int age = 25;  // 'age' stores an integer value. bool isStudent = true;  // 'isStudent' stores a boolean value. Non-Primitive Data Structures : These are more complex and are built using primitive types. They are divided into: Linear : Arrays, Lists, Queues, Stacks (data is arranged in a sequence). Non-Linear : Trees, Graphs (data is connected in more complex ways). Example : // Array is a simple linear data structure int[] number...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

Conditional Statement in Python

It is used to solve condition-based problems using if and else block-level statement. it provides a separate block for  if statement, else statement, and elif statement . elif statement is similar to elseif statement of C, C++ and Java languages. Type of Conditional Statement:- 1) Simple if:- We can write a single if statement also in python, it will execute when the condition is true. for example, One real-world problem is here?? we want to display the salary of employees when the salary will be above 10000 otherwise not displayed. Syntax:- if(condition):    statements The solution to the above problem sal = int(input("Enter salary")) if sal>10000:     print("Salary is "+str(sal)) Q)  WAP to increase the salary of employees from 500 if entered salary will be less than 10000 otherwise the same salaries will be displayed. Solution:- x = int(input("enter salary")) if x<10000:     x=x+500 print(x)   Q) WAP to display th...