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 7827529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:55:09+00:00 2026-06-02T09:55:09+00:00

I am new to PHP. When I use echo to print an array element,

  • 0

I am new to PHP. When I use “echo” to print an array element, my success seems to depend on what I name the index. That can’t be right, right?! I feel like I am going crazy. This code:

$ARRAY['q1'] = 'foo';
echo "q1 is $ARRAY[q1]<br>";

Works fine. But this code:

$ARRAY['1q'] = 'foo';
echo "1q is $ARRAY[1q]<br>";

Produces the error:

Parse error: syntax error, unexpected T_STRING, expecting ‘]’ in /var/www/html/test.php on line 6

I know that I can correct the problem like this:

echo "1q is " . $ARRAY['1q'] . "<br>";

But my question is WHY would the array index “1q” vs. “q1” matter in the first code block? I even checked to see if 1q is a constant of some kind but it doesn’t seem to be. Is this an improper way to insert an array element in a string? (I copied it from the PHP documentation.)

This is in PHP 5.3.8. I really appreciate any help.

EDIT:
Ok I got this echo syntax from Example #8 on this page of the PHP manual: http://www.php.net/manual/en/language.types.string.php Apparently it is not the right way to do things. I will add a user-contributed note to the manual.

An entire script that produces this error would be:

<?php
$ARRAY['1q'] = 'foo';
echo "1q is $ARRAY[1q]<br>"; 
?>
  • 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-02T09:55:13+00:00Added an answer on June 2, 2026 at 9:55 am

    When using arrays in a double-quoted string, if PHP sees the array key start with a number, it assumes the entire key is a numeric index, e.g.

    echo "$arr[123abc]";
    

    is interpreted as $arr[123]. The abc portion is what’s producing the expected string error, since a numeric key can’t contain non-numeric components.

    To fix this, you’ll have to use

    echo "1q is {$ARRAY['1q]'}<br>";
                ^       ^   ^^
    

    with properly quoted array indexes

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

Sidebar

Related Questions

I am new on PHP, i am wondering where can i insert / use
I'm printing a <ul> of array $_SESSION['words'] in PHP. // PHP print new words
I have a new install of Ubuntu and trying to use the php's mail()
I'm trying to learn how to use oop in php. I'm also fairly new
We are in the startup build phase of a new PHP webapp that will
Rather new to php, so sorry if this seems stupid. I'm really copying a
Im new to PHP and I can't figure out what the rules are for
I'm trying to use mongodb with PHP. For that, I have created a MongoHQ
So currently I'm working on a project that requires the use of PHP and
I am pretty new to PHP and I am trying to use the googlechartphplib...

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.