Voting

: min(two, two)?
(Example: nine)

The Note You're Voting On

phpnet dot 20 dot dpnsubs at xoxy dot net
15 years ago
Note that in php the ternary operator ?: has a left associativity unlike in C and C++ where it has right associativity.

You cannot write code like this (as you may have accustomed to in C/C++):

<?php
$a
= 2;
echo (
   
$a == 1 ? 'one' :
   
$a == 2 ? 'two' :
   
$a == 3 ? 'three' :
   
$a == 4 ? 'four' : 'other');
echo
"\n";
// prints 'four'
?>

You need to add brackets to get the results you want:

<?php
$a
= 2;

echo (
$a == 1 ? 'one' :
        (
$a == 2 ? 'two' :
        (
$a == 3 ? 'three' :
        (
$a == 4 ? 'four' : 'other') ) ) );
echo
"\n";
//prints 'two'
?>

<< Back to user notes page

To Top