Saturday, 6 August 2016

PHP Introduction - Lecture #3

PHP - Variables

Hello All... welcome every one. I'm going to start PHP lectures for Very Beginners, who wants to learn PHP from very beginning. No matter if you do not have any basic programming concept. If you follow my tutorials I hope you will surely get some how good knowledge of PHP.
Today our Topic is
`PHP Variables`.
As if you concentrate on the topic it’s PHP Variable. A Variable means something that is changeable. PHP variable is termed as something that stores value or holds information is known as Variable, why its name is Variable because its value or information does not remains same it can be changed at any time. We can say variable is just like a Container of memory that holds some information in it. The main purpose of declaring a variable that we store our information in a variable and when needed we just simply call that variable.
There are three types of variables
i-                   Local Variables: variable that is accessed from just single page
ii-                 Global Variables: variable that can be accessed from multi pages
iii-               Session Variables: variable that can be accessed from the whole domain.
Here we will discuss just local variables. Rest of others will be discussed in next topics

Declaring a Variable
We can declare multiple variables, with multiple type of information i.e. any character, full sentence, digits, special characters, and also alphanumeric information. But remember the name of each variable must be different otherwise the information we have stored can mix up. Variable names are case sensitive.
Let’s see how to declare a variable. The same process first start PHP tag and close it and start coding inside of these.
            `$` dollar sign is used to initialize a variable.  $ sign with any word is known as a variable. And give this variable a name. let suppose a
            <?php
$a = 10;
            ?>
Above code shows that variable with name a holds the information/value of number 10. In last ( ; sign ) known as terminator means here the value is ended. As it’s important to give this terminator sign at the end of value of the variable. You can declare variable with following formats.
            Formats for declaring a variable
$variableName     (correct)  
$variable_Name    (correct)  
$variable1             (correct)
$variable_2           (correct)  
Remember you cannot declare a variable with a name that starts with a number
$1variable             (wrong)   

Information Holds a Variable
$myVariable = “Double quotes work.”;         (correct)               
$ myVariable = ‘Single quotes work too.’;     (correct)               
$ myVariable = “ One or other. ’;                    (wrong)              
Any information inside quotation is known as String variable, all the characters and alphanumeric values placed within single or double quotation.
Any numeric values are called as Integer variable, and these are not placed within quotations.
Even if you place your numeric values within quotation these will be called as String variable not an Integer.

$string = ‘Don’t mix your quotes.’;                  (wrong)               

$string = “Don’t mix your quotes.”;                 (correct)
$string = “He said “that’s fine,” and left.”;       (wrong)
$string = “He said \“that’s fine,\” and left.”;     (correct)

PRACTICE
<?php
            $variable_one = 20;
            $variable_two = 30;
            $result = $variable_one + $variable_two;
            echo $result;
//The result will be 30
?>
Furthermore you practice on variables by adding, subtracting, multiplying dividing different values with each other.

Thanks Guys. Any thing missing or any question from this topic you can comment below, I will be trying to solve and answer your question.
Our Next topic will be `Operators`.
Good Luck J

No comments:

Post a Comment