PHP Beginnings Ex. #1: Show Text in a Browser

For your first PHP exercise, create a PHP page, with the standard HTML <head>, <title> and <body> tags. This is not strictly necessary but is good practice and should the the first step for each exercise. Consider creating a template file with these tags already in place. Remember to save your file with the .php extension.

Inside the <body> tag, create a PHP section that will show the text “Hello World!” in the browser. Check your work by opening the page in a browser. If you get a blank page, check to be sure that you ended each statement with “;” and that you included the opening and closing PHP markers. When you are ready, move on to the answer script.

Please note that the yellow highlight on text to print to the browser is intended only to make it easy to see what your goal is. You don't need to highlight the text in your scripts.


Your code should be something like this:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     
  2.  "">   
  3.  <html xmlns=""  xml:lang="en" lang="en"> 
  4.  <head> 
  5.  <meta http-equiv="content-type" content="text/html;charset=iso-8859-1" /> 
  6.  <title>Hello, World!</title> 
  7.  </head>   
  8.  <body>   
  9.  <?php 
  10.  echo "Hello, World!"; 
  11.  // You could have used the print statement here. 
  12.  ?>   
  13.  </body> 
  14.  </html>

See the output of the script in a separate window here. You can also view the output's HTML source from the new window, if you need to check that.