AMSOL e-Tutor

AMSOL e-Tutor helps you learn web programming better with examples. It also gives sample codes to implement.

Variables


Variables are most important elemenants of any program, these are containers for storing information, it can be number, text or alpha-numeric. In PHP every variable starts with $ symbol. After $ only alphbets are allowed and after 1st alphabet numbers can be used, variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).

$x and $X are 2 different variables.

example:
$x = 5;
$y = 10;
$z = $x * $y;

Output Variables: Variables are manipulated within program and where ever output display is required, echo statement comes in to place. echo $y;

semi colon ; sign has special meaning in PHP, it is equivalent to full stop in grammar. It means statement has completed. We can write program in continuous mode like $x = 5; $y = 10; $z = $x * $y;

For better readability we can write code as

$x = 5;
$y = 10;
$z = $x * $y;
Variables Scope

PHP has three different variable scopes:

local
global
static

global: All declared variables have global scope, unless declared within a function.

local: A variable declared within a function has a LOCAL SCOPE and can only be accessed within the function.

static: When a function is executed, all of its variables are deleted. if we want a local variable NOT to be deleted we use static variable.

To do this, use the static keyword when you first declare the variable:

static $x = 0;
Variable Data Types

PHP supports the following data types:

String Integer Float (floating point numbers - also called double) Boolean Array Object NULL Resource