1

I'm getting an error trying to make this

$array = array("text"); echo "text $array['0']"; 

it's possible to make it shis way instead concatenate

echo "text ".$array['0']; 
2
  • echo "text ".$array['0']; should work Commented Apr 24, 2018 at 10:46
  • Or enclose your array element in { } - echo "text {$array['0']}"; Commented Apr 24, 2018 at 10:48

2 Answers 2

2

remove single quotes ' inside double quortes ",

echo "text $array[0]"; 

Live Demo

Sign up to request clarification or add additional context in comments.

4 Comments

Always disliked the way PHP has these inconsistent ways of doing things.
This not inconsistency, how php know that, It is direct php variable or variable is used to mix with string. like these exmaple1 and example2
If it's in ", miss out the ', if you miss out the ' when it isn't in " you get a notice (Use of undefined constant) if the index is non-numeric. A few too many if's for me.
you can think about execution and conversion (like interpolation). any language uses the conversion before execution, and php aslo does, it converts ["a"] to [a] but if write [a] it treat as constant and convert with the constant value. so its depends how you write and how it convert before execution. Thanks for comment.
1

1. You need to remove ' around 0

echo "text $array[0]"; 

Output:- https://eval.in/993458

2.Or enclose array element with {}

echo "text {$array['0']}"; 

Output:-https://eval.in/993460

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.