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 random number
Feb 15, 2023 programming

PHP random number

Today we are going to talk about generating random numbers in PHP. Knowing how to generate a random number is more useful than you think. you can select random users and much more. ...

3 Min Read Read More
PHP upload file
Feb 15, 2023 programming

PHP upload file

Today we are going to talk about uploading a file in PHP. Uploading a file is simple but it's very important to know how to handle uploading the files correctly and securely. ...

7 Min Read Read More
AngularJS Beginner to Advance 2025: The Complete Guide for Modern Web Developers
Jul 18, 2025 programming

AngularJS Beginner to Advance 2025: The Complete Guide for Modern Web Developers

In today’s web development landscape, AngularJS may no longer dominate headlines, but it still powers countless legacy applications. Many organizations—especially in finance, healthcare, and enterprise software—rely on AngularJS systems that require ongoing maintenance and enhancements. ...

43 Min Read Read More
PHP Beginner to Advanced 2025: The Complete Guide to Mastering PHP and Building Modern Web Applications
Jul 17, 2025 programming

PHP Beginner to Advanced 2025: The Complete Guide to Mastering PHP and Building Modern Web Applications

PHP is often called the engine room of the modern web. Despite debates about newer technologies like Node.js, Go, or Python’s Django, PHP has remained a consistent favorite among developers. In fact, as of 2025, PHP powers over 75% of all websites that use a known server-side programming language. ...

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