-
Notifications
You must be signed in to change notification settings - Fork 2
PHP Variables
Variables are containers for storing data such as strings
, integers
, boolean
values, array
and objects.
PHP follows certain rules for variable declarations such as:
-
The variable must begin with a dollar sign ($)
Example:
<?php $var = 5; ?>
-
The variable name can contain characters such as A-Z, a-z, 0-9, _ and ASCII characters from 127-255.
Example:
<?php $var = 5; //Valid $var_1 = "Foo"; //Valid $_var2 = 'Bar'; //Valid $var.3 = 'Baz'; //Invalid ?>
-
The variable name can begin with underscore (_).
Example:
<?php $_var2 = 'Bar'; //Valid ?>
-
The variable name must not begin with a number 0-9.
Example:
<?php $9var3 = 'Baz'; //Invalid ?>
-
The variable name is case sensitive.
Example:
<?php
$var = 5; //Valid
$VAR = "Foo"; //Valid
echo $var; //Output 5
echo "<br>";
echo $VAR; //Output Foo
?>
PHP is a loosely typed language, hence we don't need to declare the data type of a variable when declaring the variable. Unlike Java or C.
<?php
$var = 5;
$var2 = 4;
$sum = $var+$var2;
echo $sum; //Output 9
echo "<br>";
echo $var+$var2; //Output 9
?>
The variables can also be assigned by referencing. This allows two variables to refer to the same content. The &
operator is placed before the variable that is to be referenced.
Example :
<?php
$var1 = "foo";
$var2 = "bar";
myTest($var1, $var2);
echo $var1; //Output foo
echo $var2; //Output BAR
function myTest($var1, &$var2){
$var1 = "FOO";
$var2 = "BAR";
}
?>
To have variable names set dynamically we use the variable variables. This can be particularly useful when there is a need to create multiple variables.
Example :
<?php
$var = 'Tom';
echo $var; //Output Tom
$$var = 'Cat'; //The value of $$var is the value of $var. So $$var and $Tom give the same output.
echo $$var; //Output Cat
echo $Tom; //Output Cat
?>
Scope of variable refers to the places from where a variable is accessible.
- Global scope is for the variables that are declared outside a function. These variables can be accessed from anywhere but not within a function.
- Local scope is for the variables declared within a function that can not be accessed from anywhere outside the function.
Example:
<?php
$global = "Hello";
function Test(){
$local = "World";
echo $global; //Error
echo $local; //Output World
}
Test();
echo $global; //Output Hello
echo $local; //Error
?>
To access global variables inside a function:
<?php
$global = "Hello";
function Test(){
global $global;
$local = "World";
echo $global; //Output Hello
echo $local; //Output World
}
Test();
echo $global; //Output Hello
echo $local; //Error
?>
Everytime a function is created all of its local variables are deleted. To retain the variable's last value we declare it static
.
Example :
<?php
function WithStatic(){
static $var = 0;
echo $var;
$var++;
}
WithStatic(); //Output 0
WithStatic(); //Output 1
WithStatic(); //Output 2
function WithoutStatic(){
$var = 0;
echo $var;
$var++;
}
WithoutStatic(); //Output 0
WithoutStatic(); //Output 0
WithoutStatic(); //Output 0
?>
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links