Creating Variables in PHP

Creating Variables


            A "variable" is a named container in a PHP script in which a  data value can be stored .
The stored value can be referenced using the variables name and changed the script proceeds .
The script author can chose any name for a variable providing it adheres to these naming convention .

1 . Name must begin with a $ (dollar sign) 
           for example 
                        $name

2 .  Name can comprise letters , numbers and underscore ,characters but not space 
              for example 
                             $total_1

3 . The first character after the $ dollar sign must  be a letter or an underscore character 
       - it can not be a number

PHP are case-sensitive  that variable name so $name ,$Name , $NAME are three separate individual variable .

 A variable is created in a PHP script simply by stating its name . The variable can then be assigned an initialized value by using the = operator to state its value . The statement and all others in PHP must end with semi-colon like this 
for example 
           $body_temp = 70.3 ;


The value contained within the variable can then by displayed using the variable name like this   
for example 
        echo $body_temp ;


Usefully the variables value can be displayed as part of a mixed string by enclosing the string and variables name in double quotes
for example 
     echo " Body temperature is $body_temp degrees Fahrenheit";


1 . Create a vaild  HTML document then insert PHP tags into the body section 
         <?php

              # Statement to be inserted here
         ?>

2 . Now insert between the PHP tags a statement to create and initialize a variable 
        $body_temp = 98.3;

3 . Next insert a statement to display the variable value 
        echo $body_temp ;

4 . Then insert a statement to display the variable subsuited mixed string  assigned in double quotes 
       echo "Body temperature is $body_temp degrees Fahrenheit";
              
PHP Program

       <?php
                $body_temp = 98.3 ;
                echo $body_temp ;
                 echo "Body temperature is $body_temp degrees Fahrenheit";
        ?>

Output
       


Comments

Popular posts from this blog

Switching Branches