Learn PHP - Learn PHP Variables Cheatsheet - Codecademy
Learn PHP - Learn PHP Variables Cheatsheet - Codecademy
$var1 = "John";
echo $var1;
// var1 now holds the value "John"
echo $var1;
// Output: 8
PHP Variables
In PHP, variables are assigned values with the assignment
operator ( = ). $my_variable = "Hello";
Variable names can contain numbers, letters, and
underscores ( _ ). A sigil ( $ ) must always precede a variable $another_cool_variable = 25;
name. They cannot start with a number and they cannot have
spaces or any special characters.
The convention in PHP is to use snake case for variable
naming; this means that lowercase words are delimited with
an underscore character ( _ ). Variable names are case-
sensitive.
PHP Reference Assignment Operator
In PHP, the reference assignment operator ( =& ) is used to
create a new variable as an alias to an existing spot in $var1 = 5;
memory. $var2 =& $var1;
In other words, the reference assignment operator ( =& )
creates two variable names which point to the same value. So, $var1 = 6;
changes to one variable will affect the other, without having to
echo $var2;
copy the existing data.
// Output: 6
$b = 7 / 3;
// The variable $b will hold a floating point
value, since the operation evaluates to
a decimal number.
The Modulo Operator
PHP supports a modulo operator ( % ). The modulo operator
returns the remainder of the left operand divided by the right $a = 9 % 2.3;
operand. Operands of a modulo operation are converted to //2.3 is converted to the integer, 2. The
integers prior to performing the operation. The operation remainder of 9 % 2 is 1. So the variable $a
returns an integer with the same sign as the dividend. will hold the integer value 1.
$b = -19 % 4;
//The remainder of this operation is -3. So
the variable $b will hold the integer value
-3.
$c = 20 % 2;
//The remainder of this operation is 0. So
the variable $c will hold the integer value
0.