Hawk Kroeger
Hawk Kroeger

Reputation: 2334

Strange result from isset() on php 5.2.9

Can anyone explain why on PHP 5.2.9 this statement evaluates to true instead of false?

echo $value = '2010-01-01 12:30:45';
echo "<br>";
echo isset($value['everythingistrue']) ? 'TRUE' : 'FALSE';

Upvotes: 1

Views: 192

Answers (3)

salathe
salathe

Reputation: 51950

You're inadvertently accessing a single character (the first) in the string, which is indeed set.

Individual characters can be accessed by numeric offset, so the following will work fine

$string = "abc";
echo $string[2], $string[1], $string[0]; // cba

In your case, the string 'everythingistrue' is not a valid string offset and so is converted to a number using the normal string-to-integer rules: so it becomes 0. $value[0] is set (it is the first character, 2), so the isset() call returns true resulting in your ternary operation evaluating to the string TRUE.

For slightly more info, have a peek at String access and modification by character in the manual.

Upvotes: 10

tacone
tacone

Reputation: 11441

var_dump($value['everythingistrue']) may clear up your doubts

Upvotes: 0

Mārtiņš Briedis
Mārtiņš Briedis

Reputation: 17752

$value is a string. You can get characters from it like this $value[0] - first character, $value[4] - fift.

If you try to get the 'blablabla' character, PHP tries to convert it to an Int and it results in 0 - the first character which is "2". Try it - echo $value['everythingistrue']

Upvotes: 4

Related Questions