Free cookie consent management tool by TermsFeed Generator PHP Conditionals | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Conditionals

PHP Conditionals

Last Updated on Mar 21, 2023

What are Conditionals?

Conditionals are very useful in any programming language. So important that we can argue that the foundation for all the technologies around us are conditions

In our daily activities we say:

If something then do something

For example:

  • If you need money then work
  • If you are sick then go to the hospital
  • If you are fat then exercise

Different Types of Conditonals in PHP

There are 3 ways to write conditionals in PHP.

  1. if elseif else
  2. switch
  3. ternary

If elseif else

if ($condition) {
    // do something
} elseif($another_condition) {
    // do something else
} else {
    // do this
}

Let’s translate this sentence to PHP

If you need money then work

$needMoney = true;
if ($needMoney){
    work();
} else{
    rest();
}

If we change $needMoney to false then the rest() function will be called. Otherwise the work() function will be called.

Switch

The second type of conditionals is switch Rather than writing multiple ifs you can combine them in a switch statement

switch ($variable) {
    case value1:
        // do this
        break;
    case value2:
        // do this one instead
        break;
    case value3:
        // do this for third value
        break;
    default:
        // in case of no values matched do this
}

its if-else equivelant would be something like this

if ($variable == value1){
    // do this
}elseif($variable == value2){
    // do this one instead
}elseif($variable == value3){
    // do this for third value
}else{
    // in case of no values matched do this
}

In switch if multiple conditions need to run the same code then you can combine the conditions like this

switch ($variable) {
    case value1:
    case value2:
        // do this for first and second values
        break;
    case value3:
        // do this for third value
        break;
    default:
      // in case of no values matched do this
  }

Ternary

It’s like a short version of if else

(condition) ? (do this if true) : (otherwise do this);

the examples above would be something like this:

($needMoney) ? work() : rest();

You can combine multiple ternary conditions and write a complicated one. In that case your code might be functional but make sure it’s readable too.

(condition) ? (condition) ? (do this if true) : (otherwise do this) : (condition) ? (do this if true) : (otherwise do this);

see ? it can be unreadable very soon.

https://youtu.be/wgUA-a5D-_w

Conclusion

Now you know about conditionals in PHP.

I recommend you to open a PHP file and try every one of them. try to write the same condition in different ways.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • what are conditionals in php
  • how to write an if else conditional
  • how to use switch
  • what is ternary

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP array filter
Feb 14, 2023 programming

PHP array filter

Today we are going to talk about array filter in PHP. array filter is a very useful function and it helps you filter an array by key, value or both. ...

6 Min Read Read More
PHP Array Intersect
Feb 14, 2023 programming

PHP Array Intersect

Today we are going to talk about array intersect functions in PHP .If you want to find the items in your array that are present in the other arrays, array intersect is your friend. ...

5 Min Read Read More
PHP Array Diff
Feb 14, 2023 programming

PHP Array Diff

Today we are going to talk about array diff functions in PHP. If you want to find the items in your array that are not present in the other arrays, array diff is your friend. ...

5 Min Read Read More
How to Split an Excel File into Multiple Files Using Python
May 08, 2024 programming

How to Split an Excel File into Multiple Files Using Python

Sometimes you might have an excel files that you need to split. splitting the file into multiple files with Python can be easily done. ...

7 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...