January 12, 2016

Declaring Variable in PHP

In PHP a variable is represented with a $ sign and followed by the name of the variable. A variable name must start with a letter or the underscore character but cannot begin with a number. Variable name is case-sensitive. So $name, $Name, and $NAME are three different variables. A variable can have a short name ie x and y. When assigning a text value to a vairable, two double quotes are used to the value that has been assigned. A variable name can only contain alpha-numeric characters and underscores A-z, 0-9, and _ . The following example will show how to output a variable:

A variable can store a string or a number.

All variable names in PHP should start with a dollar sign ( $ ).

And to end the statement PHP requires a semicolons.

Note : No quotes should be used around numbers.

Example :

$myCountry = "INDIA";
$countryCode = 91;

Let's get started by creating a variable and giving it a value.

Now, Declare a variable, $myCountry, and give it your Country name as a string.

    <html>
    <body>
    <?php
    $myCountry = "INDIA";
    echo "$myCountry";
    ?>  
    </body>
    </html>
 
    Use echo to print out your country name

More Example of Variables :

    <html>
    <body>
    <?php
    $text = "Example";
    echo "Displaying temperature using " . $text . "!";
    ?>
     <br /> <br />  
 
    <?php
    $v_temp = 30.2;
    echo "$v_temp &deg;C";
    ?>
    <br /><br />

    <?php
    $name = "Iwriteless";
    echo $name;
    ?>
    </body>
    </html>

A variable is declared using the = operator, and the variable on the left side and the name of the variable to right side. To end the statement add a semicolon (;) Simply echo the variable name to output data to the screen. Now save the file as .php and HIT SAVE.


END...

No comments :

Post a Comment