PHPBuilder - The ABC's of PHP Part 3 - Basic Script Building in PHP Page 2



RSS Twitter
Articles Php Gtk

The ABC's of PHP Part 3 - Basic Script Building in PHP - Page 2

by: Peter Shaw
|
March 26, 2009

So, what can I put in these PHP tags?
Well, anything that's valid PHP can go in there, print statements, function calls, variable assignments and so on, as an example using the print statement:

<?php

  phpinfo
();

?>
We are printing the output of the date function directly into a set of <H1> tags, which in this case is a string ready to be printed. Some functions however return different types such as arrays and resources, the good news is that in most cases PHP will try to format the output and display something useful.
For arrays however (We'll cover arrays in more detail in future article in the series) you'll likely want to use the print_r routine, for example:

<?php

  Print_r
($_POST);

?>
The small snippet above will display the contents of the POST array, something which can be very useful when testing forms of data. Simply create a PHP page with just the above in, and set it as the action in your HTML form tag, and hey- presto.
print_r will list the array items in sequence, and if wrapped up in <pre> tags will also lay the output out neatly line after line, as though it was being output to a terminal or command line.
One last useful tip that often aids in debugging is to output the phpinfo call, EG:

<?php

  phpinfo
();

?>
This provides a wealth of information about the environment PHP is running in, and can greatly help when first setting up your server, or when you need a quick reference of what extras you have installed.
For now we'll leave it there, but I would encourage you to read the online PHP Manual. Have a look at each of the function calls and see what they each return. Customize the script above to output different pieces of information.
Summary
In this episode we looked at our first script, and examined the ways in which it could be written, in the next episode we'll be looking at variables, the life blood of any program not least PHP, and remember don't be afraid to experiment, for the most part unless your playing carelessly with file functions, there's no damage you can do, one of the best places to learn is the PHP web site and the user code comments available.
Until next time.
Shawty
The ABC's of PHP Series

« Previous Page
1
|
2

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Peter Shaw

Comment:



Comment:

(Maximum characters: 1200). You have characters left.