PHP Programming/html output

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

There are a few different way you can display html using php. Generally, you will use the echo command to output something. This will be seen by a web browser, and then it will format it.

[edit] Output from Inside The Script

Probably the most important method of outputting text and HTML is using the PHP printing functions from inside your script(s). Let's look at this method first.

[edit] Echo and Quotes

Let's see a few ways to make a the same link.

echo '<a href="http://wikibooks.org">Link</a>';
echo "<a href='http://wikibooks.org'>Link</a>\n";

Now, these are essentially the same. If you use single quotes, your html should have double quotes. This is easier for people who are already used to writing html by hand. The second one uses single quotes in the html, but has the added benefit of allowing newlines and such. You could combine the two and have both benefits, but it has a major drawback... escaping!

echo "<a href=\"http://wikibooks.org\">Link</a>\n";

If you want to use double quotes where you are already using double quotes (same with single quotes and single quotes), you have to 'escape' them to let PHP know you don't want to finish. The backslash does that for you.

If you like your html to have double quotes, and you want the source to be easily readable, you can always have:

echo '<a href="http://wikibooks.org">Link</a>';
echo "\n";

Or:

echo '<a href="http://wikibooks.org">Link</a>'."\n";

This demonstrates that you can use both as PHP doesn't care. But, other people who are looking at your code may!

Parentheses are sometimes used with the echo statement, giving it more the look of a function. Parentheses are strictly optional, but you will see them from time to time:

echo('<a href="http://wikibooks.org">Link</a>');
echo("<a href='http://wikibooks.org'>Link</a>\n");

[edit] Breaking PHP for Output

In addition to using functions such as echo and print, you can also end your script, and anything beyond the end of the script will be output as normal HTML to the browser. You can also restart your script whenever you want after you've closed the PHP tag. Confused? It's actually pretty simple.

Let's say you had a for loop to count up to five and output it.

<?php
 
 echo("<ul>");
 for($x = 1; $x < 6; $x++)
 {
  echo("<li>" . $x . "</li>");
 }
 echo("</ul>");
 
?>

While I would tend to use templates for larger pages that output a lot, we'll get to that later. Remember how all your PHP scripts start with <?php and end with ?>? Those don't have to be the very start and end of your file. In fact, PHP handles ending and restarting scripting just like if everything between the ?> and <?php tags were inside of an echo statement.

Thus, you could do something like this:

<ul>
 
 <?php
 for($x = 1; $x < 6; $x++)
 {
 ?>
  <li><?php echo $x ?></li>
 <?php
 }
 ?>
 
</ul>

This is actually a very common method of outputting variables in a script, especially if there is a lot of HTML surrounding the variables. As I said before, I personally rarely ever do this, as in my opinion, using echo for smaller scripts keeps your code cleaner (and I would use templates for larger ones). However, we want to cover most of the language here, so this is another method you could use.