Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6583855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:27:49+00:00 2026-05-25T16:27:49+00:00

In PHP, you can access characters in a string with the array syntax: $foo

  • 0

In PHP, you can access characters in a string with the array syntax:

$foo = 'abc';
echo $foo[2]; // echos 'c'

I recently spent way too long debugging why $foo['id'] wasn’t giving me the expected result. It turned out that $foo was a string instead of an associative array. PHP seemed to be casting 'id' to the integer 0, without giving any notice:

$foo = 'abc';
echo $foo['id']; // echos 'a', without notice

PHP throws a nice warning when you do this with real arrays:

$foo = array('a', 'b', 'c');
$echo $foo['id']; // Notice:  Undefined index: id in php shell code on line 1

How can I make (or why can’t) PHP throw an “Undefined index” notice instead of casting a string index to 0?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:27:50+00:00Added an answer on May 25, 2026 at 4:27 pm

    Unfortunately, there’s nothing you can do, short of patching PHP.

    However, if you do want to patch PHP, this is a possible patch (against trunk):

    Index: Zend/zend_execute.c
    ===================================================================
    --- Zend/zend_execute.c (revision 316974)
    +++ Zend/zend_execute.c (working copy)
    @@ -1268,7 +1268,7 @@
                                                    case IS_DOUBLE:
                                                    case IS_NULL:
                                                    case IS_BOOL:
    -                                                       /* do nothing */
    +                                                       zend_error(E_NOTICE, "String offset is not an integer");
                                                            break;
                                                    default:
                                                            zend_error(E_WARNING, "Illegal offset type");
    

    Then:

    $ ~/php/php-t/bin/php -d error_reporting=-1 -r '$a="foo"; echo $a["bar"];'
    
    Notice: String offset is not an integer in Command line code on line 1
    f
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In PHP you can access characters of strings in a few different ways, one
In php how can I access an array's values without using square brackets around
Can PHP array passed POST method to catch in ASP.NET MVC? Query string: WebAccess/ArrayTest?val[]=1&val[]=2&val[]=3
How can I access a static variable in a separate class in PHP? Is
In PHP I can use a foreach loop such that I have access to
How can we connect a PHP script to MS Access (.mdb) file? I tried
I would like to access a MySQL database using PHP. Can someone please explain
I have two page: rooms.php and reservation.php User can only access to reservation.php via
In PHP I can access class attributes like this: <?php // very simple :)
Can I access a cookie written with jQuery's cookie plug-in with PHP? I know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.