Learn PHP_ Introduction_ PHP Strings, Numbers, and Variables Cheatsheet _ Codecademy
Learn PHP_ Introduction_ PHP Strings, Numbers, and Variables Cheatsheet _ Codecademy
In PHP, variables can be parsed within strings specified with $my_var = "cat";
double quotes ( " ).
This means that within the string, the computer will replace an
occurence of a variable with that variable’s value. echo "There is one $my_var on the mat";
When additional valid identifier characters (ie. characters that
could be included in a variable name) are intended to appear
/* If there were three cats, then you can
adjacent to the variable’s value, the variable name can be
wrapped in curly braces {} , thus avoiding confusion as to the type: */
variable’s name. echo "There are three {$my_var}s on the 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 */
In PHP, variables are assigned values with the assignment $var1 = "Bob";
operator ( = ). The same variable can later be reassigned a new
echo $var1;
value using the same operator.
This process is known as reassignment. // var1 holds the value "Bob"
$var1 = "John";
echo $var1;
// var1 now holds the value "John"
In PHP, if you want to join two strings together, you need to use echo "Hello,"." welcome to Codecademy!";
the . operator.
// prints - Hello, welcome to Codecademy!
This process is called concatenation. Put the . operator
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.
Appending a String in PHP
In PHP, there is a shortcut for appending a new string to the $str = "Hello, ";
end of another string. This can be easily done with the string
$str .= "World!";
concatenation assignment operator ( .= ).
This operator will append the value on its right to the value on
its left and then reassign the result to the variable on its left. echo $str;
// Output: Hello, World!
PHP Strings
In PHP, a string is a sequence of characters surrounded by echo "Hello 123"; // prints Hello 123
double quotation marks. It can be as long as you want and
contain any letters, numbers, symbols, and spaces.
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 new
$copy = $original;
variable name to it.
Changes to the original variable will not affect the copy and $orginal = "Iced Tea";
changes to the copy will not affect the original. These variables echo $copy; // "Ice T";
are entirely separate entities.
In PHP, sometimes special characters must be escaped in order echo "Hello, World!\nThis is a String!";
to include them in a string. Escape sequences start with a
/* prints-
backslash character ( \ ).
There are a variety of escape sequences that can be used to Hello, World!
accomplish different tasks. For example, to include a new line This is a String!
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.
PHP Evaluation Order during Assignment
echo $var1;
// Output: 8
PHP Variables
In PHP, variables are assigned values with the assignment $my_variable = "Hello";
operator ( = ).
Variable names can contain numbers, letters, and underscores
( _ ). A sigil ( $ ) must always precede a variable name. They $another_cool_variable = 25;
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.
$b = 7 / 3;
// The variable $b will hold a floating point
value, since the operation evaluates to a
decimal number.
The Modulo Operator
$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.
Print Share