Download this Blogger Template by Clicking Here!

Ad 468 X 60

Saturday 9 March 2013

Widgets

Everything About PHP Variables Declaration And Assignment

What Are PHP Variables ?


Variables are the basic structure of a programming language. Memory is the place where we store our data which is required for the program. Data is stored temporarily in data container and its value can be changed.


PHP variables
PHP variables

Why We Need Variables?


If i want to make a program which gives the sum of two numbers and there are no variables in this program then the program will be like this

echo"5+2";

the answer or the above statement will be 7.

If I want to get the sum of 3 and 6 then by using the above method I will get 9. In the same way I will have to change the above code and replace the numbers with those numbers which I want to add. Like

echo"9+20"
or
echo"6+3"

etc

Now lets take another scenario. Lets suppose I want to make a Program in which the user inputs his name "Ali" in the textbox and then clicks enter. So as a result a page should open and a message should be printed on screen Like " Welcome Ali To My Site ". Now we need here a variable to store that name which user inputs int the textbox.

variables
Variables

How To Make And Declare Php Variables ?


It is very easy and simple to make variables is PHP. To make a variable in PHP, we just add a $ sign first and then the name of variable.

How To Identify A Variable ?


Variable name can consist of letters (A-Z, a-z) , numbers (0-9) , underscore( _ ) . You can not use space or other special characters in variable name and put a semicolon ( ; ) at the end of the variable name . Variable can be 30  characters long. Examples of correct variables are :

$a ;

$a_long_variable_name ;

$_2453 ;

$IT_World ;

Remember to add a semicolon ( ; ) at the end of every statement in PHP. The semi colon at the end of every variable above is not the part of variable name. It just indicates the end of statement.  Variable declaration is the process of making variables . Whenever we make a variable we actually reserve some space in memory for saving data. After defining and declaring data and reserving space in memory , a variable is assigned a value which stores in the memory . For example

$country = "Pakistan" ;

In above example I have made a variable named $country and I have assigned it a value "Pakistan". The above statement does not mean that $country is equal to "Pakistan". It just means that the variable $country has a value of "Pakistan".
Variable declaration
Variable declaration

How To Assign A Value To A Variable ?


The ( = ) operator is used to assign a value to a variable. This equal sign is that equal sign which we use in maths. If you are a new programmer then remember it that " =  " does not mean equals . This sign is an " Assignment Operator " and it is used to give a value to a variable. To make it more clear , now we will make a program which will remove your confusions.

If you want to Know How to Write ANd run PHP Code Then Check These



Open any text Editor and write the following code/program in it.
<html>
<head>
<title> PHP </title>
</head>
<body>
<?php
//declaring variables, adding two numbers & displaying result
$number_1 = 50 ;
$number_2 = 100;
$sum = $number_1 + $number_2 ;
echo " Sum is : ".$sum
?>
</body>
</html>

Output :

Sum is : 150

In the above code , you have seen that I have added comments and then declared two variables. The name of the variables are $number_1 and $number_2. Then I have assigned both these variables values

$number_1 = 50;$number_2 = 100;

Then we have declared a variable $sum and after calculating the sum total , the variable $sum is assigned the total sum.

Sum
Variable Sum

At the end of the program I have printed the value of $sum on screen using echo statement

echo " Sum is : ".$sum ;

You can see a dot in the above statement. It combines two statements. Whenever we want to join two stings , we add a (.). This is called Concatenation. In above statement we have combined two strings. One string is "Sum is:" and other is $sum . So by joing these two statements with the help of  a dot and then using echo statement the output is printed on the screen.


Looking Forward To You :



I am really trying my best and working hard for giving you all the knowledge that I know about PHP. Hoping that you people will be loving it. If you have any problem or query then Feel Free to ask questions. I will be waiting for your views and feedback in comments. Share this post with other and help others. Subscribe for free future posts. Stay Blessed. Be Happy and Make others Happy . Thanks. Peace .................

SHARE THIS POST   

  • Facebook
  • Twitter
  • Myspace
  • Google Buzz
  • Reddit
  • Stumnleupon
  • Delicious
  • Digg
  • Technorati
Author: Mohammad
Mohammad is the founder of STC Network which offers Web Services and Online Business Solutions to clients around the globe. Read More →

0 comments: