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?

Recommended Answers

All 4 Replies

Try this:

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

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.

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

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

<?php

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' );
commented: show off! :) +14
commented: Good Job! +7

Thank you all, most helpful

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.