PHP Shorthand If

Short Hand If

PHP allows you to simplify your code by writing single-line if statements when conditions are straightforward.

Tutorials dojo strip

Example

Here’s an example where we check the speed of a motorcycle. If its speed is less than 60, we assign a message to buy a Kawasaki:

PHP

Short Hand If…Else

You can also condense an if...else statement into one line using the ternary operator. This method is concise and ideal for simple conditions.

Example

Let’s say we want to evaluate a car’s price. If the price is less than $20,000, it will recommend a Toyota; otherwise, it suggests a Mercedes:

PHP

Tutorials dojo strip