Learn PHP - Learn PHP Variables Cheatsheet - Codecademy

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Cheatsheets / Learn PHP

Learn PHP Variables


PHP Strings
In PHP, a string is a sequence of characters surrounded
by double quotation marks. It can be as long as you want echo "Hello 123"; // prints Hello 123
and contain any letters, numbers, symbols, and spaces.

PHP String Escape Sequences


In PHP, sometimes special characters must be escaped in
order to include them in a string. Escape sequences start echo "Hello, World!\nThis is a String!";
with a backslash character ( \ ). /* prints-
There are a variety of escape sequences that can be used Hello, World!
to accomplish different tasks. For example, to include a This is a String!
new line within a string, the sequence \n can be used.
*/
To include double quotation marks, the sequence \"
can be used. Similarly, to include single quotes, the
sequence \' can be used.

Concatenating Strings in PHP


In PHP, if you want to join two strings together, you need
to use the . operator. echo "Hello,"." welcome to Codecademy!";
This process is called concatenation. Put the . operator // prints - Hello, welcome to Codecademy!
between the two strings in order to join them.
Note that strings are joined as-is, without inserting a
whitespace character. So if you need to put spaces, you
need to incorporate the whitespace manually within the
string.

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 $another_cool_variable = 25;
variable 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.
Parsing Variables within PHP Strings
In PHP, variables can be parsed within strings specified
with double quotes ( " ). $my_var = "cat";
This means that within the string, the computer will
replace an occurence of a variable with that variable’s echo "There is one $my_var on the mat";
value.
When additional valid identifier characters (ie. characters
/* If there were three cats, then you can
that could be included in a variable name) are intended to
type: */
appear adjacent to the variable’s value, the variable name
echo "There are three {$my_var}s on the
can be wrapped in curly braces {} , thus avoiding
confusion as to the variable’s name. mat ";
/* The curly braces help to avoid
confusion between the variable name and
the letter s, so PHP does not consider the
variable name as my_vars */

Reassignment of PHP Variables


In PHP, variables are assigned values with the assignment
operator ( = ). The same variable can later be reassigned $var1 = "Bob";
a new value using the same operator. echo $var1;
This process is known as reassignment. // var1 holds the value "Bob"

$var1 = "John";
echo $var1;
// var1 now holds the value "John"

PHP copying variables


In PHP, one variable’s value can be assigned to another
variable. $original = "Ice T";
This creates a copy of that variable’s value and assigns the $copy = $original;
new variable name to it. $orginal = "Iced Tea";
Changes to the original variable will not affect the copy echo $copy; // "Ice T";
and changes to the copy will not affect the original. These
variables are entirely separate entities.

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
$var1 = 6;
value. So, changes to one variable will affect the other,
echo $var2;
without having to copy the existing data.
// Output: 6
PHP Evaluation Order during Assignment
In PHP, when an assignment takes place, operations to
the right of the assignment operator ( = ) will be $var1 = 5 + 6 / 2;
evaluated to a single value first. The result of these /* Here, the operations to the right of
operations will then be assigned to the variable. the assignment operator will be carried
out first. So first 6 will be divided by
2 (6 / 2 = 3). Then 3 will be added to
5 (5 + 3 = 8). Finally, the value 8 will
be assigned to $var1. */

echo $var1;
// Output: 8

Appending a String in PHP


In PHP, there is a shortcut for appending a new string to
the end of another string. This can be easily done with $str = "Hello, ";
the string concatenation assignment operator ( .= ). $str .= "World!";
This operator will append the value on its right to the
value on its left and then reassign the result to the
echo $str;
variable on its left.
// Output: Hello, World!

Integer Values in PHP


PHP supports integer values for numbers.
Integers are the set of all whole numbers, their negative
counterparts, and zero. In other words, an integer is a
number of the set ℤ = {…, -2, -1, 0, 1, 2, …}.

Floating Point Numbers in PHP


PHP supports floating-point (decimal) numbers. They can
be used to represent fractional quantities as well as
precise measurements. Some examples of floating point
numbers are 1.5 , 4.231 , 2.0 , etc.
Arithmetic Operators in PHP
PHP supports arithmetic operators for addition ( + ),
subtraction ( - ), multiplication ( * ), and division ( / ). $a = 6 / 3;
PHP operators will return integers whenever the result of // The variable $a will hold an integer
an operation evaluates to a whole number. If the result value, since the operation evaluates to
evaluates to a fraction or decimal, then it will return a a whole number.
floating point number.

$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 $a = 9 % 2.3;
divided by the right operand. Operands of a modulo //2.3 is converted to the integer, 2. The
operation are converted to integers prior to performing remainder of 9 % 2 is 1. So the variable
the operation. The operation returns an integer with the $a will hold the integer value 1.
same sign as the dividend.

$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.

Exponentiation Operator in PHP


PHP supports an arithmetic operator for exponentiation
( ** ). $n = 4;
This operator gives the result of raising the value on the echo 2 ** 4;
left to the power of the value on the right. // Output: 16

You might also like