True/False value giving output 1 or blank using PHP - php

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.

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);

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.

Check the manual for casting to string. Try this:
echo $available_image ? 'true' : 'false', '<br>';

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

Related

why array_search function returns the key of 0 element when it is asked to search for a not included value?

Ok here I have an array named $fuitArray including some integers or strings (in this case [1,2,3,4,5,6,7,8,9,0]).
Everything works good when I try to set the $searchTarget to any of the elements inside the array, but when I try to search for anything else (in this case 'x') it returns 9 (the key for the 0 in the array).
It will also work fine if I remove 0 from array and search for a not-included element!
<?php
$fruitArray=[1,2,3,4,5,6,7,8,9,0];
$searchTarget='x';
$searchResult=array_search($searchTarget,$fruitArray);
if ($searchResult===false){
echo $searchTarget.' Not found'.'<br>';
} else{
echo $searchTarget.' Found # key#: '.$searchResult.'<br>';
}
echo '<br>';
while(current($fruitArray)!==false){
echo key($fruitArray).'. ';
echo current($fruitArray).'<br>';
next($fruitArray);
}
/>
i expected this to show
'x Not found'
instead of
'x Found # key#: 9'
array_search function doesn't check types of variables by default. It means strings and numbers will be compared by numbers rules.
For example, those rules might look like:
echo 0 == 'x' ? 'true' : 'false';
This example displays 'true' because it doesn't use strict comparison.
If you want to compare with the strict mode you should set strict parameter to true. For example:
$searchResult = array_search($searchTarget, $fruitArray, true);
You can get more information about the comparison in the documentation
You can do : $fruitArray=['1','2','3','4','5','6','7','8','9','0'];
Then it will return : x Not found

Why does boolval(false) return empty in php?

I'm a little confused how the php function boolval works. Consider the following
<?php echo boolval(true); // prints 1
And contrast with
<?php echo boolval(false); // prints nothing?
Why am I not getting back either true/false? And why is boolval(false) returning nothing?
I ran into this problem while trying to parse $argv for a boolean argument. What's the correct of extracting a bool value from $argv if not with this function?
From the php manual
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.
use
echo $boolres ? 'true' : 'false';
Following the examples in the documentation, this would be the way to show boolean values:
echo 'false: '.(boolval(false) ? 'true' : 'false')."\n";
See: http://php.net/manual/en/function.boolval.php
The manual also says:
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.
See: http://php.net/manual/en/language.types.string.php

returning true outputs 1 but returning false outputs nothing

It's not very important but I was just curious to know the difference.
echo isA("A"); //outputs 1
echo isA("B"); //outputs nothing. why doesn't it output 0?
Anybody can shed somelight on this matter? It does seem to me as a double standard when you look at it from the point of view that "true" outputs as "1" but "false"does not output "0".
Again, no big deal but I think there must be a reason for PHP to be designed like that. Knowing that may give some more insight into this beautiful language.
A true value will manifest itself as a visible 1, but a false value won't. So, tell me what's the advantage of this method?
example function I referred above;
function isA($input){
if ( $input == "A" ):
return true;
else:
return false;
endif;
}
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.
http://us3.php.net/manual/en/language.types.string.php#language.types.string.casting
If you want to print a boolean for debugging you can use var_dump or print_r.
Because when false is casted to string it becomes '' -- empty string.
To see the difference use var_dump(); instead of echo
var_dump((string) true);
var_dump((string) false);

Boolean issues in PHP

I have a question regarding bools in php. I have a stored mysql proc that is returning a boolean. When this value is grabbed on the php side it displays the value as being a 0 or 1. This all seems fine to me and I have read in the php manual that php will interpret a 0 or 1 as false or true at compile time but this does not seem to be the case to me. I have gone a step further and casted my returned value with (bool) but this still does not seem to work.
My if statements are not properly firing because of this. Does anyone know what is going on? Thanks for the help.
MySQL does not have a proper BOOL or BOOLEAN data types. They are declared as synonyms for TINYINT(1). Your procedure will return 0 or 1, which being on non-PHP ground will get transformed into a string in PHP land, so in PHP you have the strings '0' and '1'.
It is weird however that boolean casting does not convert them to the appropriate booleans. You may have some other bugs in your code.
Are you trying to cast the direct result from the query? Because that one is probably an array and:
var_dump((bool) array('0')); // true
Maybe this is your problem. Inspect the returned result.
It sounds like the boolean value is being returned as a string.
Try something like this:
$your_bool = $field_value === "0" ? false : true;
Using the script below. (You'll have to add HTML line break tags before the word "Boolean" inside the left quote to make the output look like my sample; when I do, Firefox interprets them, making the format look strange).
You'll see that the second line produces a null value which MySQL sees as something different from 0 or 1; for TINYINT it stores the PHP true value correctly but nothing for the PHP false, since a null value has no meaning for TINYINT.
Line four shows type casting with (int) is a way to insure that both PHP true and false are stored to MySQL TINYINT Boolean fields. Retrieving the resultant integers from MySQL into PHP variables works since integers are implicitly cast when assigned to PHP Boolean variables.
echo "Boolean true=".true;
echo "Boolean false=".false;
echo "Boolean (int)true=".(int)true;
echo "Boolean (int)false=".(int)false;
Here's the output from PHP 5.3.1 for MySQL 5.1.41:
Boolean true=1
Boolean false=
Boolean (int)true=1
Boolean (int)false=0
Oh! And PHP Boolean literals may be all lowercase or uppercase with the same result... try it yourself.
I use a helpful function "to_bool" for anything I'm not sure of the type of:
function to_bool($value, $default = false)
{
if (is_bool($value)) {
return $value;
}
if (!is_scalar($value)) {
return $default;
}
$value = strtolower($value);
if (strpos(";1;t;y;yes;on;enabled;true;", ";$value;") !== false) {
return true;
}
if (strpos(";0;f;n;no;off;disabled;false;null;;", ";$value;") !== false) {
return false;
}
return $default;
}
Then:
if (to_bool($row['result'])) { ... }

Is there a way to get "true"/"false" string values from a Boolean in PHP?

When I cast to Boolean (using (bool)), is there a built in way to get PHP to actually return the constants true or false. At the moment I'm getting 1 or blank, which evaluate to true and false respectively.
I want the value returned for clearer semantics. However, if I can't get it, I'll just settle with 1 and blank.
In case you're too lazy to do a comparison and echo a string or if you just want to keep it short you can use :
var_export($boolean, true); // the second parameter is to return and not output
PHP: var_export
PHP displays boolean values as 1 (true) or empty string (false) when outputted.
If you want to check if it's true or false use == (if implicit conversion is OK) or === (if it's not). For example:
echo $val ? 'true' : 'false'; // implicit conversion
echo $val === true ? 'true' : 'false'; // no conversion
I don't know of any way to make PHP output boolean values natively as true or false.
If you're looking for the strings "true" and "false," a ternary conditional would be perfect:
<?=(($boolean) ? "true" : "false")?>

Categories