samedi 21 avril 2018

Weird PHP "basic true / false declaration" results in two situations

I am confused with basic true / false declaration results in PHP code in two different situations. Lets assume that strlen($item["description"]) = 50. I want to add "..." if the description is longer than 20 characters.

Case 1:

$art = strlen($item["description"]) > 20 ? $item["description"] : substr($item["description"], 0, 20) . "...";
echo $art;

Case 2:

$cut = strlen($item["description"]) < 20 ? $item["description"] : substr($item["description"], 0, 20) . "...";
$art = $cut;
echo $art;

My question is: Why I have to change the "<" operator in Case 1 to ">", if I want to add "..." for bigger than 20 char desc.? In Case 2 everything works fine (the first statement is true and the second false).

Thanks for help!

Aucun commentaire:

Enregistrer un commentaire