PHP – If / Else statement

Hits: 0

PHP – If / Else statement

 

The if/else condition is the most simple form of conditional processing and you will find it in any programming language. So if you already know some other programming language this will be an easy read. If not, just read this tutorial carefully because it will be useful when learning any other programming language.

Did you paid attention to what I’ve just said? The upper paragraph contains a conditional statement. Let’s translate that to pseudo code. By the way, pseudo code is a simplified way of writing natural language code without taking into consideration the programming specific syntax.

PHP

if 	you already know some other programming language 
	print 	This will be an easy read

else 
	print 	Just read this carefully

Cool isn’t it? PHP have the following conditional statements when it comes to performing different actions depending if a condition is true or false

  • if – do something if the condition is evaluated to true
  • if / else – do something if the condition is evaluated to true and something else if not
  • if / elseif / else – do something different depending on different conditions
  • switch – executes a different piece of code depending on the value of a variable (or expression). We will learn more about switch statements in a future tutorial.
  • while – repeats a block of code as long as the condition is met
  • do / while – executes a block of code once, and then repeats it as long as the condition is met
  • for – executes a block of code a given number of times
  • foreach – executes a block of code for each element of an array

 

It worth to mention that while, for and their derived are also called loops, because they execute a piece of code while the condition is met.

PHP – The IF statement

Great, now let’s be more PHP specific and translate the above example to valid if/else statement syntax.

PHP

$know_js = false;

if($know_js == true) {
		echo "I know Javascript. This read will be easy";
} else {
		echo "I am a complete beginner. I will read this carefully";
}

IF syntax

By looking at the above example we can say that this is the if statement syntax

PHP

if (condition) {
    // execute this following code if condition is true
}

Here are some examples of simple conditions that can be evaluated to true or false inside the if statement

PHP

if ( $x > 1 )
if ( $x != 5 )
if ( $a == "string" )
if ( $a !== false )
if ( $x > 2 and $x < 8  )

You can read more about the comparison and logical operators in the PHP comparison operators tutorial.

 

IF / ELSE syntax

Like I said, the if / else statement executes some code if a condition is true and another code if that condition is false.

PHP

if (condition) {
    // execute this following code if condition is true
} else {
    // execute this following code if condition is false
}

IF / ELSEIF / ELSE Syntax

The if / elseif / else leaves the door open for the developer to take into consideration more then two conditions.

PHP

if (condition 1) {
    // execute this following code if condition is true
} elseif (condition 2) {
    // execute this following code if condition 2 is true
} else {
    // execute this following code if condition 1 and condition 2 are both evaluated to false
}

PHP IF / ELSE shorthand using ternary operators

The IF / ELSE statements are easy to use but they can they can sometimes become be too long for a simple thing to code. Here is where the ternary operators comes in by using “(condition) ? true : false” expressions.

Here is a simple example to look at:

PHP


$x = 5;
$a = ($x > 4 ? true : false); // returns true

The advantages of ternary logic is obvious: You improve readability while your code becomes simpler, shorter and easier to maintain.

Inline echo using IF / ELSE shorthand ternary operators

PHP

$level = 2;
echo 'May the force be with you '.($level >  4 ? "my young padawan" : "master Jedi" ).'. How are you today?';

// this will print: "May the force be with you master Jedi. How are you today?"

Multiple inline IF / ELSE shorthand expressions

Let’s take the previous example and complicate it a little bit

PHP

$level = 10;
echo 'May the force be with you '.($level >  4 ? "my young padawan" : ( $level != 10 ? "master Jedi" : "master Yoda" ) ).'. How are you today?';

// this will print: "May the force be with you master Yoda. How are you today?"

This can sometimes get quite complicated if you include more and more conditions. So just stop and think that the shorthand if / else has the purpose to simplify and make things easier. In this case, you will have to find the balance between the short and the standard form.

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.  

Google –> SETScholars