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

Courses