How to use the PHP Ternary Operator?

Are you tired of writing long, cumbersome if-else statements in your PHP code? If so, then it’s time to learn about the ternary operator.

In PHP, the ternary operator is a shorthand way of writing an if-else statement. It allows you to evaluate a condition and return a value based on the result of that evaluation. This can help you write cleaner, more concise code that’s easier to read and maintain.

Here’s the basic syntax of the ternary operator:

$result = (condition) ? value_if_true : value_if_false;

Let’s take a look at an example to see how this works in practice. Say you have a variable called “age” that holds a person’s age. You want to determine whether the person is an adult (18 or older) or a minor (under 18). Here’s how you could use the ternary operator to do this:

$age = 25;

$result = ($age >= 18) ? "Adult" : "Minor";

echo $result; // Output: "Adult"

In this example, we first evaluate the condition ($age >= 18) to see if it’s true or false. If it’s true, the ternary operator returns the value “Adult”. If it’s false, the operator returns the value “Minor”.

One thing to keep in mind is that the ternary operator is an expression, not a statement. This means that you can use it anywhere you would use a value, such as in a variable assignment or a function argument. You can’t use it to control the flow of your code, like you would with an if-else statement.

Here’s another example that shows how to use the ternary operator in a more complex situation. Say you have a function called “getDiscount” that takes a price and a discount code and returns the discounted price. If the discount code is valid, the function applies the discount and returns the new price. If the discount code is invalid, the function returns the original price. Here’s how you could implement this function using the ternary operator:

function getDiscount($price, $discountCode) {
  // Define a list of valid discount codes
  $validDiscountCodes = ["SUMMER20", "FALL30", "WINTER40"];

  // Check if the discount code is valid
  $isValidCode = in_array($discountCode, $validDiscountCodes);

  // Calculate the discount amount
  $discountAmount = ($isValidCode) ? (int) substr($discountCode, -2) : 0;

  // Calculate the discounted price
  $discountedPrice = $price - ($price * $discountAmount / 100);

  // Return the discounted price
  return $discountedPrice;
}

// Test the function
echo getDiscount(100, "SUMMER20"); // Output: 80
echo getDiscount(100, "INVALIDCODE"); // Output: 100

In this example, we use the ternary operator twice: once to check if the discount code is valid, and once to calculate the discount amount based on the code. We also use the ternary operator as part of the function’s return value, so that the function returns the discounted price if the discount code is valid, and the original price if the code is invalid.

As you can see, the ternary operator can be a powerful tool in your PHP toolkit. It can help you write cleaner, more concise code that’s easier to read and maintain. With a little practice, you’ll be using the ternary operator like a pro in no time. Happy coding!