Monday, August 6, 2012

PHP variables

Today we will learn about PHP variables.

One can use PHP variables to store informations there are some rules for PHP variables such as
  • PHP variable starts with the $ sign, and followed by name of the variable.
           for example: $text
  • PHP variable name much begin with a letter or the underscore character.
  • PHP variable name can only contain alphabetic, numeric characters and underscore.
  • Variable name should not contain spaces.
  • Variable names are case sensitive.
            for example: $text and $TEXT are two different variables.

Here is simple programme for sum of two numbers using variable.

<!DOCTYPE html>
<html>
<body>


<?php
$no1=5;
$no2=10;
$answer=$no1+$no2;
echo
$answer;
?>

</body>
</html>


-------------------------------
output:
15

PHP has 4 different variable scopes such as
  • local
  • global
  • static
  • parameter
Local variables are declared within a PHP function and can only access within that funcion.
Global variable is declared outside of any function and It can be accessed from any part of script.
Static variable are used when you do not want to delete local variable by using static keyword before the first declaration of the variable.
Parameter is a local variable whose value is passed to the function by the calling code.


No comments:

Post a Comment