Originally posted by PaulaRun
How can I have the PHP code without the HTML?? I thought PHP had to be in an HTML page?
Not necessarily. Consider:
<?php
echo "<html>\n<head><title>";
echo "My PHP Page</title>\n</head>";
echo "<body>\nThis is my page about PHP, Hope you like it.\n<br>\n<br>";
echo "\n</body>\n</html>";
?>
PHP is rather good at producing HTML output. However, it's a little slower to use print() or echo() to just output HTML, and it's a waste (why not just use HTML?) so most people mix the two.
OTOH, once a script is called, the PHP parser will execute code whether it's any kind of 'output' to the browser or not; and, as you found out, certain functions, like header(), setcookie() and session_start() ONLY work when nothing has yet been output to the browser (any text of any kind, whether actually code, or even just a space, which also has an ASCII value). So, as a general rule, always start your scripts/pages with enough PHP code to handle all possible branches of the logic tree before you start writing browser output.