Conditionals and Logic in PHP
Learn about the boolean data type, conditionals, `switch` statements, the ternary operator, comparison operators, and logical operators!
StartKey Concepts
Review core concepts you need to learn to master this subject
PHP else statement
PHP comparison operators
PHP If Statements
PHP switch statement
PHP readline()
PHP elseif statements
PHP Truthy and Falsy
PHP Boolean Values
PHP else statement
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.
- 1Have 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 …
- 2We’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…
- 3The 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 …
- 4In 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…
- 5So 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…
- 6We 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…
- 7In 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…
- 8PHP 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 { $…
- 9So 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…
- 10The 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…
What you'll create
Portfolio projects that showcase your new skills
Magic 8 Ball
In this lesson, you've learned about conditionals—a fundamental yet powerful part of programming. Conditionals are so powerful, in fact, that they can be used to predict the future. In this project, you'll be creating a function that can answer any "yes" or "no" question.
PHP Number Guessing
In this project, you'll create a number guessing game. Your program will generate a random number between 1 and 10. You'll run the game 10 times and tell the user some information about their guessing abilities.
Save the Farm!
_Hello there. It's been a harrowing few weeks. First your toenail issue, and now Great-Aunt Natasha's emu farm is in danger of being reposessed! So here you are---after a brief stopover at the Mayo clinic---in Uncle Boris's remote cabin in the heart of the Terror Woods. Family legend holds that a golden statue of immense value is hidden somewhere within these walls..._ Welcome to the world of [interactive fiction](https://en.wikipedia.org/wiki/Interactive_fiction). In this project you'll be building a text adventure game called **Save the Farm**!
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory