Computer Magic
Software Design Just For You

PHP Tutorial – Lesson 3 Print statements everywhere!!!

Welcome to tutorial 3. Today we are going to cover the concept of output. This is an important topic to understand and will compliment the information covered in the last tutorial.

All programs (web, application, windows, Linux, dos, etc..) have I/O
(Input/Output). For a Dos program (that black screen with white writing for you noobs… start -> run -> cmd), the input is the keyboard and the output is the console (that black screen again). Some programs do not
require any input. The fact that you are starting the program might be
enough. I have written scripts to process a web server log file (how many visitors, hits, etc…) and generate neat graphs and statistics. This was a very special custom script that worked great on my system, but would probly not work on yours. Why not? Because I hard coded all the information the script needed to function right into the script. The main thing it would need to know would be the location of the log file
(c:\windows\log_files\mylog.log). If you put this script on your computer and tried to make it run, it probly couldn’t find your log file because you keep yours in a different spot than mine.

We could add some Input capabilities and have it wait for you to type in the path to your log file. We could simply read the next line typed in at the keyboard. This sounds like a good idea, except that the user would be
sitting there staring at a blank screen. Their first reaction… “stupid
free internet code, this stuff is crap!”. What is wrong? The program is
sitting there with a blinking cursor waiting for input, but the user doesn’t know what to do. We need to prompt the user with some output. Something like “Please enter the file path to your log file:” should do it. We can do this by simply using your languages print statement. This print statement sends the stuff you tell it to print to the current default or standard output (the screen for a console program, or the browser for a PHP script).


print "Please enter the file path to your log file: ";

In the script above, we tell the php script to output the prompt to the
requesting browser. Notice that the data (the text we want to print) is all contained in the “s. Since this is a PHP block, we have to contain all NON PHP code in “s. Take a look at this next PHP script.

Please enter the file path to your log file:

In this script, there are no PHP tags. Since there are no PHP tags, the PHP engine treats this as regular HTML. How does PHP deal with regular HTML? It simply treats it as if you had use the print statement to send it to the browser. Try them both out, they will have the same output: Please enter the file path to your log file:

Now, lets mix and match…


print "PHP Print 1
?> HTML Print 1
still HTML printing... <?php
print "PHP Print 2
?> HTML Print 2....
print "PHP Print 3
?> <?php
print "PHP Print 4
?> HTML print 3....

This script outputs text from both HTML and PHP. Notice that they happen in order. You don’t get all the PHP, then all the HTML, you get them in the order that the PHP processor finds them. This is a nice feature as it can be some what difficult to put a large quantity of HTML inside of “s. Very hard to read.

The point of all this is that the ultimate purpose of any PHP script is to generate some output and send it to the browser for review. It all happens through a series of print statements (either manual or automatically). This also means that you can do some cool stuff with the output before it gets shipped off. As an example, PHP allows you to compress your pages before sending it out on the wire and save you some bandwidth (as long as the
client browser understands zipped content). I myself have code that captures the output so that I can process it further before it goes to the browser (toss in number of page views, add a template to the page automatically,

You might ask, if PHP can create its own output, then what does HTML have to do with anything? If you looked at these scripts, then you will know that they look pretty plain. By adding some HTML, we can liven things up and
make them look nice. HTML is not a language, it is a formatting
specification. We can create tables, set background images, and show links to more information. This is what makes HTML useful and why we use it in
conjunction with PHP. It is common for PHP to generate HTML on the fly by using various print statements. Think about or They
both use a dynamic engine like PHP to generate their pages on the fly. Part of that is the outputting of HTML to turn the plain data into a killer website. The data is the information extracted from a database by the scripting engine and contains the book or item details, but the part that lines it up and adds some pictures and color is the HTML.

Notice that you can open/close many different PHP blocks in one script. You can do this as many times as you see fit. The best way to figure this stuff out is to play with it. Get some server space and upload some scripts and see what they do. Good luck 🙂

Ray Pulsipher


Computer Magic And Software Design

Comments are closed.

Home | My Blog | Products | Edumed | About Us | Portfolio | Services | Location | Contact Us | Embedded Python | College Courses | Quick Scan | Web Spy | EZ Auction | Web Hosting
This page has been viewed 726848 times.

Copyright © 2005 Computer Magic And Software Design
(360) 417-6844
computer magic