+ 2

The code in the description is working in playground where as it should not because it has mistakes...plz tell me why

<?php $myArray[0] = "John"; $myArray[1] = "<strong>PHP</strong>"; $myArray[2] = 21; echo "$myArray[0] is $myArray[2] and knows $myArray[1]"; ?>

26th Oct 2017, 8:16 PM
Bilal Asghar
Bilal Asghar - avatar
6 Answers
+ 5
Because in Php string in double quotes are interpreted: if they contains variable names, they are replaced by their value, so your last line is equivalent to: echo $myArray[0]." is ".$myArray[2]." and knows ".$myArray[1];
26th Oct 2017, 8:28 PM
visph
visph - avatar
+ 3
Your question, and almost your problem, is unclear: it works as it should in sololearn code playground... have you problem with trying to use it elsewhere?
26th Oct 2017, 8:22 PM
visph
visph - avatar
+ 3
( btw i did not write this code, it is an example code from arrays exercise of php course )
26th Oct 2017, 8:35 PM
Bilal Asghar
Bilal Asghar - avatar
+ 2
why the variable myArray[0] is inside double quote, i thought only strings need double quotes
26th Oct 2017, 8:25 PM
Bilal Asghar
Bilal Asghar - avatar
+ 2
It's echoing a string. That's what echo does.
26th Oct 2017, 8:26 PM
Bagshot
Bagshot - avatar
+ 2
dot should be used for string concatenation and the array value place without double quotation only the string part must be in double quotation
27th Feb 2018, 11:45 AM
Kamalakanta Biswal
Kamalakanta Biswal - avatar