Key Concepts

Review core concepts you need to learn to master this subject

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.

Arrow Chevron Left Icon
PHP Strings and Variables
Lesson 1 of 2
Arrow Chevron Right Icon
  1. 1
    illustration of string In everyday conversation, we use the word data to refer to any sort of information. This info…
  2. 2
    We 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…
  3. 3
    It 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…
  4. 4
    Let’s say I have a really long string in my program, and I’m going to need to use it multiple times. Do I have to type the string out every time I need to use it? The answer is “no”. Variables are …
  5. 5
    Let’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…
  6. 6
    Once 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 …
  7. 7
    In 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…
  8. 8
    The 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…
  9. 9
    We 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…
  10. 10
    When 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. …
  11. 11
    Awesome work! We’ve covered a lot of material in this lesson, so let’s review: - Strings are collections of text that the computer treats as a single piece of data. - A string can be any length an…
  1. 1
    Numbers and arithmetic operations are a fundamental part of programming. In this lesson, we’ll learn how to use and manipulate numbers in PHP. PHP has two number data types. The integer data ty…
  2. 2
    PHP provides several operators we can use on numbers. Let’s start with two that are likely familiar: the addition (+) and subtraction (-) operators: echo 5 + 1; // Prints: 6 echo 6.6 + 1.2; // Pri…
  3. 3
    We can use number operators on variables that hold number values: $tadpole_age = 7; $lily_age = 6; $age_difference = $tadpole_age - $lily_age; echo $age_difference; // Prints 1 Let’s look at anot…
  4. 4
    PHP also gives us operators for performing multiplication (*) and division (/). echo 2 * 3; // Prints: 6 echo -21 / 7; // Prints: -3 Like with addition and subtraction, when we perform multiplica…
  5. 5
    PHP give us an operator for raising a number to the power of another number: the exponentiation operator (*). For example, we can square a number by raising it to the power of 2: echo 4 * 2; //…
  6. 6
    PHP also provides an operator that might be less familiar: modulo (%). The modulo operator returns the remainder after the left operand is divided by the right operand. echo 7 % 3; // Prints: 1…
  7. 7
    We can chain multiple operations together to get a single result: echo 2 + 3 + 4 + 5 - 1.1; // Prints: 12.9 echo 2 * 9 / 6; // Prints: 3 You might have learned about operations having an order of …
  8. 8
    One common task when manipulating number variables is to reassign them to their old value with some operation performed on it. $savings = 800; $bike_cost = 75; $savings = $savings - $bike_cost; e…
  9. 9
    Great job! In this lesson, we learned all about using numbers in PHP. Let’s review what we covered: - PHP has two number data types: integers and floating point numbers - We can use arithmetic op…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo