$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;
}
Related
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
I'm new to PHP and I found in my code, when I pass a FALSE to a function. It converted to null immediately. I've read some articles knowing that False and null are equal. But I don't know when this conversion happens.
Below is my code
function equal($expected, $actual){
if($expected == $actual) { //... }
}
function foo(){
$signal = getSignal();
equal(FALSE, $signal->good); //...
}
You will need to use triple equal signs === for equality. For example
false == null; // true
false === null; // false
0 == false; // true
0 === false; // false
Check the docs on comparison operators
No, they are NOT converted. You can var_dump($variable) at any time to see both the type and the value of $variable any time. But there are a few things which evaluates to false with the == comparison operator. So in fact false == null will evaluate to true just as false == 0 or false == "0" in PHP. This is why the comparison operator === comes into the picture - using that instead of == in above example, all will evaluate to false instead of true.
For more information, see http://php.net/manual/en/language.operators.comparison.php
I know that != is "not equal", but what does it mean when you have this:
if(!$something)
My first guess is something to do with exceptions, but a look around google did not return anything.
So what does this do?
Whatever is in the variable is converted to a Boolean (the variable itself of course remains intact), and then a NOT operation (!) is done on the resulting Boolean. The conversion will happen because ! is a Logical Operator and only works on Boolean values.
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
Tip: If the variable is not expected to be Boolean, you might want to use something more specific like isset($variable), empty($variable), $variable === '', etc. depending on what you want to check for. Check the manual for details.
It's the same as:
if((bool)$something != true) {
See: http://www.php.net/manual/en/control-structures.if.php
if (!$something) {
is an equivelent of
if ($something == false) {
Checks to see whether $something is falsy.
It just means "If not something".
if (!false) {
this_happens_because_not_false_is_true();
}
It converts the variable into boolean equivalent of the variable. This can be given in a few cases:
<?php
// Case 1: $variable is boolean
$variable = true;
$variable = !$variable; // Changes to false;
var_dump($variable); // bool(false)
// Case 2a: $variable is a positive integer
$variable = 5;
$variable = !$variable; // Changes to false;
var_dump($variable); // bool(false)
// Case 2b: $variable is an integer other than 0
$variable = 0;
$variable = !$variable; // Changes to false;
var_dump($variable); // bool(true)
// Case 2c: $variable is a negative integer
$variable = -5;
$variable = !$variable; // Changes to false;
var_dump($variable); // bool(false)
// Case 3a: $variable is string
$variable = "Hello";
$variable = !$variable; // Changes to false;
var_dump($variable); // bool(false)
// Case 3b: $variable is empty string
$variable = "";
$variable = !$variable; // Changes to false;
var_dump($variable); // bool(true)
?>
In short, it makes the opposite of the empty() function! :)
Hope this helps! :)
if(!$variable) is the same as if($variable == false) so it checks if $variable is false
Look at #bažmegakapa answer to see which values are considered false.
it check if !$something is false or you can understand it like (if not$something) then {//this will execute } and if $something is present then the this will not enter in the if
!$variable is the 'Not' logical operator
http://uk3.php.net/manual/en/language.operators.logical.php
it takes a boolean value and flips it. True becomes false and false becomes true.
Checks to see if $something is false.
I met following code once
if (!$this->error) {
return false;
} else {
return true;
}
I thought "if no error , why false?? Thats wrong!" Because i thought "!" operator equals "NOT".
And swaped returns. But everytime error condition codes ran.
Then learned. "!" operator converts variables to boolean. Empty variables converted to "false". And "if" statements with "!" operator run like that "if this boolean is false, return false, else return true.! :)
if($somethin == ""){
}
Or
if($somethin != ""){
}
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)
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