1. 18
    Alternate PHP if statement syntax for view files
    3m 36s

Alternate PHP if statement syntax for view files

Mark Shust
InstructorMark Shust
Share this video with your friends

Social Share Links

Send Tweet

Writing if statements alongside your HTML can be confusing to process. Because of this, PHP supplies an alternate if/else syntax for outputting PHP alongside HTML.

Instructor: [0:00] We can also write these if-else statements using an alternate syntax. Currently, this is written in one giant block of PHP code, but usually, when you are intermingling PHP and HTML, you will instead want to use inline PHP blocks with an alternate if-else syntax.

[0:20] Let's remove all of this code and we will use inline PHP instead. Within this tag we will have our if statement, followed by the condition of checking if hasPost is true. Rather than using a bracket, we will use a colon.

[0:38] To end this control structure, we will open up another PHP tag and within it, we will type end-if.

[0:47] Since we are closing up this PHP tag directly after it, we do not need a semicolon here. Since our PHP tags are closed off, we don't need an echo statement or anything within this if-statement. We can just type our posts exist message.

[1:05] Since this is outside the normal scope of PHP statements, the HTML will just be outputted. When we save and refresh the page, we will see that our code still executes properly. The else statement works in very much the same way.

[1:21] By opening up another PHP tag, we can type else followed by a semicolon. Then we can execute any fallback code within this block. This will be where our there are no posts message goes. If we update numPost to zero and refresh the page, we can see that this else condition still executes.

[1:49] This alternate syntax is not used within larger blocks of PHP because it is hard to know when conditional statements start or stop when they don't have brackets, especially because you can nest multiple if-statements together. Let's bring back our there are exactly three posts message.

[2:09] We can add this check within the hasPosts conditional logic. Rather than directly outputting posts exist, let's replace this with another PHP tag. We will add another if-statement and this will check if numPosts equals three. If so, we will output there are exactly three posts.

[2:37] If not, we can run an else statement, and then output our posts exist message. Then we can close up this if-statement with end-if.

[2:49] If our number of posts has three and we refresh the page, our there are exactly three posts message shows because hasPost is true, and number of posts equals three.

[3:03] If this is 10, or some other figure besides three and greater than zero, when we refresh the page, this post exist message will get caught because numPost does not equal three. Note that this has the exact same output as the previous lesson that used an else-if statement.

[3:21] Though, this uses completely different logic to carry this out. This proves that there is almost always more than one way to do something. There are usually different approaches that can be used to tackle the same task.