Skip to content
Advertisement

Short IF and Long IF Variable Assignment Method in PHP

I have 2 codes that do the same task. 2.In the long code $sort = 'newest'; I’m throwing the variable. But in the 1st shortcode I don’t know how to assign it.

*The code to be edited is the 1st Code. (Short Code)

Short IF PHP Code

JavaScript

Long IF PHP Code

JavaScript

My fault trial result:

JavaScript

Advertisement

Answer

Using nested ternary expressions like the “short code” are generally considered bad practice as they are difficult to read, understand, and maintain. In a team environment, I would expect any code that uses them to be rejected in code review.

In a case like this, I’d probably use a switch statement something like the below:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement