0

I am coding a simple loop through an associative array in php. Here is my code:

<?php

$height["Peter"]=177;
$height["Laura"]=179;
$height["Adam"]=169;

//echo "Peter is ".$height["Peter"]." cm tall.";

foreach($height as $x => $y) {
    //echo $x;
    echo $x." is ".$y." cm tall.";
    //echo $y;
}

?>

output:

PHP Parse error: syntax error, unexpected '$x' (T_VARIABLE) in /home/ndGQqP/prog.php on line 9

Line 9 starts with the foreach. Could you please help me to find a mistake? Thank you.

Jan
  • 11
  • 4
  • 1
    Can't reproduce with your current code. Tested on PHP 7.1, 8.1 – vee Feb 07 '22 at 16:21
  • Can I ask what exactly is the problem part of my code? – Jan Feb 07 '22 at 16:25
  • 1
    I can't answer because your current code have no problem. Make sure that you post your code that can reproduce the problem. – vee Feb 07 '22 at 16:26
  • Check if your "x" is actually latin, not cyrillic. Remove all commented lines. Try to write separate echo statements without concatenation, i.e. echo $x; echo 'is '; echo $y; – Alex K Feb 07 '22 at 16:32
  • 1
    I have identified the problem. It was a form of a dot instead of a space located on line 9. It was caused by using multiple text editors, because I am on an Android device. Thank you. – Jan Feb 07 '22 at 16:33

0 Answers0