Klausos Klausos
Klausos Klausos

Reputation: 16040

User-friendly boolean printing in PHP

In the below code 'resAvailability' might be equal to 1 or 0. Is it possible to update this code in such a way that 1 produces 'Yes' and 0 results in 'No'?

   <?php foreach ($result as $row):?>
    <tr>
    <td><?php echo $row['resAvailability']; ?></td>
    <?php endforeach;?>

Upvotes: 0

Views: 82

Answers (3)

Emil Vikstr&#246;m
Emil Vikstr&#246;m

Reputation: 91902

echo $row['resAvailability'] ? 'Yes' : 'No';

This is called the ternary operator.

Upvotes: 10

Bono
Bono

Reputation: 4849

You mean like this? Very basic stuff

if($row['resAvailability'] == 1)
{
    echo "Yes";
}
else
{
    echo "No";
}

edit
Emil his code is effectively the same is this, though since you asked such a basic question I thought you were quite new and in my opinion this is easier for beginners ;) though I would definitly go with Emil's way (less code and all that).

Upvotes: 3

carbontwelve
carbontwelve

Reputation: 1100

This is the way I would do it:

echo ($row['resAvailability'] == 1) ? "Yes": "No";

Be aware that 1 will also validate as true and 0 as false so in actual fact you don't need the == 1 in my example as either way it will run as:

Is $row['resAvailability'] true, return yes, else return no.

Upvotes: 3

Related Questions