PHP HelpPHP Help

Online Community that helps beginners learn PHP,
and webmasters solve PHP coding problems

since 1999





Common PHP Tasks and Questions

Printing to Web Pages and First Steps to Functions

How do I include PHP values in a HTML tag?

Although it may seem a strange question, beginners often wonder how they can output values as attributes into HTML tags, not just between them. Maybe this is because PHP code sections look like HTML tags and they are unaware that you can intermingle HTML and PHP code anywhere on the page. This includes the "inside" portion of tags. Although PHP code looks like a tag itself, it is not restricted to any particular place in HTML. A web page is just on long stream of text where PHP can itself generate HTML codes. Values from variables can be inserted into HTML tags by printing them where you want them to appear in the HTML code.

An easy way to include a <?php ... ?> tag in an HTML tag (<body>) is shown here.

<body bgcolor="<?php print $bgcolor?>" text="<?php print $textcolor?>">
 

Mixing code directly into HTML tag attributes can easily become confusing and error prone. The following code is cleaner and more controlled way of specifying values in a PHP generated tag:

<?php
  $bgcolor 
"#fffff";
  
$text "#000000";
  
$link "#0000FF";
  print 
"<body bgcolor=$bgcolor text=$text link=$link>";
?>
 

In this example, I make it easier on the programmer by not quoting attribute values. This is not the best practice. The HTML specification requires that certain attribute values be quoted. It's usually a good idea to always quote, but of course stray quotes can easily bring down your program. Try this:

<?php
  $bgcolor 
"#fffff";
  
$text "#000000";
  
$link "#0000FF";
  print 
'<body bgcolor="'$bgcolor .'" text="'$text .'" link="'$link .'">';
?>
 

I use the single quote for static text portions of the string where I do not need variable substitution. Values are included using the concatenation operator, which combines string values into a single string. This may be a little slower but it's a lot clearer. It's also easy to add or insert another string into the output should another attribute be needed.

Using a Function to Make HTML

Instead of writing the same code over and over for each tag, you could make this into a function.

<?php
function html_body($bgcolor,$text,$link ) {
  
$bgcolor "#fffff";
  
$text "#000000";
  
$link "#0000FF";
  print 
'<body bgcolor="'$bgcolor .'" text="'$text .'" link="'$link .'">';
}
?>
 

The function definition can be placed in a configuration page included into any pages where you wish to control the body attributes. Instead of the tag you call the function that generates it. You can reuse the code anywhere the tag is needed, thus saving time, effort and errors.

<?php
function make_html_body($bgcolor,$text,$link ) {
  
$bgcolor "#fffff";
  
$text "#000000";
  
$link "#0000FF";
  print 
'<body bgcolor="'$bgcolor .'" text="'$text .'" link="'$link .'">';
}
?>
<head>
<title>Page With A Dynamic Personality</title>

</head>
<?php print make_html_body("#fffff","#000000","#0000FF"); ?>

<h2>Page With A Dynamic Personality</h2>
 

To drop the HTML output into our page we need only print the value returned from the make_html_whatever() function.

To see how it works, copy the code into a PHP page and run it. The function definition does not have to be literally in the page as I show here, but can be included from another file.

However, sometimes having the function return its value by printing directly to the browser can cause problems. Another programmer may want to use the same function on a page that handles cookies. The print statement may interfere with this. Can we create a function that can be used in any context, with the programmer deciding how to use the output? Yes, here's how:

<?php
function make_html_body($bgcolor,$text,$link )
{
  
$bgcolor "#fffff";
  
$text "#000000";
  
$link "#0000FF";
  
$content '<body bgcolor="'$bgcolor;
  
$content .= '" text="'$text;
  
$content .= '" link="'$link .'">';
  return 
$content;
}
?>
 

Instead of printing the output directly to the web browser through PHP's built-in output stream, we return it as the value of the function. I've broken the sting into three separate concatenations (using the .= operator that both puts the strings together and assigns the new value to the variable), just to show you how it can be done. Often you'll build up the output value from a series of string operations. You don't have to specify a return value, because PHP returns the last value assigned in a function. But it's good documentation practice to explicitly show the return statement. If you're concerned about efficiency, just comment out the return statement.

You've now just learned a very important principle in programming. It's called modularity and shows you the advantages of reusing code, containing code in packages that are easy to locate all in one place (the place where you keep all the functions used on your web pages) and write functions that return values, which can be used without regard to their context. You don't know it, but you are on your way to understanding object oriented programming. But unfortunately, you're also leaving behind the easy days of just mixing code in with HTML. When it comes to large web applications it's often better when the script is generating all HTML directly from a series of print statements or a function.

Pages:  1   2   3   4Next:  Web Pages With Variable Content »




Copyright © 2017 PHPHelp.com