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

the ultimate guide to Git: learn everything you need in less than 1 hour
Nov 28, 2022 programming

the ultimate guide to Git: learn everything you need in less than 1 hour

Here is everything you need to know about git in a series of short question and answers. ...

11 Min Read Read More
PHP Static
Mar 22, 2023 programming

PHP Static

Today we are going to talk about Static methods and properties in PHP. You can access the methods and properties of a class without create a new object but for that you need to know about static. ...

5 Min Read Read More
PHP Validate Email
Feb 15, 2023 programming

PHP Validate Email

Today we are going to talk about validating emails in PHP. In a lot of projects you need to validate the emails given by the user to make sure the email address has a correct format. ...

4 Min Read Read More
Node.js Beginner to Advanced 2025: Everything You Need to Know to Build Modern Projects
Jul 20, 2025 programming

Node.js Beginner to Advanced 2025: Everything You Need to Know to Build Modern Projects

Node.js has matured into one of the most essential tools in modern web development. Originally released in 2009, Node.js transformed JavaScript from a strictly frontend language into a robust backend solution capable of powering APIs, real-time applications, microservices, IoT systems, and more. ...

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