Learn PHP Variables
Learn about PHP variables and the string and number data types.
StartKey Concepts
Review core concepts you need to learn to master this subject
Parsing Variables within PHP Strings
Reassignment of PHP Variables
Concatenating Strings in PHP
Appending a String in PHP
PHP Strings
PHP copying variables
PHP String Escape Sequences
PHP Evaluation Order during Assignment
Parsing Variables within PHP Strings
Parsing Variables within PHP Strings
$my_var = "cat";
echo "There is one $my_var on the mat";
/* If there were three cats, then you can type: */
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 can be parsed within strings specified with double quotes ("
).
This means that within the string, the computer will replace an occurence of a variable with that variable’s value.
When additional valid identifier characters (ie. characters that could be included in a variable name) are intended to appear adjacent to the variable’s value, the variable name can be wrapped in curly braces {}
, thus avoiding confusion as to the variable’s name.
- 2We use quotation marks to indicate the start and end of a string. The quotation marks tell the computer that we want everything inside them to be treated as a single piece of data. But how do we in…
- 3It can be useful to combine two strings together. This process is called string concatenation, and we can use the concatenation operator (.) to do this. An operator is a character that perfor…
- 5Let’s look at an example of creating a variable: $my_name = “Aisle Nevertell”; In the code above, we’re actually doing two things with a single statement: we’re declaring a new variable by givi…
- 6Once we’ve declared a variable and assigned a value to it, we can use it as many times as we want. We refer to a variable by using the dollar sign followed by the variable’s name. $favorite_food …
- 7In the last exercise, we saw how concatenating a number of strings and string variables got annoying. There’s an easier way! PHP strings allow us to place variables directly into double quoted st…
- 8The word variable comes from the latin variāre which means “to make changeable.” This is an apt name because the value assigned to a variable can change. ![gif of reassignment](https://content.co…
- 9We can assign and reassign variables to the values that result from operations: $full_name = “Aisle” . “ Nevertell”; echo $full_name; // Prints: Aisle Nevertell During assignment, the computer wi…
- 10When we create a variable assigned to another variable, the computer finds a new space in memory which it associates with the left operand, and it stores a copy of the right operand’s value there. …
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory