Key Concepts

Review core concepts you need to learn to master this subject

PHP Strings

echo "Hello 123"; // prints Hello 123

In PHP, a string is a sequence of characters surrounded by double quotation marks. It can be as long as you want and contain any letters, numbers, symbols, and spaces.

PHP String Escape Sequences

echo "Hello 123"; // prints Hello 123

In PHP, sometimes special characters must be escaped in order to include them in a string. Escape sequences start with a backslash character (\).

There are a variety of escape sequences that can be used to accomplish different tasks. For example, to include a 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

echo "Hello 123"; // prints Hello 123

In PHP, if you want to join two strings together, you need to use the . operator.

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.

PHP Variables

echo "Hello 123"; // prints Hello 123

In PHP, variables are assigned values with the assignment operator (=).

Variable names can contain numbers, letters, and underscores (_). A sigil ($) must always precede a 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

echo "Hello 123"; // prints Hello 123

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.

Reassignment of PHP Variables

echo "Hello 123"; // prints Hello 123

In PHP, variables are assigned values with the assignment operator (=). The same variable can later be reassigned a new value using the same operator.

This process is known as reassignment.

PHP Reference Assignment Operator

echo "Hello 123"; // prints Hello 123

In PHP, the reference assignment operator (=&) is used to create a new variable as an alias to an existing spot in memory.

In other words, the reference assignment operator (=&) creates two variable names which point to the same value. So, changes to one variable will affect the other, without having to copy the existing data.

PHP Evaluation Order during Assignment

echo "Hello 123"; // prints Hello 123

In PHP, when an assignment takes place, operations to the right of the assignment operator (=) will be evaluated to a single value first. The result of these operations will then be assigned to the variable.

Appending a String in PHP

echo "Hello 123"; // prints Hello 123

In PHP, there is a shortcut for appending a new string to the end of another string. This can be easily done with the string 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.

Integer Values in PHP

echo "Hello 123"; // prints Hello 123

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

echo "Hello 123"; // prints Hello 123

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

echo "Hello 123"; // prints Hello 123

PHP supports arithmetic operators for addition (+), subtraction (-), multiplication (*), and division (/).

PHP operators will return integers whenever the result of an operation evaluates to a whole number. If the result evaluates to a fraction or decimal, then it will return a floating point number.

The Modulo Operator

echo "Hello 123"; // prints Hello 123

PHP supports a modulo operator (%). The modulo operator returns the remainder of the left operand divided by the right operand. Operands of a modulo operation are converted to integers prior to performing the operation. The operation returns an integer with the same sign as the dividend.

Exponentiation Operator in PHP

echo "Hello 123"; // prints Hello 123

PHP supports an arithmetic operator for exponentiation (**).

This operator gives the result of raising the value on the left to the power of the value on the right.

Chevron Left Icon
PHP Strings and Variables
Lesson 1 of 2
Chevron Right Icon
  1. 1
    illustration of string In everyday conversation, we use the word data to refer to any sort of informati…
  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://s3.amazona…
  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