-->

  • PHP 7 Variables



    Kumar Atul Jaiswal






    PHP 7 Variables

    Variables are  "containers"  for storing information



    Creating ( Declaring ) PHP  Variables

    In PHP a variables start with the       sign, followed by the name of the variable : 



    Example 



    <!DOCTYPE html>
    <html>
    <head>

    <title> PHP Variables </title>

    </head>
    <body>

    <h1> </h1>


    <?php


    $txt = "Hello Mr. Kumar Atul Jaiswal.";
    $x = 10;
    $y = 10.5;

    echo $txt;
    echo "<br>";
    echo $x;
    echo "<br>";
    echo $y;



    ?>



    </body>
    </html>




    Result

    Hello Mr. Kumar Atul Jaiswal.
    10
    10.5







    After the execution of the statements, above the variable $txt will hold the value   "Hello Mr. Kumar Atul Jaiswal"   the variable $x will hold the value 10, and the variable $y will hold the value 10.5 .


    NOTE :- When you assign a text value to a variable, put quotes around the value.
    NOTE :- Unlike other programming languages, PHP has no command for declaring a variable. It is created the moment you first assign  a value to it.



    • Think of variable as container for storing data. 





    PHP Variables    

    A variable can have a short name ( like x and y) or a more descriptive name ( like carname, total_volume, age, old ).


    Rules for PHP variables 

    • A variable start with the $ sign, followed by the name of variable
    • A variable name must start with a letter or a underscore character.
    • A variable name cannot start with a number.
    • A variable name can only contain alpha-numeric characters and under score ( like A-z, 0-9 )
    • Variable names are case sensitive ( $age or $AGE are two different values).





    Output variables 


    The php echo statement is often used to output data to the scree.

    The following example will show how to output text and a variable :




    Example


    <!DOCTYPE html>
    <html>
    <head>

    <title> PHP Variables </title>

    </head>
    <body>

    <h1> </h1>


    <?php

    $txt = "www.kumaratuljaiswal.in";
    echo "I Love $txt";

    ?>



    </body>
    </html>




    Output 

    I Love www.kumaratuljaiswal.in


    The following example will produce the same output as the example above:



    Example


    <!DOCTYPE html>
    <html>
    <head>

    <title> PHP Variables </title>

    </head>
    <body>

    <h1> </h1>


    <?php

    $txt = "www.kumaratuljaiswal.in";
    echo "I Love " .  $txt  . ;

    ?>



    </body>
    </html>




    Output 

    I Love www.kumaratuljaiswal.in




    The following example will output the sum of two variables:


    Example


    <!DOCTYPE html>
    <html>
    <head>

    <title> PHP Variables </title>

    </head>
    <body>

    <h1> </h1>


    <?php

    $x = 10;
    $y = 5;

    echo $x+ $y;


    ?>



    </body>
    </html>




    Output 

    15 














  • 0 comments:

    Post a Comment

    For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.