1

I am facing one issue. I am setting boolean true/false value but when I am echo that value its showing me 1/blank using PHP. I am explaining my code below.

  $qry="SELECT c.member_id,c.rest_name,c.quadrant,c.proviance,c.postal,c.address,c.country,c.city,c.person,c.mobile,c.url,c.share_url,c.premium,c.image,c.multiple_image,c.business_phone_no,c.latitude,c.longitude,q.quadrant AS quadrant_name,ct.city_name FROM db_restaurant_basic AS c LEFT JOIN db_quadrant AS q ON c.quadrant=q.quad_id LEFT JOIN db_city AS ct ON c.city=ct.city_id  WHERE c.member_id='".$member_id."' and c.status=1 ORDER BY rand()";
    $fetchqry=mysqli_query($connect,$qry);
    if(mysqli_num_rows($fetchqry) > 0){
      while($row1=mysqli_fetch_array($fetchqry)){
          if($row1['multiple_image']==''){
              $available_image=false;
          }else{
             $available_image=true;
         }
      }
    }
echo  $available_image.'<br>';

Here that echo output is given me 1/blank value . Here I need to get the true/false as output.

  • 1
    Possible duplicate of [PHP - Get bool to echo false when false](https://stackoverflow.com/questions/4948663/php-get-bool-to-echo-false-when-false) – iainn Jan 15 '18 at 11:35
  • In your code php is reading **true** and **false** as boolean number so it is displaying as 1/blank. so make it string by putting inside the quotation mark as **"true"** and **"false"**. – Sushank Pokharel Jan 15 '18 at 11:39

4 Answers4

4

PHP doesn't support printing True/False, so you can use following as a work-around:

echo $available_image ? 'true' : 'false';

Or even simpler, you can use:

echo json_encode($available_image);
d3corator
  • 1,154
  • 1
  • 9
  • 23
1

There is a simple explanation.

As the documentation page of echo() describes it, echo outputs one or more strings. Since the value of $available_image is not a string but a boolean, conversion is required.

The documentation page of strings, section Converting to string explains:

A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string values.

This is how PHP works. Also take a look at the type comparison tables.

If you need to display true or false (as strings), all you have to do is to use $available_image as the condition into a ternary comparison operator to produce the strings you need:

echo $available_image ? 'true' : 'false';

Or, if you print the value of $available_image only for debug purposes, you can use var_dump() instead of echo(). It displays not only the value (of the expression passed to it) as string (it produces true and false for booleans) but also the type of the value.

axiac
  • 68,258
  • 9
  • 99
  • 134
0

Check the manual for casting to string. Try this:

echo $available_image ? 'true' : 'false', '<br>';
steffen
  • 16,138
  • 4
  • 42
  • 81
0

Please declare the $available_image at the top of your code and set default value TRUE/FASLE to it like follows

  $available_image = FALSE;
Sanjun Dev
  • 518
  • 8
  • 20