Backend and Infrastructure

The Wonderful PHP if Statement

Sprout Video

When you reference a key on an array that doesn't exist, PHP will complain. Instead, let's code defensively. In other words, if we know that it's possible that the age key might be missing, we should check for it and only print the age if it's there. We need to add some logic. To do this, we'll finally meet the wonderful and super-common if statement. Like foreach, it's a language construct, and is one of those things that uses curly braces to surround a block of code. Where foreach accepts an array and executes the code between its curly braces one time for each item, if accepts a Boolean value — in other words: true or false. If what you pass it is true, it executes the code between its curly braces. In this lesson we'll use the if and else statements, and see how to combine our conditions, along with a variety of operators, to get exactly what we need.