Talk 25-07-2000 PHP
From CLUG Wiki
Contents |
An Introduction to PHP
Craig Balfour
Copyright © 2000 by Western Cape Linux Users Group
- Dedication
- To all those web programmers forced to use ASP.
Preface
PHP is a very popular rapid application development (RAD) language for writing programs for the world wide web. This talk aims to give a brief glimps of what is possible with dynamic programming environment.
This presentation was made at the Western Cape Linux Users Group General Meeting on Tuesday, 25 July 2000, in Lecture Theatre 2, Chemical Engineering Building at the University of Cape Town.
Introduction
PHP is a rapid application development (RAD) programming language for the web. Its goal is to provide a language that allows web developers to develop applications very quickly.
As a RAD language PHP has the following features:
- It is intepreted when it is, rather than compiled to binary. This has the advantage of being quick to test (no need to code, compiiiiiile, run, code...) but it has the disadvantage of being slower to execute (because the code has to run through an interpreter).
- It is loosely typed so variable types do not need to be define. PHP defines them based on the data you put in them. There is no need to worry about changing an integer to a string.
- It has functions for just about everything under the sun. Write code is basically using existing function calls.
PHP has a syntax that is a combination between perl and C. The language itself, being a RAD language is more like perl.
PHP can be used to write different types of code:
- Bad
- long rambling sequential code. Difficult to read but quick to write.
- Good
- nice module code that puts everything in functions which are called in the main body of the code. Easy to read.
- New-Age
- abstract code that defines stuff in classes and and uses the methods and properties of these classes in the main body of the code. Easy to read.
The examples here probably fall into the category of bad.
Part of what makes PHP such a popular RAD language is that it has built-in support for pretty much everything.
- Accessing remote datasources:
- http, but currently no proxy support.
- ftp
- Databases: MySQL, postgres, Microsoft SQL Server,Informix, Interbase, Oracle, any ODBC, but getting linux ODBC support might be little tricky.
- Mail: POP, IMAP
- Calendar: MCAL
- Files: text, xml
- SNMP
- Encryption
- Regular expressions
PHP is now in its 4th generation, PHP4. PHP4 uses the zend engine as its core, is much faster, provides more builtin functions and is much easier to extend.
These popular sites use PHP:
These sites are excellent sources of PHP information:
- PHP documentation. Probably the best set of online language reference I ever seen.
- PHP builder. A reference site for PHP developers. If you're thinking doing using some PHP functionality such as using the calendaring code, see if anybody at PHP builder was written a brief tutorial.
Examples
show.php
<?
// CLUG PHP Presentation by Craig Balfour: Simple Address Book
// Tue Jul 25 17:23:21 SAST 2000
// Read address records out of the database and display them.
// Some variables; things that can be defined as variables should be; more
// module code!
$DATABASE="address";
$DBHOST="localhost";
$DBUSER="username";
$DBPASSWORD="passwd";
// Establish a persistent connectionto the database server or display error
mysql_pconnect($DBHOST,$DBUSER,$DBPASSWORD) or
die("Unable to connect to server");
// Establish a connection to the appropriate database or display error
mysql_select_db($DATABASE) or
die("Unable to connect to database");
// Setup our SQL query to get the data we want from the database
$SQLquery = "SELECT * FROM address";
// Execute the SQL query and return a pointer to the results of the query
// or return an error
$rp = mysql_query($SQLquery) or die("Invalid query");
// Print out the number of rows returned by our query. This is very useful
// for debugging
echo(printf("<P>Our query returned %s rows", mysql_num_rows($rp)));
// Run the following loop while there is still to return.
// While is simpler than using a for loop
// mysql_fetch_array will fetch results from the results pointer row by
// row until all the results are returned.
// Results are put into a nice associate array referenced by field name
// - much nicer than mysql_fetch_row
while ($row = mysql_fetch_array($rp)) {
// Get the fields of the result out of the associative array and
// display it on the screen
echo(printf("<P>Surname: %s", $row['surname']) );
echo(printf("<P>First Name: %s", $row['first_name']) );
echo(printf("<P>Email: %s", $row['email']) );
}
?>
edit.php
<?
// Variables -- it's good to put stuff in variables
$DATABASE="address";
$DBHOST="localhost";
$DBUSER="username";
$DBPASSWORD="pass";
// If the variable operation is not set to add then display the form
if ($operation != "add") {
// A HTML form
?>
<FORM METHOD="POST">
Surname: <INPUT TYPE="TEXT" NAME="surname"><BR>
First Name: <INPUT TYPE="TEXT" NAME="first_name"><BR>
Email: <INPUT TYPE="TEST" NAME="email"><BR>
<INPUT TYPE="HIDDEN" NAME="operation" VALUE="add"><BR>
<INPUT TYPE="SUBMIT" VALUE="Save">
</FORM>
<?
// End of HTML form
// If operation variable is set to add then we assume user has already
// submitted form, so we try and write data to the database.
// Form variables are automatically converted to PHP variables, no work
// required.
} else {
// First try and see if the fields are valid, we don't want to write
// bad data into the database. All fields must contains data.
// Here we get to do some case insensitive regular expression stuff!
if ( (eregi_replace(" ","",$surname) == "") OR
(eregi_replace(" ","",$first_name) == "") OR
(eregi_replace(" ","",$email) == "")) {
// If all three fields don't contain data then we display a message
// Data does not go into the database!!
echo ("<P>Sorry, all fields must be completed!");
} else {
// If everything in the three fields looks OK, we write it setup a
// connection to the database and write the data in.
// Establish connection to server, or complain
mysql_pconnect($DBHOST,$DBUSER,$DBPASSWORD) or
die ("Unable to connect to server.");
// Establish connection to database or complain
mysql_select_db($DATABASE) or
die ("Unable to open database");
// Prepare the SQL query. This one must add data to database
$SQLquery = sprintf("INSERT INTO address(surname,first_name,email) "
. "VALUES('%s','%s','%s')",
$surname, $first_name, $email);
// Display the query, good for debugging
echo(printf("<P>%s", $SQLquery));
// Execute the SQL query; if it failed return code will be FALSE.
$rc = mysql_query($SQLquery) or die("Invalid query");
if ($rc) {
// If return code is positive, we know the update worked, so display
// nice friendly success message.
echo ("<P>Updated successfully.");
} else {
// If return code is negative, we know the update failed for some
// reason. Display friendly message.
echo ("<P>Update failed.");
}
}
}
?>
