473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

isset returns true when it should return false

<?php
$var['a'] = 'test';

echo isset($var['a']['b']) ? 'true' : 'false';
echo '<br>';
echo isset($var['b']) ? 'true' : 'false';
?>

Why does that result in this output?:

true
false

It seems to me that it should instead output this:

false
false

Using array_key_exists instead works, but I'm not sure why isset
doesn't? Is this maybe a PHP bug?

Apr 27 '07 #1
2 2559
On 27.04.2007 21:01 yawnmoth wrote:
<?php
$var['a'] = 'test';

echo isset($var['a']['b']) ? 'true' : 'false';
Since $var['a'] is string, php applies string indexing operator, which
converts its second argument to an integer. That is, $var['a']['b']
becomes $var['a'][0] and equals to "t", which is, of course, set.

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Apr 27 '07 #2
yawnmoth kirjoitti:
<?php
$var['a'] = 'test';

echo isset($var['a']['b']) ? 'true' : 'false';
echo '<br>';
echo isset($var['b']) ? 'true' : 'false';
?>

Why does that result in this output?:

true
false

It seems to me that it should instead output this:

false
false
It has something to do with the fact that $var['a'] is a string and
strings can be handled as an array of characters. I only assume php
casts any non-integer offsets to integers since strings (when handled as
an array of characters) is non-associative. Thus 'b' is cast to int,
which is zero. var_dump($var['a']['b']) echoes 't' which is the first
character in 'test', which supports the theory that 'b' is being casted
to int.

Also, if you use an integer instead of string:
$var['a'] = 7;
echo isset($var['a']['b']) ? 'true' : 'false';
it yields false now, since an integer is not an array.
Using array_key_exists instead works, but I'm not sure why isset
doesn't? Is this maybe a PHP bug?
A bug or a feature? That's a tricky question. I suppose this is
undocumented feature. The string accessing with [] is explained in the
manual, but it does not mention the automatic casting.

The workaround:
echo (is_array($var['a']) && isset($var['a']['b'])) ? 'true' : 'false';

Make sure you are dealing with an array, because the results of isset
are ambiguous when it's a string.

--
Ra*********@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
Apr 27 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Martin Lucas-Smith | last post by:
I wrote the function below as part of a larger class. The fopen stage works, and, as according to the documentation at www.php.net/fopen that succesfully creates a new file. The fwrite stage...
3
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns...
5
by: lkrubner | last post by:
www.php.net says: >>>>>>>>>>>> Only for SELECT,SHOW,EXPLAIN or DESCRIBE statements mysql_query() returns a resource identifier or FALSE if the query was not executed correctly. For other type of...
6
by: Patrick Fitzsimmons | last post by:
Hi, I'm sure I should know this, but I can't find it in the manual. Is there a function in Python like the function in PHP isset()? It should take a variable name and return True or False...
5
by: juglesh | last post by:
"$string = isset($xyz) ? $xyz : "something else";" Hello, someone gave code like this in another thread. I understand (by inference) what it does, but have not found any documentation on this...
48
by: Skybuck Flying | last post by:
Hi, I came across this C code which I wanted to understand etc it looked like this: if (-1) etc It made me wonder what the result would be... true or false ? In C and Delphi
2
by: Schorschi | last post by:
Can't seemd to get ReadFile API to work! Returns invalid handle error? =========================================================================== Ok, the visual basic gurus, help! The...
9
by: wouter | last post by:
hey hi..... I wanna make a switch wich does this: if pagid is set do A, if catid is set do B, if projectid is set do C, else do D. So i was thinking something like this:
9
by: arundelo | last post by:
Is there a way to tell whether accessing a variable will result in an "Undefined variable" E_NOTICE? isset() almost does this, but it also returns false if a variable is set to null: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.