PHP Programming

 

 

We're introducing programming programming for ebusiness with PHP in this class, where the textbook introduces it with JavaScript.  This lets us focus on database programming during the limited time available, and project 2 relates more to server-side scripting in the Linux, Apache, MySQL, PHP environment.  JavaScript programming in HTML and PHP are introduced with project 3 in a few weeks.

The text's best treatment of 'Structured Programming' is given in the chapters about JavaScript, 7 thru 12 and it would be a good idea to read thru these with particular attention about the 'general topics' related to programming languages: memory, arithmetic, decision making, control statements, functions, arrays and objects.   

It has a good, detailed introduction to Relational Databases and MySQL in Ch 22.  We're using different tables in class to show the relational model, and to make SQL queries that JOIN tables together to make reports.

PHP is covered in Ch 26.  It is one of today's most popular Open Source language for web programming and it works on both Unix/Linux and Windows web servers.  Other alternatives for server-side scripting are IBM's WebSphere, MicroSoft's ASP, or other languages like Perl or Python using the CGI (Common Gateway Interface) provided so that a server's http processes can communicate with application programs and databases.

 

'Structure' is an important topic when systems analysis and programming are involved.   A 'structured approach' means that programming statements are related using  three basic 'logical structures': sequence, alternative selections, and loops.  These are discussed below.

Structure is particularly important in designing the details of systems and scripts so that the correct actions at done at the correct time.  Structured design, and other documentation, are more important the larger and more complex a project becomes.  Any system involving more than one person for more than a week will benefit from using a 'System Development Methodology' that includes structured components.  

Building a system that takes several people several months or more without an appropriate methodology is the equivalent of building a large house or office building without drawings and plans.  Nobody will be happy when the budget has been expended, and that's bound to be well before the system is delivered.

One of the most popular conceptions of 'structured programming' is that it is 'programming without using the GOTO statement'.  It is a mostly true statement, but there's more to structure than than avoiding GOTO statements.  GOTO was/is often used inappropriately by programmers working with 'unstructured languages' and the result is often a script that is 'buggy' for its users and defies 'fixing' by a programmer.  

Older languages, like BASIC & FORTRAN, did not provide syntax for structured programming and an undisciplined programmer could cost an employer lots of money by writing code that could not be fixed or maintained as needed in the future.  We spent a lot of time teaching how to do structured programming with the GOTOs required in these languages.  Since the languages didn't provide clearly structured elements, it wasn't readily apparent how to go about even simple programming tasks in a Structured way. 'Spaghetti code' that is impossible to understand or maintain was often the result, and system projects by undisciplined teams were very likely to fail.

Today all modern programming languages are structured, and there are very few cases where a GOTO statement is appropriate.   

A 'structured language' like PHP, JavaScript, C++, VisualBasic.NET, or Python, makes it easy for a programmer to follow the discipline of structure.  An awareness of structure and a few other good  programming techniques, like clearly naming variables, add value to software by making scripts 'self documenting' and relatively easy to understand when they need to be maintained to accommodate changes in business rules or for other reasons.  

(Here is a spoof on this topic that may be more entertaining than books about _good_ programming techniques, and drives home many good points...)

Unfortunately, even a highly structured language doesn't _compel_ a programmer to use structured design techniques or write structured code.  This adds great cost to the maintenance of systems written without regard to structure, object orientation, and other standards.  There are a number of cases where organizations have gone bust because of expensive, bug-ridden software that doesn't meet requirements.

Use of a good 'structured design notation' is important for designing scripts and systems. The scripts provided here are very small (less than 100 lines) and written & formatted so that their structure is apparent.  But, 'real software' has scripts that are hundreds or thousands of lines long when 'real considerations' like editing data, securing access to web pages, or other data processing operations are encountered.  Software projects can cost millions of dollars and must be approached in a similar fashion as architectural projects.

Where sketches lead to building plans, or 'blue prints', in an architectural project, sketches lead to object oriented and structured designs in software projects.  I'll show some of these in class.  

Managers and Executives need to be aware of structured and object oriented techniques so they can recognize where they are missing in software they are considering for purchase or perhaps for replacement.  There are techniques for 'refactoring' and 're-engineering' software that may be more cost-effective than continually fixing and patching a poorly designed system, or buying an 'off the shelf' system.  Dr. Peter Aiken, on our faculty, has literally 'written the book' about re-engineering and practices it in the field.

 

Control Structures in programming languages

The clips on this page refer to the sample LinkToReports.html and a PHP script, DonationReports.php.  It would be a good idea to get these scripts in your editor now so you can refer to them.  The URLs used in the <a href= tags in the html file LinkToReports contain GET data after the ?, where the variable 'WhichReport' has it's value hard coded to indicate which report the user has clicked for.  DonationReports is a php script that expects WhichReport to contain one of the reports it's been programmed to provide.

Programming languages support Structure by including syntax to support structured alternative selections and loops.  All programming languages support 'sequences' naturally -- each programming statement (line) is executed sequentially unless one of the language's 'control structures' is encountered.  These control structures provide the other two logical structures of 'alternative selections' and 'loops'.  

Alternative selections and Loops are what allow a program to provide different results each time it runs.  These 'branching statements' allow the script to test conditional statements for logical true or false and take a 'non-sequential' path thru the script depending on the data encountered.

Most modern languages provide at least three types of structured alternative selections: if/then; if/then/else; and some form of if/then/elseif/else (commonly called case selection).  

if/then works where there is only one alternative, as might be the case when the DBMS is not available when a web browser requests a report.  In PHP the structure, with no 'white space' is:  

if (cond) {action; action;}  

if/then/else is used if there are only two alternatives: one taken if the condition tested is true; the other taken if the condition is false.  It looks like this: 

if (cond) {1st action if true; action; action;} else {action if false;} 

Look at how these logical structures are supported by PHP to provide a web user with the report they select.  In the sample script, this if/then control structure is used to control production of the reports.  

The links on LinkToReports.html include a 'GET variable and data' after a question mark (?) in the url for each <a href= link that points to DonationReports.php.  If the browser doesn't set $_GET["WhichReport"] a gentle error message is returned instead of a report.  Notice the URL displayed by Internet Explorer in the screenshot below, where the mouse-pointer (not shown) is hovering over the 'Donations by Region' link.

Slide1.JPG (35022 bytes)

Here is the HTML for this page.  The GET variable after the ? is WhichReport.  GET data are placed after an =.  Each link to DonationReports provides a value for WhichReport that the script will accomodate.  Note that quotes or special 'URL encoding' is not required if the GET data contains no spaces:

Slide2.JPG (37831 bytes)

DonationReports.php is the 'resource' referenced in the URL shown here.  Unlike an ordinary, static webpage, this page will be passed to PHP by the Apache server.  Any statements between 'get into PHP' and 'get out of PHP' tags (<? and ?>, respectively) get passed to PHP, and the output (mostly from 'print' statements) is substituted in the html.  

It's an ordinary thing for a PHP script for a page with 'dynamic content' to be made the way DonationReports.php is:  At the beginning of the file there is PHP code that puts stuff into variables that are shown to the user.  Here, the 'long' PHP script between the first <? and ?> makes two variables, $Links and $Report, that are used in the 'short' html section at the end.  When they're needed in the html, the programmer can 'jump into PHP' using the PHP tags in the html.  Here, these two PHP lines are 'embedded' in the html at the bottom:

<?
  print "$Report";
  print "<br><br>$Links";
?>

Let's look at the script's 'control structures' first, then look at the other syntax. 

Here's a part of the script that shows both if/then and if/then/else logic being discussed...

Slide3.JPG (56050 bytes)

If/then/else logic is used to control display of the report: IF the GET variable has not been set (!isset means 'Not Set') then $Report gets a terse message; ELSE the script goes on to attempt connecting to the database and look to see what's in $_GET["WhichReport"].  

Look at the script in your editor to see this whole structure.  There is only one statement executed, line 6, if (!isset$GET_["WhichReport"]) is true.  If it is 'NOT NOT isset' (two NOTs evaluate as TRUE) lines 8 thru 62 are executed.

Also, notice how the logical structures are emphasized in the script by accurate punctuation.  The 'controlled statements' are uniformly indented within the 'controlling statements, making it easier to see where logical structures begin and end.  

The if/then statements are associated with 'error handling'.  If a connection to MySQL cannot be established or the EBUS202 database is not available, there is nothing much that _can_ be done, so the 'then statements' print an error message and exit the PHP script prematurely. 

if/then/elseif/else is used when there are more than two alternative and the requirement is that only one of them should be executed.  Without white space it looks like this: 

if (cond1) {stmts1;} elseif (cond2) {stmts for 2;} else {stmts if not 1 and not 2;}

In the sample script, case logic is used to examine the GET data from the URL put in the variable  $WhichReport.  There is a section of code to provide each of the 4 reports that DonationReports.php is programmed to provide, and an error message is included after the 'else' statement to provide a response if for some reason the URL includes a value for WhichReport which is not accommodated by the script.

Slide4.JPG (87232 bytes)

Loops are the third of the structured logical structures.  Modern languages provide at least three types: while/loop; do/until loop; and for/next loop.  We'll look at the while loop first, since it's probably  most often appropriate.  Look to the text for other examples of loops.

Loops are usually used to process 'sequential data structures' like records in a file or results from a database query.  In PHP, it's convenient to process the results from an SQL query using a while loop that processes the statements inside itself as long as (while) there are rows in the result.

Here are a MySQL query and the results returned to the terminal:  

Slide5.JPG (30433 bytes)

In a script, the equivalent is returned to a 'result set' that the PHP script can 'see', one row at a time. 

$SQLStmt = "select MemberName, Amount from Donations order by Amount desc limit 10";
$TopTenResults = mysql_query($SQLStmt);

When the mysql query is executed, there will be one 'row' in the result for each record selected from the table.  This particular SQL statement limits the results to 10 rows since we're interested in seeing the Top Ten Donations if we click the link for this report.  

Here is a the general form of a while loop in PHP without any much white space:

while ($ARow = mysql_fetch_assoc($SomeResult)) { statements to handle $ARow;}

Here is the section of script that prepares the TopTenInTable report.  

Slide6.JPG (59027 bytes)

PHP uses mysql_fetch_assoc to get the 'next' row from $SomeResult and put it in the associative array $ARow.   When there are no more rows in the result set, PHP's control is transferred to the first statement _after_ the loop, where the table's end tag is placed on $Report.  In the loop, first the extract statement is used to put data from the 'associative array' $ARow into 'simple variables' named the same as the column names: $MemberName and $Amount.

 

Structure and Object Orientation are important in modern systems

We'll consider Object Orientation when we look at JavaScript.  For the time-being, we're exploring Structure -- Age before beauty, so to speak.

 

Syntax must be perfect.  This is one of the great woes of programming.

In PHP this means that every statement must be terminated with a semi-colon (;).  Every control structure's { and } must be perfectly matched.  Every left parenthesis ( ( ) must have a matching right parenthesis ( ) ).

Every 'verb' must be perfectly spelled, and if there are parentheses involved what's contained has got to be perfectly correct.  

 

Variables & Data Structures in programs

PHP makes it easy for programmers to recognize variables in scripts since the all begin with a dollar mark.  '$Report',  '$TopTenResults', and other variables in this script were 'invented' by its author, are not 'built into the language'.

'Simple Variables' contain one 'piece of data'.  'Data Structures' are often required since data often consist of many related pieces.  

Two data structures are considered in the sample script.  $ARow is an 'Associative Array' is used to hold the two 'pieces of data' (MemberName and Amount) that are in each row.  In PHP any string or number may be used to reference each element in an associative array.  $ARow["MemberName"] and $ARow[0] both refer to the same datum.  

If $ARow was an 'ordinary' array as found in many languages the elements could only be referenced using 'numeric subscripts' like 0 & 1 -- in PHP the first element in an array has a subscript of 0 (not 1 as in some other languages).

The other data structure encountered in this script is the 'result set' that is returned from the SQL queries.  $TopTenResults and $RegionResults are data structures that hold the rows and columns returned by an SQL query.  These 'collections' of rows can be processed sequentially using mysql_fetch_assoc to place each row in the result set into the associative array $ARow.

 

Data in Web Browsers -- GET and POST data

PHP provides 'super global variables' to hold data sent from browsers in GET and POST data.  GET data are usually contained in the URL sent to the browser with the GET request for a resource.  

POST data will be covered in the next topic, and are usually contained in 'FORM fields' where a web page has been designed so that the user can fill in fields on the page and submit it to the server for processing.

These super globals are easy to recognize since they look similar: $_GET (dollar underscore GET) is an 'associative array' that holds any GET data from the browser.  In this example, the only entry in the array should be $_GET["WhichReport"].

 

Database programming

Database programming is when the script accesses a database to report on it or to insert or update records.  In this course, most database programming will be to make reports.  If you're interested in updating a database let me know and I'll set you up with one where you can have insert & update privileges as well as the select-only privileges you enjoy in the EBUS202 database.

The sample script connects to the MySQL server & selects the EBUS202 database using mysql_connect() and mysql_select_db() statements.  Then, it uses mysql_query() to make a query to the DBMS and mysql_fetch_assoc to get each row from the result set into the associative array $ARow.  

 

PHP programming at info465.net

Apache parses files ending with .php and .html as it serves them up.  Not all servers will parse .html files.

'Register Globals' is set on, for the time-being.  If you're using this feature in your scripts, they won't run on a machine where Register Globals is set off, which is the modern thing to do.  With it set on, PHP automagically makes $Variables out of all the GET and POST data from a browser and an unaware programmer may introduce a security breach that may be exploited by a cracker.

'Non-fatal' Errors from PHP scripts are not displayed on the web page on this server.  They are logged in /var/log/httpd/error_log.  You can see them by typing on the command line: tail /var/log/httpd/error_log to display the last ten lines of this file.  If that's not enough for you to see your errors, as when there are others generating errors along with you, use the -n option to specify a number of lines to tail: tail -n100 /var/log/httpd/error_log will show the last 100 lines.  If you want to 'follow' the log, keep an extra session open and use the -f option to see lines as they are added to the tail of the file: tail -f /var/log/httpd/error_log.