How to treat boolean? - php

I've received a boolean from a query, and I want to check if the boolean's value is greater than zero. Is it possible to modify the approach used when handling resources to check a boolean's value? (See; example of resource handling below:)
return (mysql_result($query, 0) == 1) ? true : false;
Any help appreciated.
Thank you in advance.

Since PHP would interpret as a string, you can simply cast it as (bool)
return (bool)(mysql_result($query, 0));
Non-zero values will cast as TRUE. Note that you should only do this if the return values are 0 or 1. Negative values will cast as TRUE.
var_dump((bool)"1");
// bool(true)
var_dump((bool)"0");
// bool(false)
var_dump((bool)-2);
// bool(true)

Just do return mysql_result($query, 0) == 1;
Don't do something like return $a_boolean_value ? true : false;.

I've received a boolean from a query
that is not true. you can't get boolean from the query but merely a string.
I want to check if the boolean's value is greater than zero.
booleans cannot be measured in that way. boolean can be either true or false. or, it you like it - zero or non-zero. not "greater" but just "not".
'greater than' operator in PHP is >. so, you can use it in your expression instead of equal operator(== one)

Related

strpos function error. I'm trying to parse MRZ from string [duplicate]

if(strpos("http://www.example.com","http://www.")==0){ // do work}
I'd expect this to resolve as true, which it does. But what happens when I do
if(strpos("abcdefghijklmnop","http://www.")==0){// do work}
This also passes on php 5 because as far as I can work out the strpos returns false which translates as 0.
Is this correct thinking/behaviour? If so what is the workaround for testing for that a substring is at the beginning of another string?
Yes, this is correct / expected behavior :
strpos can return 0 when there is a match at the beginning of the string
and it will return false when there is no match
The thing is you should not use == to compare 0 and false ; you should use ===, like this :
if(strpos("abcdefghijklmnop","http://www.") === 0) {
}
Or :
if(strpos("abcdefghijklmnop","http://www.") === false) {
}
For more informations, see Comparison Operators :
$a == $b will be TRUE if $a is equal to $b.
$a === $b will be TRUE if $a is equal to $b, and they are of the same type.
And, quoting the manual page of strpos :
This function may return Boolean
FALSE, but may also return a
non-Boolean value which evaluates to
FALSE, such as 0 or "". Please
read the section on Booleans for
more information. Use the ===
operator for testing the return
value of this function.
=== and !== compare type and value as shown below:
if (strpos("abcdefghijklmnop", "http://www.") !== false) {
// do work
}
strpos returns an int or boolean false. the == operator also evaluates 0 to mean false, you want to use the === operator (three equals signs) that also checks that the types being compared are the same instead of just seeing if they can be evaluated to mean the same.
so
if (strpos($hastack, $needle) === 0)
{
// the $needle is found at position 0 in the $haystack
}
0 is a possible return value from strpos when it finds a match at the very beginning. In case if the match is not found it returns false(boolean). So you need to check the return value of strpos using the === operator which check the value and the type rather than using == which just checks value.
I personally tend to use this way :
if(!strpos($v,'ttp:'))$v='http://'.$v;
or
if(strpos(' '.$v,'http'))
to avoid the "0" position then always make it a number more than 0
cheers

Php "" and 0 return same value

$page_now=array_search($id, $user_id);
if($page_now==""){return TURE;}
else{return FALSE}//include [0]index
I have an array_search, if it can't find the match it will return "",
However I have problem on [0] index
if the search index return 0 which is 1st one from array.
if statement $page_now=="" & $page_now==0 both are return TURE
Try this
$var=0;
if($var!=""){echo "have value in var";}else{echo "no value in var";}
I want it return have value even it is 0
This is a documented behavior:
http://php.net/array_search
http://php.net/manual/en/types.comparisons.php
http://www.php.net/manual/en/language.types.type-juggling.php
Warning
This function may return Boolean FALSE, but may also return a
non-Boolean value which evaluates to FALSE. Please read the section on
Booleans for more information. Use the === operator for testing the
return value of this function.
Also make sure you are aware about this:
strict
If the third parameter strict is set to TRUE then the
array_search() function will search for identical elements in the
haystack. This means it will also check the types of the needle in the
haystack, and objects must be the same instance.
You should use strict comparison operator === if you don't want to fall into dark abyss of PHP weak-types comparisons:
php > var_dump(0 == "0afca13435"); // oops, password's hash check went wrong :)
bool(true)
php > var_dump(0 == false);
bool(true)
BUT:
php > var_dump(false == "0afca13435");
bool(false)
// Uh, oh :) that's because int and string comparison will cast string to int,
// and in php string->int cast will return either 0 or any numeric prefix the
// string contain; bool and string comparison will cast string to bool, and
// numeric prefix is no longer an issue
----------
php > var_dump(false == "");
bool(true)
php > var_dump(0 == "");
bool(true)
// WTF! :)
And with strict:
php > var_dump(0 === "0afca13435");
bool(false)
// ahh, much better
The function array_search() returns false if it can't find the match. So you should use strict comparison operator === and compare it with false:
if($page_now===false) {
return true;
}
else {
return false;
}
try this (the empty checks if the variable is empty (""(empty string), 0, false, null etc ) is counted as empty (and will trigger this)
your code is now not checking if something is empty you only check if it isn't "" and null etc. will be triggered as not empty.
if(empty($page_now)){
return true;
}else{
return false;
}
if it is allowed to be 0 you can use this
if(empty($page_now) && $page_now != 0){
return true;
}else{
return false;
}

Logic in PHP, why is this happening?

function x() {return -1;}
echo x();
echo "<br>";
if(x()) {echo "True";} else {echo "False";}
output:
-1
True
Why am I getting 'True' outputted, surely the if() test would fail as it's negative?
That's because you can only test on true or false.
False is defined as 0, while true is defined as not 0. So -1 is just as much true as 1, 2, 3 etc.
To make sure you're getting the right result, make a real comparison.
-1 is considered TRUE in boolean context. See Converting to boolean in the PHP manual.
Only numeric 0 values are false in PHP: http://php.net/manual/en/language.types.boolean.php
0 is false and everything else is true. That's why !
In PHP a -1 is true as it isn't 0. Use a real comparison like:
if(x() <= 0) { ...do stuff... }
Have a look here:
var_dump(x()); //output: int(-1)
and casted to boolean:
var_dump((bool)x()); //output: bool(true)
-1 is not false in PHP. You could check if it's > 0?
All what is 0 is false and everything else is true. Wikipedia article about it
Converting to boolean in PHP
To explicitly convert a value to boolean, use the (bool) or (boolean) casts. However, in most cases the cast is unnecessary, since a value will be automatically converted if an operator, function or control structure requires a boolean argument.
See also Type Juggling.
When converting to boolean, the following values are considered FALSE:
the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags
Every other value is considered TRUE (including any resource).
Warning
-1 is considered TRUE, like any other non-zero (whether negative or positive) number!
Source http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting

Comparison Operator - Type Juggling and Booleans

I've been reading the PHP Docs on Type Juggling and Booleans but I still don't understand why this comparison evaluates as true. My [incorrect] understanding tells me that in the below if statement, the integer 0 is considered FALSE and "a", being a non-empty string is considered TRUE. Therefore, I expected this comparison to resolve to FALSE == TRUE and ultimately, FALSE. Which part did I get wrong?
<?php
if(0 == "a"){
$result = "TRUE";
}else{
$result = "FALSE";
}
//$result == "TRUE"
?>
http://codepad.viper-7.com/EjxBF5
When PHP does a string <=> integer comparison, it attempts to convert the string to a number in an intelligent way. The assumption is that if you have a string "42" then you want to compare the value 42 to the other integer. When the string doesn't start with numbers, then its value is zero.
From the docs:
The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero).
This behavior is also inferred in the comparison docs (look at the first line in the first example).
Your mistake is that you assume operator == coerces each of its operands to boolean before comparing them. It does no such thing.
What happens is that since you are comparing an integer to a string, the string is converted to an integer (in this case "a" converts to 0) and then the comparison 0 == 0 is performed.
It will work if you use a strict comparison === instead of ==. The strict comparison also checks the type of the variables, so 0 === 'a' would be false.

php 5 strpos() difference between returning 0 and false?

if(strpos("http://www.example.com","http://www.")==0){ // do work}
I'd expect this to resolve as true, which it does. But what happens when I do
if(strpos("abcdefghijklmnop","http://www.")==0){// do work}
This also passes on php 5 because as far as I can work out the strpos returns false which translates as 0.
Is this correct thinking/behaviour? If so what is the workaround for testing for that a substring is at the beginning of another string?
Yes, this is correct / expected behavior :
strpos can return 0 when there is a match at the beginning of the string
and it will return false when there is no match
The thing is you should not use == to compare 0 and false ; you should use ===, like this :
if(strpos("abcdefghijklmnop","http://www.") === 0) {
}
Or :
if(strpos("abcdefghijklmnop","http://www.") === false) {
}
For more informations, see Comparison Operators :
$a == $b will be TRUE if $a is equal to $b.
$a === $b will be TRUE if $a is equal to $b, and they are of the same type.
And, quoting the manual page of strpos :
This function may return Boolean
FALSE, but may also return a
non-Boolean value which evaluates to
FALSE, such as 0 or "". Please
read the section on Booleans for
more information. Use the ===
operator for testing the return
value of this function.
=== and !== compare type and value as shown below:
if (strpos("abcdefghijklmnop", "http://www.") !== false) {
// do work
}
strpos returns an int or boolean false. the == operator also evaluates 0 to mean false, you want to use the === operator (three equals signs) that also checks that the types being compared are the same instead of just seeing if they can be evaluated to mean the same.
so
if (strpos($hastack, $needle) === 0)
{
// the $needle is found at position 0 in the $haystack
}
0 is a possible return value from strpos when it finds a match at the very beginning. In case if the match is not found it returns false(boolean). So you need to check the return value of strpos using the === operator which check the value and the type rather than using == which just checks value.
I personally tend to use this way :
if(!strpos($v,'ttp:'))$v='http://'.$v;
or
if(strpos(' '.$v,'http'))
to avoid the "0" position then always make it a number more than 0
cheers

Categories