Php – Ternary Operators – POFTUT

Php – Ternary Operators

We have previously examined the if-elseif-else conditional statements. We know that these statements provide branching ability according to the conditions. While using these statements in single and simple situations it may become a trivial task. Php provides an alternative way for simple usage. This is called ternary operators.


Syntax of ternary operator is like below. Php looks to the CONDITION and if the result is true the TRUE part is executed if not FALSE part code is executed.



Here we will check the login function status and if the login is successful the function will return true and true part of the ternary operator will be executed.

$status = login();

($status ? echo "Successfully logged on" : echo "Login Failed");


  • The ternary operator will make code simpler
  • The ternary operator will make code more readable
  • The ternary operator will make the code shorter

LEARN MORE  How To Download, Install, and Configure XAMP To Create A Webpage?

Leave a Comment