Tuesday, November 15, 2011

How to Create Variables in PHP

variables in php

Summary: In this tutorial I will explain you what are variables, how to create variables in PHP, rules for naming variable , how to assign values to variables, strings , integers and floats, how to create comments and lastly there is a video tutorial on How to Create variables in PHP in both English and Hindi.

What is a Variable

A variable is used to store unknown or changing values. In simple words a variable is just like a container to hold values, say a bottle of water can be considered as a variable, the bottle itself is a variable and the water inside it is value. The level of water can vary inside bottle and so with variable.

A bottle may come in different shapes and sizes to store various types of liquid, say a water bottle, milk bottle and cold drink bottle, similarly variables comes in many flavours known as strings, integers, float and boolean. These are know as data types in programming terms.

Variables in PHP

To create a variables in PHP you must follow few simple rules:

  • Variable names must begin with a $ sign.
  • First letter of variable name cannot be a number.
  • There should be no space in variable names.
  • You can use alphabets, numbers and underscore in variable names, all other characters are not allowed in variable names.

You write $ sign followed by variable name and then put a assignment operator that is = sign and then put the values to be stored in that variable. In PHP you do not explicitly define the data types like in many other languages C, C++, Java, data types are set implicitly according to the values a variable contains.

So value with quotes will become String, a value without quotes will become Integer and values with decimal point will become floating point automatically.

<?php
 
  // String Variable
 $myName = "Ankit";
  
  
  // Integer Variable 
  $age = 25;
  
  
  // Float Variable
  $hieght = 5.7;
  
  
  // Concatenating Variables and displaying result  
  echo $myName . " " . $age . " " . $height;
  
?>

You can use . operator, also know as concatenation operator to join two values in PHP, you will see them everywhere in PHP code.

Variable names are case sensitive in PHP, so a variables called $myName and $MyName are not equal in PHP.

<?php
 
 $myName = "Ankit";
  
  
  $MyName = "Ashish";
  
  echo $myName . " <p></p> ";
  
  echo $MyName;
  
?>

You can use two forward slashes // to create single line comment and forward slash star /* and star forward slash */ to create multi lines comments in PHP. Comments are not processed by PHP interpreter and they are used to make your code organized and readable, comments help you a lot when you project becomes complex.

<?php

 // single line comment
  
  
  /* 
  This is 
  Multi line comments
  */

?>

Variables in PHP [Video tutorial in English]

Variables in PHP [Video tutorial in Hindi]

This is the fifth chapter in which I explained you How to create Variables in PHP, in the next chapter I will explain you Booleans in PHP.