Showing Stuff on the Screen

PHP and HTML are interchangeable. Within your source code, you can tell a PHP program to use HTML very easily.

The only oncept you need to do this is code blocks . To use these, you open and close the sections of PHP commands like this:

<?php        <!-- Open PHP code block -->
... enter your PHP commands here
?>         <!-- Close PHP code block -->
... enter your HTML commands here
<?php         <!-- Open a second PHP code block -->
... enter more PHP commands here
?>         <!-- Close second PHP code block -->


A simple example might be:

<?php
$hello = "Hello World";
?>
<html>
<body>
<p>

<?php
echo $hello;
?>
</p>
</body>
</html>


PHP will then substitute the values as needed to generate the HTML. The above example would then generate:

<html>
<body>
<p>

Hello World
</p>
</body>
</html>



Next article: Variables

Using echo and print

PHP has two commands to print things directly to the screen. These are especially useful to display variables in the middle of HTML code. For example:

< h1 > <?php echo $headertext ?> < /h1 >


This can also be useful to debug your script if you get stuck trying to find out why your PHP program isn't working properly.

To look in detail at the differences between print and echo, look here

Next article: Variables

Previous

The previous article tells you how to get started and how to create your first PHP program.

Next

The next screen gives you a tutorial on how to handle variables in PHP.
This tutorial tells you how to manipulate numbers, strings and how to make decisions and loops in PHP programs.

External Links

  • UK PHP Home Page including PHP manual and examples
  • Crimson Editor - a text editor the lays out PHP code and uses line numbers
  • Wikipedia - list of text editors (look in 'freeware' section for good, free editors)

Download links

Summary

  • Enclose your PHP commands in a code block
  • Open a code block with <?php
  • Close a code block with ?>
  • Use HTML tags after closing a code block

More Details

  • Heredoc
  • Fomatting text to display