php

PHP One Line If Statement

Decision-making is a critical part of any productive application. Conditional statements allow us to evaluate matching conditions and take action accordingly.

In PHP, decision-making constructs are implemented using if and if…else statements. Let us discuss using these statements and implement them in our programs.

PHP If Statement

The if statement in PHP allows you to check for a specific condition and perform a particular action if the state is true or false.

The syntax is shown below:

  if(condition) {
        // action
    }

The program checks the condition in parenthesis. If the condition is true, it executes the code inside the curly braces.

We can illustrate this with an example as shown below:

<?php
    $age = 20;
    if ($age > 18) {
      echo "Pass!!";
    }
?>

The previous code checks if the value stored by the $age variable is greater than 18. If true, it prints the string “pass!!”.

The output is shown below:

Output:
Pass!!

PHP If…Else

In the previous example, we check for one condition and act if it is true. However, if the condition is false, the program does not do anything.

We can use an if…else block to specify the action if the condition is false.

The following syntax is provided:

    if(condition){
        // action if true
    } else {
        // action if false
    }

The following example is shown:

<?php
    $age = 10;
    if ($age > 18) {
      echo "Pass!!";
    }
    else {
      echo "Denied!!!";
    }
?>

In this example, we set the value of the $age variable to 10. Then, we use an if..else block to check if the age is greater than 18. If true, echo “Pass!!” else print “Denied!!”.

The previous code should return output as shown below:

Output:
Denied!!!

PHP If…Elseif…Else

The other condition constructs in PHP is the if..elseif..else statement. This allows you to evaluate multiple conditions in a single block.

The following syntax is shown:

  if (test condition 1){
        // action if condition 1 is true
    } elseif (test condition 2){
        // action if condition 2 is true
    } else{
        // action if all are false
    }

We can implement the following example:

<?php
  $age = 37;
  if ($age  18 && $age 25 && $age < 65) {
    echo "Tickets are 15$";
  }
  else {
    echo "Not allowed";
  }
?>

If we run the previous code, we should get the following output:

Output:
Tickets are 15$

PHP One Line If Statement

PHP provides us with a ternary operator to create a one-line if statement. It acts as a more concise version of an if…else statement.

The syntax is provided below:

(Condition) ? trueStatement : falseStatement

Here’s the following example :

<?php
  $age = 37;
  echo ($age < 18) ? "Under age" : "Pass";
?>

This should return:

Output:
Pass

Both the ternary operator and an if…else statements work similarly. However, one is more verbose and readable, while the other is minimal and concise.

Conclusion

This tutorial covered conditional statements in PHP, including the ternary operator statement.

In addition, examples were provided to evaluate the matching conditions. Check other Linux Hint articles for more tips and tutorials.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list