Php 7 STATEMENTS IF ELSE ELSEIF LOGIC CODE EXAMPLES LESSON 17

Php 7 Statements include “if”, “else” and “elseif”. We logically consider the situation and use these Php 7 statements accordingly. Programming Php 7 statements is like “reasoning” something out. In this Php 7 tutorial we are going to use money and relationships to explain this process.

File for this lesson. 

Php 7 IF Statement

A Php 7 “IF” Statement sets a condition. We are asking “if” something exists. Lets use money as an example to answer the question, “Do you have a $100?”. Right away you think in your mind if you have that in cash in your wallet or do you have to go to the ATM, right? And, IF you had a 100 bucks how would you reply to my question? We will answer that in the “ELSE” extended Statement.

Only cool people share!

So, to set an “IF” statement we do something like this.  The “IF” Statement is followed by parentheses then parentheses is followed by curly brackets.  The curly brackets are going to hold the answer for the IF statement. We then echo out the answer inside the curly brackets. This is the setup leading up to an “IF Statement” in Php7.

  1. Consider the situation.
  2. Reason it out by asking yourself an “IF” question.
  3. Plan your answer
if($situation){echo 'answer';}

If Statement Example

Here is an if statement example. After looking in your wallet you have $25. So the answer would be, “No I Don’t have $100”. Notice we are using the “Less Than Comparison Operator” to answer this question..

/*
THIS SETS UP AN IF STATEMENT.
THE QUESTION WE WILL ANSWER IS
"DO YOU HAVE $100?"
Consider the situation.
Reason it out by asking yourself an "IF" question.
Plan your answer if you have $25
*/
//CALCULATE THE CASH IN YOUR WALLET
$mycash=25;
//LETS GO THROUGH THE LOGIC
if($mycash < 100){
echo '<p>No I do not have $100.</p>';
};

 

Php 7 Else Statement

The Php 7 else statement will create additional replies to the IF statement.  It looks for something that is false. For example, your girlfriend asks you if you have $100. You do have $100 but you don’t want to give her all your cash. You reason out that you will give her $20. So your programming logic would look like this.

/*
THIS SETS UP AN IF STATEMENT.
THE QUESTION WE WILL ANSWER IS
"DO YOU HAVE $100?"
GIRLFRIEND IS ASKING WITH LOVEY
DOVEY IN HER EYES. YOU DO HAVE
$100 BUT YOU DON'T WANT TO GIVE HER
ALL YOUR CASH
SO WHAT DO YOU TELL HER?
*/
$mycash=100;
if($mycash < 100){
echo "<p>No I don't have $100.</p>";
}else{
echo "<p>I have $20, here baby its all yours.</p>";
};

 

Php 7 Else If Statement

The Php 7 Else If Statement will consider and set up additional answers. You can use the else if statement in Php7 as 2 words or just one word like this, elseif. So, the question, “Do you have $100?”. Depends a lot on who is asking that question, right? IF that person was your grandma you just might give it all to her. So in this programming logic we are going to add another variable. Therefore that Php variable is going to be set to $who. So if grandma asks for $100 we will give it to her. If girlfriend asks for $100, we will give her $10. If anyone else asks we will tell them to get a job.

/*
THIS SETS UP AN IF STATEMENT.
THE QUESTION WE WILL ANSWER IS
"DO YOU HAVE $100?"
IRONICALLY THAT DEPENDS ON
WHO IS ASKING THE QUESTION
*/
$mycash=100;
$who="Grandma";
if($mycash < 100){
echo "<p>No I don't have $100.</p>";
}elseif($mycash < 100 && $who=="Grandma"){
echo "<p>Here's $$mycash Grammy.</p>";
}elseif($mycash < 100 && $who=="Girlfriend"){
echo "Here is $20 baby its all yours!";
}else{
echo "Get a job!";
};

Conclusion on Php 7 Statements

With Php 7 statement the important thing is to understand a situation before you program it. Else if statements can go on for a very long time therefore we will be learning about switch statements in our next lesson.

Follow Up Video On Php Statements

 

Php 7 STATEMENTS IF ELSE ELSEIF LOGIC CODE EXAMPLES LESSON 17 was last modified: October 29th, 2021 by Maximus Mccullough
Php 7 STATEMENTS IF ELSE ELSEIF LOGIC CODE EXAMPLES

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.