Conditional ternary operator malfunctions (PHP)

Angel Politis

I have built a function to change a given Y-m-d date like this: 2016-07-02 to this format: July 2nd.

The code:

// Format the given Y-M-D date
function format_date($date) {
    // Parse the date
    list($year, $month, $day) = array_values(date_parse($date));

    // Give the appropriate subscript to the day number
    $last_char = substr($day, -1);
    $pre_last_char = (strlen($day) > 1) ? substr($day, -2, -1) : null;
    $subscript = ($last_char === "1") ? "st" :
                 ($last_char === "2") ? "nd" :
                 ($last_char === "3") ? "rd" : "th";
    $subscript = ($pre_last_char === "1") ? "th" : $subscript;
    $day .= $subscript;

    // Get the month's name based on its number
    $months = [
        "1" => "January",
        "2" => "February",
        "3" => "March",
        "4" => "April",
        "5" => "May",
        "6" => "June",
        "7" => "July",
        "8" => "August",
        "9" => "September",
        "10" => "October",
        "11" => "November",
        "12" => "December"
    ];
    $month = $months[$month];

    // Omit the year if it's this year and assemble the date
    return $date = ($year === date("Y")) ? "$month $day $year" : "$month $day";
}

The function works as expected, but there's a catch. The first conditional ternary operator for $subscript returns "rd" for every number that ends in 1 and 2.

Example:

echo format_date("2016-01-01"); // It will output January 1rd

How can I fix that?

Alex Blex

Documentation reads:

Note: It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious:

<?php
// on first glance, the following appears to output 'true'
echo (true?'true':false?'t':'f');

// however, the actual output of the above is 't'
// this is because ternary expressions are evaluated from left to right

// the following is a more obvious version of the same code as above
echo ((true ? 'true' : false) ? 't' : 'f');

// here, you can see that the first expression is evaluated to 'true', which
// in turn evaluates to (bool)true, thus returning the true branch of the
// second ternary expression.
?>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Kotlin Ternary Conditional Operator

From Dev

ternary conditional operator equivalent?

From Dev

Ternary Conditional Operator in kotlin

From Dev

Operator 'sizeof' with conditional (ternary) expression

From Dev

Incompatible types in ternary conditional operator

From Dev

Ternary operator spoil OR-conditional

From Dev

Using a concatenation operator with a ternary conditional?

From Dev

How to apply += operator inside a conditional ternary operator

From Dev

Nullpointer exception with conditional operator, (ternary operator) but not with if else

From Dev

Ternary conditional operator on the left of assignment operator

From Dev

understading the ternary operator in php

From Dev

evaluation of ternary operator in php

From Dev

Ternary Operator in PHP

From Dev

PHP Ternary Operator Misunderstanding?

From Dev

evaluation of ternary operator in php

From Java

Does Python have a ternary conditional operator?

From Dev

TCL conditional commands using ternary operator

From Dev

declaring a variable within conditional expressions (ternary operator)

From Dev

Implementing Ternary Conditional Operator in a List-Comprehension

From Dev

Devel::Cover Branch coverage on conditional ternary operator

From Dev

Operator Precedence with Ternary Conditional and Logical And operators in JavaScript

From Dev

How to write following code with Conditional(Ternary) Operator?

From Dev

Pandas: Ternary conditional operator for setting a value in a DataFrame

From Dev

Does Julia have a ternary conditional operator?

From Dev

Nullable(Of ) is not set to Nothing in conditional ternary operator

From Java

How to write a PHP ternary operator

From Dev

PHP ternary operator syntax error

From Dev

Understanding PHP's ternary operator

From Dev

ternary operator in php with echo value

Related Related

  1. 1

    Kotlin Ternary Conditional Operator

  2. 2

    ternary conditional operator equivalent?

  3. 3

    Ternary Conditional Operator in kotlin

  4. 4

    Operator 'sizeof' with conditional (ternary) expression

  5. 5

    Incompatible types in ternary conditional operator

  6. 6

    Ternary operator spoil OR-conditional

  7. 7

    Using a concatenation operator with a ternary conditional?

  8. 8

    How to apply += operator inside a conditional ternary operator

  9. 9

    Nullpointer exception with conditional operator, (ternary operator) but not with if else

  10. 10

    Ternary conditional operator on the left of assignment operator

  11. 11

    understading the ternary operator in php

  12. 12

    evaluation of ternary operator in php

  13. 13

    Ternary Operator in PHP

  14. 14

    PHP Ternary Operator Misunderstanding?

  15. 15

    evaluation of ternary operator in php

  16. 16

    Does Python have a ternary conditional operator?

  17. 17

    TCL conditional commands using ternary operator

  18. 18

    declaring a variable within conditional expressions (ternary operator)

  19. 19

    Implementing Ternary Conditional Operator in a List-Comprehension

  20. 20

    Devel::Cover Branch coverage on conditional ternary operator

  21. 21

    Operator Precedence with Ternary Conditional and Logical And operators in JavaScript

  22. 22

    How to write following code with Conditional(Ternary) Operator?

  23. 23

    Pandas: Ternary conditional operator for setting a value in a DataFrame

  24. 24

    Does Julia have a ternary conditional operator?

  25. 25

    Nullable(Of ) is not set to Nothing in conditional ternary operator

  26. 26

    How to write a PHP ternary operator

  27. 27

    PHP ternary operator syntax error

  28. 28

    Understanding PHP's ternary operator

  29. 29

    ternary operator in php with echo value

HotTag

Archive