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.
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.
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.