Key Concepts

Review core concepts you need to learn to master this subject

PHP else statement

$condition = FALSE; if ($condition) { // This code block will not execute } else { // This code block will execute }

A PHP else statement can follow an if block. If the condition of the if does not evaluate to TRUE, the code block following else will be executed.

PHP comparison operators

$condition = FALSE; if ($condition) { // This code block will not execute } else { // This code block will execute }

PHP comparison operators are used to compare two values and return TRUE or FALSE depending on the validity of the comparison. Comparison operators include:

  • identical (===)
  • not identical (!==)
  • greater than (>)
  • less than (<)
  • greater than or equal (>=)
  • less than or equal (<=)

PHP If Statements

$condition = FALSE; if ($condition) { // This code block will not execute } else { // This code block will execute }

PHP if statements evaluate a boolean value or expression and execute the provided code block if the expression evaluates to TRUE.

PHP switch statement

$condition = FALSE; if ($condition) { // This code block will not execute } else { // This code block will execute }

PHP switch statements provide a clear syntax for a series of comparisons in which a value or expression is compared to many possible matches and code blocks are executed based on the matching case.

In PHP, once a matched case is encountered, the code blocks of all subsequent cases (regardless of match) will be executed until a return, break, or the end of the statement is reached. This is known as fall through.

PHP readline()

$condition = FALSE; if ($condition) { // This code block will not execute } else { // This code block will execute }

The PHP built-in readline() function takes a string with which to prompt the user. It waits for the user to enter text into the terminal and returns that value as a string.

PHP elseif statements

$condition = FALSE; if ($condition) { // This code block will not execute } else { // This code block will execute }

PHP elseif statements must be paired with an if statement, but many elseifs can be chained from a single if.

elseifs provide an additional condition to check (and corresponding code to execute) if the conditional statements of the if block and any preceding elseifs are not met.

PHP Truthy and Falsy

$condition = FALSE; if ($condition) { // This code block will not execute } else { // This code block will execute }

PHP values within a condition will always be evaluated to TRUE or FALSE. Values that will evaluate to TRUE are known as truthy and values that evaluate to FALSE are known as falsy.

Falsy values include:

  • false
  • 0
  • empty strings
  • null
  • undefined
  • NaN.

All other values are truthy.

PHP Boolean Values

$condition = FALSE; if ($condition) { // This code block will not execute } else { // This code block will execute }

PHP Boolean values are either TRUE or FALSE, which are the only members of the boolean type

PHP ternary operator

$condition = FALSE; if ($condition) { // This code block will not execute } else { // This code block will execute }

In PHP, the ternary operator allows for a compact syntax in the case of binary (if/else) decisions. It evaluates a single condition and executes one expression and returns its value if the condition is met and the second expression otherwise.

The syntax for the ternary operator looks like the following:

condition ? expression1 : expression2

PHP Nested Conditionals

$condition = FALSE; if ($condition) { // This code block will not execute } else { // This code block will execute }

In PHP, nested conditional statements deepen the complexity of our programs’ decision-making capabilities. They allow us to create programs where each decision made sends our program on a different route where it might encounter additional decisions.

PHP Logical Operators

$condition = FALSE; if ($condition) { // This code block will not execute } else { // This code block will execute }

In PHP, expressions that use logical operators evaluate to boolean values. Logical operators include:

  • or (||)
  • and (&&)
  • exclusive or (xor)
  • not (!)

PHP && Operator

$condition = FALSE; if ($condition) { // This code block will not execute } else { // This code block will execute }

The logical operator && returns:

  • TRUE only if both of its operands evaluate to true.
  • FALSE if either or both of its operands evaluate to false.

PHP ! (not) Operator

$condition = FALSE; if ($condition) { // This code block will not execute } else { // This code block will execute }

In PHP, the not operator (!) is used to invert a Boolean value or expression.

PHP Operator Precedence

$condition = FALSE; if ($condition) { // This code block will not execute } else { // This code block will execute }

Each operator in PHP holds a different operator precedence.

We can avoid operator precedence confusion by using parentheses for force the evaluation we want.

PHP Xor Operator

$condition = FALSE; if ($condition) { // This code block will not execute } else { // This code block will execute }

In PHP, the logical operator xor stands for exclusive or.

It takes two different boolean values or expressions as its operands and returns a single boolean value.

xor evaluates to TRUE only if either its left operand or its right operand evaluate to TRUE, but not both.

Logical Operators - Alternate Syntax

$condition = FALSE; if ($condition) { // This code block will not execute } else { // This code block will execute }

PHP provides an alternate syntax for the || operator — the or operator.

It also provides an alternate syntax for && operator — the and operator.

These operators have the advantage of making our code more human readable.

Multi-File Programs: include

$condition = FALSE; if ($condition) { // This code block will not execute } else { // This code block will execute }

A way to improve our code and separate concerns is with modularity, separating a program into distinct, manageable chunks where each provides a piece of the overall functionality. Instead of having an entire program located in a single file, code is organized into separate files.

In PHP, files can be included in another file with the keyword include. An include statement is followed by a string with a path to the file to be included. The code from the file will be executed.

Chevron Left Icon
Booleans and Comparison Operators
Lesson 1 of 2
Chevron Right Icon
  1. 1
    Have you ever noticed that hyperlinks change color after you click them? If the link has been clicked, then the web browser renders it in purple, rather than blue. The programming concept that …
  2. 2
    We’re going to learn about a specific type of conditional called an if statement. An if statement follows this basic structure: if (/some condition/) { // Do something… } The parentheses ho…
  3. 3
    The condition, or expression, in an if statement can hold a boolean value—like TRUE or FALSE, a variable assigned to one of those values, or an expression that evaluates to TRUE or FALSE. Just …
  4. 4
    In this exercise we’re going to learn a few more comparison operators and see how we can use them to compare more than just number values. The identical operator (===) will return TRUE if the lef…
  5. 5
    So far, we’ve been writing conditionals that can only handle one condition. If that condition is met, we do one thing, otherwise we do something else. This only allows us one or two courses…
  6. 6
    We often want to compare a value, expression, or variable against many different possible values and run different code depending on which it matches. We can use a series of if/elseif statements wh…
  7. 7
    In the previous exercise, we taught you to use the keyword break after the block for each case. Without the keyword break, not only will the first matching case’s block run, but so will all the co…
  8. 8
    PHP offers a short-hand syntax to conditionally return a value. Before learning it, let’s consider some example code: $isClicked = FALSE; if ( $isClicked ) { $link_color = “purple”; } else { $…
  9. 9
    So far in our conditionals, we’ve been dealing with expressions that would evaluate to boolean values in any context. In practice, any value or expression in the condition will be converted
  10. 10
    The outcomes of programs we’ve been writing so far have been predetermined. Unless we manually change our code, it will produce the same results each time it’s run. But this isn’t very realistic. P…
  11. 11
    Woah! We covered a lot in this lesson. Good work. Let’s review what we learned: + Conditionals make it possible for programs to decide how to react to a wide variety of situations. + if statements …
  1. 1
    In the previous lesson, we explored the foundations of making decisions in programming: booleans and conditionals. In this lesson, we’ll deepen the complexity of our programs’ decision-making capab…
  2. 2
    Expressions that use logical operators evaluate to boolean values. The logical operator || takes two different boolean values or expressions as its operands and returns a single boolean value. …
  3. 3
    Often, we’ll encounter situations where we have more than one condition we need satisfied in order to take an action. The logical && operator returns TRUE only if both of its operands evaluat…
  4. 4
    The logical not operator (!) takes only a right operand. It reverses the boolean value of its operand. !TRUE; // Evaluates to: FALSE !FALSE; // Evaluates to: TRUE The not operator has very …
  5. 5
    The logical operator xor stands for exclusive or. It takes two different boolean values or expressions as its operands and returns a single boolean value. Unlike regular or which evaluates to TRU…
  6. 6
    An alternate syntax for logical || operator is the or operator, and an alternate syntax for logical && operator is the and operator. These operators have the advantage of making our code more human…
  7. 7
    Separation of concerns is the programming design principle of organizing code into distinct sections each handling a specific task. It enables us to quickly navigate our code and know where to look…
  8. 8
    Great job! You’ve learned the tools needed to craft programs with powerful decision making capabilities. Let’s review what we covered: + By nesting conditionals within one another, we can create br…

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