Array program of Php embedded in HTML tags giving wrong output

Hi,
I am trying to execute the following HTML program containing Php code. The code is:

<html>
   <body>

      <?php
         /* First method to create array. */
        
         $numbers = array( 1, 2, 3, 4, 5);
         
         foreach( $numbers as $value ) {
            echo "Value is $value <br />";
         }
         
         /* Second method to create array. */
         $numbers[0] = "one";
         $numbers[1] = "two";
         $numbers[2] = "three";
         $numbers[3] = "four";
         $numbers[4] = "five";
         
         foreach( $numbers as $value ) {
            echo "Value is $value <br />";
         }
      ?>
      
   </body>
</html>

I am getting the following output:

"; } /* Second method to create array. */ $numbers[0] = “one”; $numbers[1] = “two”; $numbers[2] = “three”; $numbers[3] = “four”; $numbers[4] = “five”; foreach( $numbers as $value ) { echo "Value is $value

"; } ?>

Somebody please guide me how to solve this problem.

Zulfi.

Zulfi, your file is not being processed by PHP on the server. The web server is just sending it to your browser as if it were html.

  • Do you have PHP running on the server?
  • Is the file named something.php (ie, not something.html)?

Strange that it seems to be processing the first part of it, unless the OP has only put part of the output in their post.

I suspect a problem with the closing quotes on that first echo() that makes PHP think that the string has not been closed, and therefore outputs the rest of the code instead of executing it.

It’s not. The opening < in <?php is treated by the browser as the start of a html tag. The first > that’s encountered, on the end of the <br />, closes that html tag, then everything after that point is outside of a html tag and is displayed as is. The ‘view source’ in the browser would show all the php code.

2 Likes

Oh yes, I see that. Back to the OP then.

Simple solution is to use {} brackets:

<?php
         /* First method to create array. */
        
         $numbers = array( 1, 2, 3, 4, 5);
         
         foreach( $numbers as $value ) {
            echo "Value is {$value} <br />";
         }
         
         /* Second method to create array. */
         $numbers[0] = "one";
         $numbers[1] = "two";
         $numbers[2] = "three";
         $numbers[3] = "four";
         $numbers[4] = "five";
         
         foreach( $numbers as $value ) {
            echo "Value is {$value} <br />";
         }

Hi my friends,
Thanks for your responses and time. If I comment the html tag and name the file as file.php then the program runs fine with or with out using brackets. However, if I use HTML tags and the file as file.html, then I am getting the same previous output, even I use brackets with {$value} variable:

/*<html>
   <body>*/

      <?php
         /* First method to create array. */
         /*https://www.tutorialspoint.com/php/php_arrays.html*/
         $numbers = array( 1, 2, 3, 4, 5);
         
         foreach( $numbers as $value ) {
            echo "Value is $value <br />";
         }
         
         /* Second method to create array. */
         $numbers[0] = "one";
         $numbers[1] = "two";
         $numbers[2] = "three";
         $numbers[3] = "four";
         $numbers[4] = "five";
         
         foreach( $numbers as $value ) {
            echo "Value is $value <br />";
         }
      ?>
 /*     
   </body>
</html>*/

Based upon this problem I have commented the html tags and renamed the file as “*.php”.
Zulfi.

Zulfi, whether you comment or remove the html tags depends on where you’re trying to output this. If the device is a web browser, it expects html and thus you should leave the html in there (with the file named *.php). On the other hand if you’re just wanting to see the php output via a terminal window, then you don’t want html tags at the start/end, and your br tags should be replaced with “\n” (newline) characters.

1 Like

Yes, the html tags aren’t really the issue - as long as the file is not named *.php, the server won’t process any php within it, so the php code is just displayed. This is partly done for speed - there’s no point making the web server parse a file for code that won’t be there, so you can use the names to control that.

In your testing, you commented out the html and renamed the file to *.php and it worked, and then you un-commented the html and renamed back to *.html and it did not. What happened when you left the html un-commented and renamed the file to *.php ? That’s the combination you didn’t mention.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.