Blake O'Hare .com

PHP Tutorial Part 2 - Input & Output

What's the point of a program if you can't have input or output? In the case of PHP, the output of the program is HTML. The input, however, usually takes the form of jibberish tacked on the end of the URL. On many websites, you may notice a question mark in the URL followed by a list of parameters with equal signs after them. Don't recall seeing anything like that off the top of your head? Just Google something and look at the URL! But let's first figure out how to do output.

Output

Output can be done in a few ways. First of all, PHP is inserted inside an HTML document. This means that a regular old-fashioned HTML document is technically a valid PHP file. And therefore is a form of HTML output. To start a block of PHP code, you simply surround your PHP code with <? and ?>. Here's an example...

<html><body>
<p>This is HTML code</p>
<? PHP CODE GOES HERE! ?>
<p>This is HTML code again</p>
</body></html>

If you want to generate output from within PHP code, then you use the echo command. Like so...

<html><body>
<p>This is HTML code</p>
<?
  echo "<p>hello, there</p>";
?>
<p>This is HTML code again</p>
</body></html>

Whitespace does not matter in PHP. You can put as many spaces, tabs, or linebreaks between things and it won't make a difference. So it's best to use whitespace in a way that makes your code as readable as possible. This PHP code generates the following HTML code that the end-user sees...

<html><body>
<p>This is HTML code</p>
<p>hello, there</p>
<p>This is HTML code again</p>
</body></html>

To run PHP files, upload them to your PHP server and simply visit the URL of the file in a browser as though it were an HTML file.

The user never sees any of the PHP code! But this was a boring example. Let's do something that HTML can't do. Like multiplication.

<html><body>
<p>This is HTML code</p>
<?
  echo "<p>";
  echo 397529 * 9573250;
  echo "</p>";
?>
<p>This is HTML code again</p>
</body></html>


This generates the following HTML code that the user's browser sees...

<html><body>
<p>This is HTML code</p>
<p>3805644499250</p>
<p>This is HTML code again</p>
</body></html>

And that's output. But it's no fun if it says the same thing every time. So let's move on to Input.

Input

Input allows you to add an extra parameter to your script so the result of the script can vary. One common use for this may be, for example, a page ID passed to a PHP script which looks up the ID# in a database of articles and then displays the corresponding article with that ID. This is how many article-based websites work. Here is an example of a parameter passed to a PHP script...

http://blahblahblah.com/myPHPscript.php?variable=4279


With this technique we can quickly modify our code from above and turn it into a simple multiplication calculator.

<html><body>
<p>This is HTML code</p>
<?
  echo "<p>";
  echo $a * $b;
  echo "</p>";
?>
<p>This is HTML code again</p>
</body></html>

Now when you view your PHP script in a web browser, add ?a=5&b=8 to the end of the URL. If all goes well, you will see this in your browser...

This is HTML code
40
This is HTML code again


$a and $b are what we call variables.

Variables

Forget what you know about variables from high school math. Those were place-holders for values that you were trying to solve for. You did not know what the variables stood for until the end of the math problem. That is not the case here. These variables are more like temporary storage units. A variable can hold any sort of data, but typically it holds numbers or text 90% of the time. In this case we're passing a number via the URL as a and b and our PHP script takes those values and multiplies them. We don't know what the value of $a and $b are at the time we write the code, but when we run the PHP script, the server knows what values those are from the URL and properly switches in the corresponding values.

Variables in PHP always begin with a dollar sign. That's just the notation the designers of the language chose. No money is involved. Or maybe there is money involved, but purely by coincidence.

More on variables in the next section.

Next: Manipulating Variables and Program Flow
Back to Tutorial Overview