My Pages

Saturday 9 April 2011

Server-Side - Introduction to PHP


Introduction

Last week we started to discuss server-side technologies specifically services which when working together provide the necessary building blocks to create a web application. We installed and configured XAMP which is one such tool which incorporates Apache web server to publish web pages, PHP to script logic embedded into web pages and MySQL to store the data generated by the application. This week’s main focus will be server-side scripting which involves the creation of scripts which execute on the server to ;
  • Generate dynamic web pages
  • Interface to the database or other data sources
The need for dynamic web pages has been with us for quite some time and in the early days this was handled from server-side by developing C programs, Perl or Shell script using CGI (Common Gateway Interface). These programs were executed by the operating system of the web server as opposed to a server-side language such as PHP which is interpreted by the web server.

There are numerous interpreted and compiled languages which are used to create server-side code, such as;
  • CGI / Perl
  • Cold Fusion
  • C Server Pages
  • ASP (Classic)
  • ASP.NET
  • Java Server Pages
  • Python
  • Ruby on Rails
  • PHP
  • Cache Server Pages
These languages were created and then improved over time as new versions were released to adhere to emerging requirements in web applications.

Throughout this blog our main focus will be on the PHP scripting language which stands for PHP Hypertext Preprocessor. PHP is a free scripting language which means that it’s source code can be customisable if need be. Platform independence makes PHP a very flexible scripting language . PHP is also considered as a glue language since it can be used to connect and translate software components.

Three components are needed for PHP to work;
  • PHP parser which is a program usually in the form of CGI or server modules which interpret code.
  • Web Browser
  • Web Server
When a user asks for a web page which is scripted in PHP the PHP file on the web server is located and passed through the interpreter which executes the php code. The result is displayed on the user web browser. A PHP file is created by creating a file with a .php extensions. PHP code is written between and open tag <?php and a close tag ?>. PHP is a loosely typed scripting language which means that a variable’s data type is inferd during execution and variable do not need to be declared before used. In style PHP is very similar to C based languages and there are various ways in which the same code can be developed.

Task Summary


This week’s lecture focused on the basics of PHP syntax and constructs and some built in functions. Throughout this blog entry the following tasks will be demonstrated;
  • Verify that PHP is working on the web server
  • Create an associative array of user names and passwords and list the entire array in a table
  • Difference between the echo() and print() functions


Testing PHP on web server


Testing PHP on a web server requires one to create a simple syntax in a PHP file, place the file in a directory which is visible to the end user and then browse for the file. In this case I used an echo function which either prints a literal or a value passed from a previously assigned variable to the screen. To achive this first I created a php file and placed it in a folder within the document root of the apache web server;

Path to file within the document root:
\GregDevPortal\PHPIntro\phpIntro.php

Within the created file I placed an echo function and a literal to print on screen. 




If the PHP module is not installed then an error would pop up on the screen. Since PHP is installed on this machine the following result was displayed.





Create an associative array of user names and passwords and list entire array in a table



PHP is very versatile when it comes to arrays. An array in PHP can have one from three forms;
  • Numeric Array: This array is the conventional array which uses numerical indices
  • Associative array: Each key within this array is associated with a value.
  • Multidimensional array: An array with contains more than one array


An array in php is initialised like any other variable in php such as $arr. In the case of an associative array the array is then filled with items in one of two methods:

Method 1



Method 2



Creating an array of user names each with its respective password like shown in the methods above takes care of the first part of this task. After creating the array we need to iterate through the array and print the user name and password. The user name and password will be shown in a tabular format and this can be achieved by using one of the PHP printing functions print() or echo().

By using either one of the functions HTML can be written on screen while iterating through the array to get the details. In this case with each iteration an html table row will be creating wrapping the user name in the first cell and the password in the second cell. Once the array is created the array needs to be iterated and sent as HTML to the browser. PHP is very rich when it comes to traversing arrays. In this case since we have an associative array we need both the key and the value from the array. We will traverse this array by creating a foreach loop and with each iteration of the $usrArr we will be creating two variables whose scope would be code block within the loop. By using the associative operator the $key variable will hold the user name while the $value will hold the password.





With each iteration the variables are included in the string literal, the PHP parser recognises the variables and the PHP interpreter prints the respective values for each.The end result would be like shown in the image below.




Difference between the echo() and print constructs


Both of these constructs are used by PHP developers to output data to screen. There are slight differences between the two functions and using one or the other comes down to personal preference most of the times. Both of these language constructs are not considered as functions since parenthesis are not necessary when coding.

One difference between the print and the echo constructs is when it comes to the outcome of the function. The print construct can return a boolean value which can be particularly helpful when a developer wants a status at a certain stage during an execution of a script. Even though print is not considered as a function it can behave like one. 




Print’s execution time is slightly slower when compared with than that of echo. This was tested by iterating through a large array and time execution time for each construct separately. 



The echo construct is especially faster when using a comma to parametrise strings instead of a period to concatenate strings together.




Conclusion



PHP is fast, flexible and relatively easier to learn when compared with other compiled languages such as Java or C#. One should also take note on pitfalls that could be encountered when coding with PHP. Due to the versatility and flexibility that PHP provides it’s very easy to write bad code with PHP thus I find that keeping up with standards and coding patterns help me in developing secure and performant applications especially with PHP.


No comments:

Post a Comment