Hi, I have a simple query here, one of my database fields is boolean, 1 or 0, when I echo out the value I want it to be yes or no,
I did this, but it doesnt work,

$value = .$row[booleanresult];
if ($value ="1"){
$value = "YES"
} else
$value = "NO"

can someone help please?

5 Years
Discussion Span
Last Post by websponge

Try this:

$value = .$row[booleanresult];
if ($value) { // if true
  $value = "YES";
} else {
  $value = "NO";

phorce has it, but to show you why your code didn't work:

if ($value ="1"){

This sets $value to 1 every time, so regardless of the initial value, it will always return 'YES' in your code.

The comparison operator is the '=='

if ($value == 1){

That should work - should work with 1 and "1". However, phorce's solution is nicer.

Edited by diafol

Votes + Comments
Eeek! I should have given more of a descriptive answer, but I was really busy! Thanks for extending it, good answer!

Here is the extended response proving both of the responses above :) are true.


function check_bool($value){

return ($value == 1 ? 'yes' : 'no');


$value = true; // this can be from database

## let us test the function above.

echo 'This shows a true value : <b>'. check_bool($value) .'</b><br/>';

echo 'This shows a false value : <b>'. check_bool('false') .'</b><br/>';

echo 'This given the value of 1 : <b>'. check_bool(1) .'</b><br/>';

echo 'This has value of 0 : <b>'. check_bool(0) .'</b><br/>';

echo 'This has value of "1" : <b>'. check_bool("1") .'</b><br/>';

echo 'This has value of "0" : <b> '. check_bool("0") .'</b><br/>';

echo 'This has an empty value : <b> '. check_bool(" ") .'</b><br/>';

## another way of utilizing the above function can be something like a comparator seen in c or java not like it, but serves like it ( now, I am confused which one is it really ???) :)

echo (check_bool($value) == 'yes' ?  '<b> Yes is the answer </b>' : 'No Way' );
Votes + Comments
Good Job!
show off! :)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.