PHP Freelancer

Freelance PHP Developer

PHP Tutorial – Basic PHP variables

Posted on | March 18, 2009 | No Comments

Hello Friends ,

PHP variables are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive, so $a is different from $A.

A variable name should start with a letter or underscore, followed by any charactor. A variable name starts with number is not a valid variable.

Example :

<?php
$a          = “Hi”;     // valid
$a_b-987 = “Hello”;     // valid
$987abc      = “Wrong”; // invalid bcaz variable name starts with number
?>

Variable Scope :

The scope of a variable is the context within which it is defined. Basically you can not access a variable which is defined in different scope.

Look at below example.

<?php
$a = 1; // $a is a global variable

function hi()
{
echo $a; // try to print $a, but $a is not defined here
}

hi();
?>

If you want to use $a variable in hi() function than you have to use ‘global’ keyword. Look at below example.

<?php
$a = 1;
$b = 2;

function add()
{
global $a, $b; // now $a and $b are available in add()
$a = $a + $b;
}

add();
echo $a;
?>

PHP Superglobals variables :

Superglobals are variables that available anywhere in the program code. They are :

1 . $_SERVER
2 . $_GET
3 . $_POST
4 . $_COOKIE
5 . $_FILES
6 . $_ENV
7 . $_REQUEST
8 . $GLOBALS


Cheers !!

      
Plugin by: PHP Freelancer

Comments

Leave a Reply





Freelance PHP Developer