This question already has answers here:
Evaluate object to a boolean
(6 answers)
Closed 9 years ago.
I made an object that was suposed to represent an array, but with a few methods. So I made it implement the native interfaces: IteratorAggregate and Countable. That way you can foreach and count it. So far so good.
But now I want it to evaluate like an array as well. So, if count($object) is zero, if($object) is suposed to evaluate as false. Is there a Comparable interface or something?
Use type Juggling.
You can assign the expected type of your var.
$object = count ( $object );
$object = (array) $object;
The same way, if you want to assign to your variable some other type, here is list of possible values:
(int)
(bool)
(float)
(string)
(array)
(object)
(unset)
(binary)
Also check this this Comparable interface.
The documentation shows everything that evaluates to 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
As of this list an object will never be considered as false. So the only way is to cast the value of count() to boolean using (bool) if you need to do a strict comparison as pointed out in Tomi Sebastián Juárez's answer.
Related
This question already has answers here:
Comparing String to Integer gives strange results
(5 answers)
Closed 2 years ago.
PHP 7
in_array('one', [0 => 0]) //true
in_array('one', [0]) //true
in_array('one', array_keys([0 => 0])); //true
Expected result is FALSE, because obviously there are no string values in given array, also there are no 'true' values (string can be evaluated to true and match)
If we assume any type comparsion 0 integer is always compared to false in PHP, and String compared to true, so why this works anyway?
I know, strict mode exists and works fine, but anyway absolute annoying why this happend. Any explanation?
The answer to your question is here:
Comparing String to Integer gives strange results
Same situation when "one" == 0 as in your examples.
TL:DR - on string-to-integer comparison the string will be converted to integer, not the integer to string, so "one" will be converted to 0
in_array works when u use it like this
in_array('one', [0,1])
in_array function expect array as indexed array i.e [0,1,"two"] not associative array (Key value pair).
You can use array_values to ignore the key of specific, it returns only values.
in_array('one', array_values([0 => 0])) // true //tested
I'm facing this dilemma, basically I want to create an error object if a certain task isn't met. Now to understand what I've got to send back to the user, I need to check to see if that error object is either empty or has data. The issue is this:
$obj = new stdClass();
var_dump(empty($obj)); // returns false
As you can see in this example, it's returning false instead of true as it's empty.
$o = new stdClass();
$a = array();
var_dump(empty($o));
var_dump(empty($a));
Example
This works perfectly fine for an array, but does anyone know why this is happening for objects?
I've read answers like this which state to cast it as an array, but my question is this:
Why does it return false when it's empty? What is the logic behind it?
If I wanted an array, I would've started with that.
From php.net:
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
Every object you create won't be "empty".
I noticed the following strange occurrence:
var_dump(in_array("test", array_keys(array("hello"))));
Yields: bool(true)
How is this possible? Array does not contains keys, therefore array_keys() will return an empty array. Test is not in this empty array, so why would it return true? Is this a bug in PHP?
simple answer: loose typing, 'test' == 0
Use
var_dump(in_array("test", array_keys(array("hello")), true));
for strict typing
And all arrays have keys, if you don't explicitly assign a key, then PHP will assign one based on incrementing integers from 0 (hence 'test' == 0).... array_keys() will not be empty, because PHP will assign a key of 0 to your array entry of "hello"
I am dealing with very strange issue of dealing with garbage in iterating through variable which has been cast to an array
$arr = (array)$var; // problem
$arr = array($var); // ok
The first method seems to work fine on values with integers, but not with strings. Is there any documented difference and does php have real casting ?
The problem is with lavarel 4, Database sources, function on line 704
If $var is a scalar, it's documented that both lines do the same:
For any of the types: integer, float, string, boolean and resource, converting a value to an array results in an array with a single element with index zero and the value of the scalar which was converted. In other words, (array)$scalarValue is exactly the same as array($scalarValue).
http://www.php.net/manual/en/language.types.array.php#language.types.array.casting
There are two ways to cast a variable in PHP as a specific type.
using the settype() function
using (int) (bool) (float) etc
More Info : http://www.electrictoolbox.com/type-casting-php/
Do you know why <?= count(false) ?> returns 1?
It's specified behavior:
If var is not an array or an object with implemented Countable
interface, 1 will be returned.
According to http://php.net/manual/en/function.count.php
Because false is also a value and if the count() does not get array but a valid variable it returns true which is 1.
$result = count(null);
// $result == 0
$result = count(false);
// $result == 1
A nice way to remember this in your mind:
count(false) is basically the same as:
count ("one boolean"), and therefore there are "ONE" booleans as result.
It looks to me like PHP is preventing one from using count() to determine if an element is an array or an object. They have dedicated functions for this (is_array(), is_object()) and it may be tempting to naively use count() and check for a false condition to determine array or object. Instead, PHP makes non-objects, non-arrays return 1 (which is truthy) so that this method cannot be be naively used in this way (since 0 is a valid, falsy result for an empty array/object).
This may be the why behind the choice of value to be returned by the function in the situation you're describing.