Parse error: syntax error, unexpected '' in C: \ xampp \ htdocs \ php \ Country and City.php on line 17 nose why you mark it

2
<html>
<head><title>Pais y ciudad</title></head>
<body>
<table width="50%" border="10">
<?php
$ciudad=Array("Pais"=>"Estados Unidos","Lugar"=>"Miami");
$ciudad=Array("Pais"=>"Estados Unidos","Lugar"=>"Nueva Orleans");
$ciudad=Array("Pais"=>"Inglaterra","Lugar"=>"Londres");
$ciudad=Array("Pais"=>"Irlanda Del Norte","Lugar"=>"Belfast");
$ciudad=Array("Pais"=>"Mexico","Lugar"=>"Tejupilco");
$ciudad=Array("Pais"=>"Alemania","Lugar"=>"Berlin");

foreach($ciudad as $Key => $Row){
Echo "$Row[Pais]".""."$Row[Lugar]."."<BR>".";}

$NumElementos=Count($ciudad);
Echo "<TR><TD><P>Ciudad</P></TD> <TD><P>Pais</P></TD></TR>";

For($i=0;$i<$NumElementos;$i++){
Echo "<TR><TD><center>".$ciudad[$i]["Lugar"]."</center></TD></TR> "; 
}
?>

</table>
</body>
</html>
    
asked by Alberto Perez 15.10.2018 в 02:56
source

3 answers

3

I recommend you refactor your code because beyond that it is very difficult to read; you can achieve the same thing with a better structure and fewer lines of code

OBSERVATIONS TO YOUR CODE

  • The first thing is that if you are going to have multiple associative arrangements, for example do one alone but be multidimensional; that is, keep several arrays within the
  • The second thing is that with two foreach you read the route and finally print on a board
  • The last thing you do with count is not clear to me that you want to achieve so if you tell me with pleasure I add it to the answer, but all this that I recommend is going to help you improve your code
  • Sample functional code

    <html>
    <head><title>Pais y ciudad</title></head>
    <style>
        table, th, td {
        border: 1px solid black;
    }
    </style>
    <body>
    <table>
        <tr>
            <th>estado</th>
            <th>Ciudad</th>
        </tr>
    <?php
    
    $destinos = array(
        array("estado" => "michoacan", "ciudad" => "zitacuaro"),
        array("estado" => "mexico", "ciudad" => "toluca"),
        array("estado" => "df", "ciudad" => "magdalena contreras")
    );
    
    foreach($destinos as $destino => $estado){
        echo "<tr>";
        foreach($estado as $e => $ciudad){
            echo "<td>".$ciudad."</td>";
        }
        echo "</tr>";
    }
    
    ?>
    </table>
    
    </body>
    </html>
    

    Result obtained



    On the other hand I make the following observations

      
  • Respect the writing of reserved words, because although PHP is permissive it makes it difficult to keep your code
  •   

    echo goes in lowercase for goes in lowercase

         

    The same as all the other HTML tags that you capitalized    - List element

        
    answered by 15.10.2018 в 03:34
    0

    there are some quotes of more the end of this line:

    Echo "$Row[Pais]".""."$Row[Lugar]."."<BR>".";}
    
    $NumElementos=Count($ciudad);
    

    Which causes the string to close just after Echo on this line:

    Echo "<TR><TD><P>Ciudad</P></TD> <TD><P>Pais</P></TD></TR>";
    

    the < of <TR> is interpreted as "less than" and the > is the unexpected of the error.

    note To avoid this type of errors, it is very useful to use an editor that colors the syntax of the language. Notice how the first Echo looks light blue (indicates a keyword / function / instruction) and the second one looks brown / dark red (indicates string)

    foreach($ciudad as $Key => $Row){
    Echo "$Row[Pais]".""."$Row[Lugar]."."<BR>".";}
    
    $NumElementos=Count($ciudad);
    Echo "<TR><TD><P>Ciudad</P></TD> <TD><P>Pais</P></TD></TR>";
    

    and corrected, notice how the colors change:

    foreach($ciudad as $Key => $Row){
    Echo "$Row[Pais]".""."$Row[Lugar]."."<BR>";}
    
    $NumElementos=Count($ciudad);
    Echo "<TR><TD><P>Ciudad</P></TD> <TD><P>Pais</P></TD></TR>";
    
        
    answered by 15.10.2018 в 03:08
    -1

    YOUR CORRECTED CODE

    <html>
        <head><title>Pais y ciudad</title></head>
        <body>
        <table width="50%" border="10">
        <?php
        $ciudad=Array("Pais"=>"Estados Unidos","Lugar"=>"Miami");
        $ciudad=Array("Pais"=>"Estados Unidos","Lugar"=>"Nueva Orleans");
        $ciudad=Array("Pais"=>"Inglaterra","Lugar"=>"Londres");
        $ciudad=Array("Pais"=>"Irlanda Del Norte","Lugar"=>"Belfast");
        $ciudad=Array("Pais"=>"Mexico","Lugar"=>"Tejupilco");
        $ciudad=Array("Pais"=>"Alemania","Lugar"=>"Berlin");
    
        foreach($ciudad as $Key => $Row){
          echo $Row["Pais"]." ".$Row["Lugar"]." <BR> ";
        }
    
            $NumElementos=Count($ciudad);
            echo "<TR><TD><P>Ciudad</P></TD> <TD><P>Pais</P></TD></TR>";
    
            for($i=0;$i<$NumElementos;$i++){
              echo "<TR><TD><center>".$ciudad[$i]["Lugar"]."</center></TD></TR> "; 
            }
        ?>
    
        </table>
        </body>
        </html>
    
        
    answered by 15.10.2018 в 06:13