3
$a = 'a';
echo isset($a['b']);

This code returns 1. Why?

3
  • stackoverflow.com/questions/9132715/… Commented Aug 23, 2013 at 9:29
  • It returns nothing checked on writecodeonline.com/php But echo isset($a); returns 1. Commented Aug 23, 2013 at 9:32
  • have you tested your code? isset($a['b']) returns false. Commented Aug 23, 2013 at 9:33

5 Answers 5

7

String characters can be referenced by their offset using syntax like $a[0] for the first character, e.g.

$string = 'Hello';
echo $string[1];  // echoes 'e'

so PHP is recognising that $a is a string; casting your 'b' to a numeric (which casts to a 0), and trying to test isset on $a[0], which is the first character a

Though it should also throw an illegal offset 'b' warning if you have errors enabled

EDIT

$a = 'a';
echo isset($a['b']), PHP_EOL;
echo $a['b'];

PHP 5.3

1
a

PHP 5.4

Warning: Illegal string offset 'b' in /Projects/test/a10.php on line 6
a

PHP 5.5

PHP Warning:  Illegal string offset 'b' in /Projects/test/a10.php on line 6

Warning: Illegal string offset 'b' in /Projects/test/a10.php on line 6
a
Sign up to request clarification or add additional context in comments.

2 Comments

@Mark isset wont throw a warning. and as of php 5.4 Checking non-numeric offsets of strings now returns FALSE.`
strange, I tested it on PHP 5.4.16 (both windows and linux) no warnings for echo isset($a['b']), just returns false. my error_reporting is set to -1 and display errors is on.
2

Only for php 5.3:

so lets do it slowly:

$a['b'];

returns 'a' because b is converted to 0 and $a[0] (the first char of 0 = a)

isset($a['b']);

return true because $a['b'] is 'a' not null

echo true;

outputs "1" because true is converted to a string and this to "1".

3 Comments

Thanks for an explaination. I didn't expect that 'b' will be converted to integer.
@Christoph you are totally wrong! isset($a['b']); returns false not true. Have you tested it before posting your answer?
@bansi I'm totally right did you test it before posting your comment? I did. (php 5.3.23) Ahh see Mark Baker's answer .. it doesn't work starting with php 5.4
1

because ISSET return the 1 if the value is set.

Use it like this :

if(isset($a['b']){
echo $a['b'];
}

3 Comments

yes it should, because you have defined $a['b']='a'; No choice there..!!
Where did I defined $a['b'] = 'a'?
i mean at the time of execution the b converted to 0 and 0 stands for the first position of your string so the output will be 'a'.
0

For the same reason as this...

echo true;

PHP cannot echo a non-string/non-integer so it converts the true into 1 and 0 for false.

1 Comment

But it doesn't explain yet why it's returning true.
0
<?php
$a = 'a';
var_dump($a);
?>
it will gives output string(1) "a" 
if you will echo $a['b'] it will give you output as a so $a['b'] also has value
hence
<?php
$a = 'a';
echo isset($a['b']);
?>
outputs value 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.