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

  • SEARCH
  • Home
  • 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 8808301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:35:19+00:00 2026-06-14T02:35:19+00:00

<?php error_reporting(-1); $a = array(‘key’ => ‘2’); $a = $a[‘key’]; var_dump($a); echo $a[‘key’], \n;

  • 0
<?php
error_reporting(-1);
$a = array('key' => '2');
$a = $a['key'];
var_dump($a);
echo $a['key'], "\n";
?>

I expected the code to throw empty string or a warning saying key isn’t present, but the code prints 2;

PHP 5.3.10-1

Update1 : Adding var_dump and error_reporting.

Output is

hari@hari-laptop:~$ php run.php  
string(1) "2"  
2  
  • 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-06-14T02:35:20+00:00Added an answer on June 14, 2026 at 2:35 am

    $a is a string that holds the value "2".

    Strings in PHP can be indexed to access their characters using square brackets. For example:

    $a = "abcde";
    echo $a[3];  // This will print 'd'
    

    When the index is a string, PHP tries to parse it as an integer. If the string is not a number, parsing will give 0. Hence, The zero’th index, representing the first character is used. (In PHP 5.4 and up, it gives a warning). In your case, you got “2” because $a['any_stuff'] = first character = '2'

    From PHP manual:

    String access and modification by character

    Characters within strings may be accessed and modified by specifying
    the zero-based offset of the desired character after the string using
    square array brackets, as in $str[42]. Think of a string as an array
    of characters for this purpose. The functions substr() and
    substr_replace() can be used when you want to extract or replace more
    than 1 character.

    As of PHP 5.4 string offsets have to either be integers or integer-like strings, otherwise a warning will be thrown.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple code: <?php error_reporting(E_ALL); ini_set('display_errors', true); ini_set('html_errors', false); $test = array(); $test
<?php error_reporting(E_ALL); ini_set(display_errors, 1); $AWS_ACCESS_KEY_ID = KEY; $AWS_SECRET_ACCESS_KEY = ACCESS KEY; $base_url = http://webservices.amazon.com/onca/xml?;
The code chokes at fopen(): <?php ini_set('display_errors',1); error_reporting(E_ALL); $fp = fopen(/path/to/file/some_file.txt,a) or die(can't open
Right now I have this code. <?php error_reporting(E_ALL); require_once('content_config.php'); function callback($buffer) { // replace
<?php error_reporting(-1); $config = array ( siteURL => http://domain.com, loginCheck => checkuser.php, userAgent =>
The code: #!/usr/bin/php <?php error_reporting(E_ALL); while(true){ // Check how many API calls remain $rate_limit
<?php error_reporting(E_ALL); include func.php; connectToDatabase(localhost,root,,test); $fileName = $_FILES['name']; $fileSize = $_FILES['size']; $fileType = $_FILES['type'];
Whether using error_reporting(0) in php script will prevent hacking using SQLi or XSS by
<?php ini_set('display_errors',1); error_reporting(E_ALL); require_once('/include/adLDAP.php'); $adldap = new adLDAP(); $username = user123; $password = pass123;
A PHP Error was encountered Severity: Notice Message: Uninitialized string offset: 0 Filename: index.php

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.