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'];
afdrd25
  • 37
  • 4

2 Answers2

2

remove single quotes ' inside double quortes ",

echo "text $array[0]";

Live Demo

Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
  • 1
    Always disliked the way PHP has these inconsistent ways of doing things. – Nigel Ren Apr 24 '18 at 10:53
  • This not inconsistency, how php know that, It is direct php variable or variable is used to mix with string. like these [exmaple1](https://eval.in/993498) and [example2](https://eval.in/993499) – Niklesh Raut Apr 24 '18 at 11:02
  • 1
    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. – Nigel Ren Apr 24 '18 at 11:09
  • 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. – Niklesh Raut Apr 24 '18 at 11:15
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

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98