php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44863 isset detects an array item that var_dump does not.
Submitted: 2008-04-29 18:34 UTC Modified: 2008-04-30 00:04 UTC
From: igordonin at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.5 OS: Windows XP SP2
Private report: No CVE-ID: None
 [2008-04-29 18:34 UTC] igordonin at gmail dot com
Description:
------------
Development Environment is PHP 5.2.5, Apache 2.2.

When requesting PHP to unset an array item, there's a fatal error:

Fatal error: Cannot unset string offsets in C:\[hidden]\index.php on line 147

When I var_dump the array variable, the array item is not show as part of that array, but the unset array item command is inside a if (isset($variable["ArrayName"])) { ... which means that the item has to exist in order for the unset to occur.

-----

On my production enviroment, though, I'm running PHP Version 4.3.9, with Apache Handler 2.0.52(Oracle) | API Version 20020903, and it does not cause a Fatal Error, but the error is still there. Here's the URL:
http://helpdesk.mozart.com.br/public/index.php
-> Try to connect with anything. It matters not, I think.

If this is not a bug, I do apologize. It's just that I've checked this code so many times and I can't find anything wrong ...

Thanks in advance.

Reproduce code:
---------------
		# 1. Execute Query
		$Query["Try_Login"] = Query_Repository("Try_Login", 0, 2, 0);
		
echo "<br><br> This is the var_dump for array \$Query: <br>";
var_dump($Query);
echo "<br><br>";

if (isset( $Query["Try_Login"]["Error"] )) { echo "<br><br>\$Query[\"Try_Login\"][\"Error\"] is set as: \"" . $Query["Try_Login"]["Error"] . "\".<br><br>"; }
die();

Here are the URLs for the functions that you might need to take a look at. I saved them as txt so you may check them out.

Query_Repository:
http://helpdesk.mozart.com.br/public/query_repository.txt

Request_Value:
http://helpdesk.mozart.com.br/public/request_value.txt

validate_input:
http://helpdesk.mozart.com.br/public/validate_input.txt

Expected result:
----------------
$Query["Try_Login"] was supposed to be set only as string, either with "conn_err" or "conn_ok". That's all var_dump really shows, and that's what it's supposed to be ...

Actual result:
--------------
... but it was also supposed to not have an ["Error"] array Item. It's not shown while using var_dump, but "isset" returns true, and it's value is set to string "c". Still, I cannot unset it for it may produce a fatal error.

Fatal error: Cannot unset string offsets in C:\[hidden]\index.php on line 147

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-04-29 18:39 UTC] colder@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

$variable is a string and not an array. 

Your error : "Fatal error: Cannot unset string offsets" is pretty explicit about it. 
 [2008-04-29 18:43 UTC] igordonin at gmail dot com
You're wrong, var_dump says:

array(1) { ["Try_Login"]=> string(8) "conn_err" } 


It's so an array!
 [2008-04-29 18:51 UTC] colder@php.net
Look: $Query is an array that contains a string at the index "Try_Login". So $Query["Try_Login"] is a string.

You're unsetting $Query["Try_Login"]["Error"] so yes, you're trying to unset an offset of a string.
 [2008-04-29 18:56 UTC] igordonin at gmail dot com
You're right!

But is this expected to return true?

if (isset($Query["Try_Login"]["Error")) {
  unset($Query["Try_Login"]["Error"]);
}

'Cause that's when I'm trying to unset $Query["Try_Login"]["Error"] ...
 [2008-04-29 19:03 UTC] colder@php.net
Yes this is expected:

non-numeric indexes are interpreted as 0 for string offsets.

so isset($string["foo"]) is equivalent to isset($string[0]) which checks whether the string offset 0 exists, and it does, your string is not empty.
 [2008-04-29 19:13 UTC] igordonin at gmail dot com
Hey, I don't mean to be annoying, hope didn't get there yet, but check this out:

Query_Repository returns string "conn_err", so it's the same as:
$Query["Try_Login"] = "conn_err";

var_dump( $Query ) results in:
array(1) { ["Try_Login"]=> string(8) "conn_err" }

There's no suck key as ["Error"] at all ...

So what you mean is that isset will recognize $Query["Try_Login"]["Error"] as $Query["Try_Login"][0] because of the string offset and then return true?

You may be right, that may be in fact the current behavior, but I don't see how that can be the best behavior at all! 

I'm no expert though. So thank you very much for taking the time to reply. I do appreciate it.
 [2008-04-30 00:04 UTC] colder@php.net
for the last time, you've $Query['Try_Login'] = "conn_err"; and you're doing an isset($Query['Try_Login']['Error']);

So, $Query['Try_Login'] is a string.

This is equivalent to $mystring = "conn_err"; and isset($mystring['Error']) which is equivalent to isset($mystring[0]);

You're wrongly using isset() here, You probably want to check whether 'Error' is a defined index of $Query, so do isset($Query['Error']).. instead of isset($Query['Try_Login']['Error']).

But really bugs.php.net is not a place for that kind of support, you should check out php.net/support and find a medium that suits you better.

Regards.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 16:01:36 2025 UTC