![]() |
Given three different numbers, the task is to find the Largest Number using Ternary Operator in PHP. In PHP, the ternary operator (?:) allows you to make a quick decision based on a condition. It is a shorthand method to write an if…else statement. You can use the ternary operator to find the largest number among two or more numbers. Table of Content Approach 1: Comparing Two NumbersThe simplest use case of the ternary operator is to compare two numbers and return the largest one.
Output Largest Number: 20 In this example, if $num1 is greater than $num2, then $num1 is assigned to $largest; otherwise, $num2 is assigned. Approach 2: Comparing Three NumbersYou can also use the ternary operator to compare three numbers and find the largest one.
Output Largest Number: 20 In this example, the outer ternary operator compares $num1 and $num2 first. If $num1 is greater, it compares $num1 with $num3 to determine the largest. If $num2 is greater, it compares $num2 with $num3 to determine the largest. |
Reffered: https://www.geeksforgeeks.org
PHP |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |