Is there any limitation or problem with where you use echo on a page that is mixed html and php

I have a php page that also includes html5. My favicon works fine but when I add some php code for testing / error display the favicon stops working. I just wondered why and if there is some fundamental rule regarding php echo and it’s position relevant to html5. Obviously I can work around this, and the page will ultimately be more complex and error reporting lines removed before going live, but is there some fundamental rule I am not aware of or some fundamental mistake I am making

Favicon does not work like this…

<!DOCTYPE html>
<html lang="en">
<?php
echo 'loading error reporting ...<br>';
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>
<head>
	<!-- Favicon -->
	<link rel="icon" type="image/png" href="../img/favicon.png" />
	<meta content="keywords will go here" name="keywords">
	<meta content="Description will go here" name="description">
	<title>Title will go here</title>	
</head>
<body>	
	Hi ya !
</body>	
</html>

But if I comment out the echo like below, it works…

<!DOCTYPE html>
<html lang="en">
<?php
// echo 'loading error reporting ...<br>';
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>
<head>
	<!-- Favicon -->
	<link rel="icon" type="image/png" href="../img/favicon.png" />
	<meta content="keywords will go here" name="keywords">
	<meta content="Description will go here" name="description">
	<title>Title will go here</title>	
</head>
<body>	
	Hi ya !
</body>	
</html>

cheers

It’s most likely because you’re generating invalid html by doing it the way you are. You’re ending up with this and the browser engines are getting confused.

<!DOCTYPE html>
<html lang="en">
loading error reporting ...<br>
<head>

If you really need a display of that message, move that whole php block into the body, and it should work OK.

1 Like

Yea, cheers. Makes sense when ya step back and take a new look. Thanks for the idea, works fine, in fact I can just move the echo. Kind of forget sometimes that an echo is basically creating html. :grinning:
Doooh
Thanks again!

1 Like

Php’s error related settings should be in the php.ini on your system, so that -

  1. They will cause ALL errors to be reported and displayed/logged. By putting them in your code, they won’t cause php parse errors to be reported and displayed/logged for the file they are in since your code never runs for this type of error to even cause the settings to take effect.
  2. You can change the settings at a single point without searching through and editing the settings in a bunch of files.
  3. You can move your code from your development system to a live server without needing to go through and edit/remove the settings.

If you do have an occasion where you temporarily need to put the settings into your code, they should be near the top of your code file, before any functional code, in an initialization section, not in the html document, which should be near the end of your code. You also cannot set the display of php startup errors in your code, because, well, php has already started at that point and the setting will have no effect.

2 Likes