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.

PHP Strings and Variables
Lesson 1 of 2
  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](…
  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…

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