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 function
Mar 21, 2023 programming

PHP function

Today we are going to learn about functions in PHP. Functions are very important because they can help you divide your application in small bites that can be handled with a function. ...

10 Min Read Read More
PHP Regular Expressions
Mar 22, 2023 programming

PHP Regular Expressions

Today we are going to learn about regular expressions in PHP. A regular expression is a sequence of characters that specifies a search pattern. it's important to know how to apply them in PHP. ...

5 Min Read Read More
Machine Learning in PHP
Mar 18, 2023 programming

Machine Learning in PHP

Machine learning is a way for computers to learn and improve at tasks without being specifically programmed to do so. Are you curious about machine learning and PHP? ...

10 Min Read Read More
A Beginner's Guide to Different Types of Software Testing
Apr 07, 2024 programming

A Beginner's Guide to Different Types of Software Testing

Software testing is a crucial aspect of programming and software development. By testing, developers can ensure that their software performs as expected. ...

9 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 ...