التخطي إلى المحتوى الرئيسي

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?


 

تعليقات

  1. shadab shah

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

    ردحذف
  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;

    ردحذف
  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;



    ?>

    ردحذف
  4. Shadab Shah
    How to Convert Celsius to Fahrenheit


    ردحذف
  5. Aamir Shaikh
    How to Calculate the area of Circle


    ردحذف
  6. Mansi Dubey

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

    ردحذف
  7. Mansi Dubey

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

    ردحذف
  8. Mansi Dubey

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

    ردحذف
  9. Mansi Dubey

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

    ردحذف
  10. Mansi Dubey

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

    ردحذف
  11. Kritika Barod

    Calculate Area of Circle--

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

    ردحذف
  12. Kritika Barod

    convert temperature from Celsius to Fahrenheit

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

    ردحذف
  13. ";
    echo "area of circle is - $A";

    ?>

    ردحذف
  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";

    ردحذف
  15. Area of Circle

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

    echo "Area of Circle is " .$a;

    ردحذف
  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';

    ردحذف
  17. //area of rectangle
    $l=10;
    $b=20;
    $area=$l*$b;
    echo("Area of rectangle is:-" .$area);

    ردحذف
  18. // area of triangle
    $base=20;
    $height=10;
    $t_area=($base*$height)/2;
    echo("
    ");
    echo("Area of triangle:-" .$t_area);

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

    ردحذف
  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);

    ردحذف
  21. WAP to find greater number using if else condition
    $b)
    {
    echo "a is greater";
    }
    else
    {
    echo "b is greater";
    }
    ?>

    ردحذف

إرسال تعليق

POST Answer of Questions and ASK to Doubt

المشاركات الشائعة من هذه المدونة

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

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...

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...