Thursday, 7 April 2011

Week 7 - The first shot with PHP

Introduction 
This weeks lesson was about some basic PHP. I did some deep research before starting to help understand more what was explained in class and started this weeks task.

What is PHP?
This is a server side and an HTML-embedded scripting language for creating dynamic Web Pages,it contains certain syntax which was borrowed from other languages like C, Java and Perl together with is unique features. PHP allows web developers to write pages quickly that are dynamic. PHP is an open source and cross platform it runs on Windows NT and many Unix versions and it can also be built as an Apache module.

How does it work?
When the PHP webpage is visited by someone,  the server processed the PHP code and checks out which part needs to be shown to the visitor, like the content and also the pictures. The other parts like file operation and some math calculations are hidden, it then translates PHP into HTM and sends the webpage to the visitors web browser. 

Some PHP Advantages and Usage
     ·         Reduces time when creating a large website
     ·         Open up thousands of possibilities for online tools.
     ·         Allow creation of shopping carts for e-commerce websites.
     ·         Allows the addition of connecting to databases within a website
     ·         Send HTTP headers and set cookies and redirect users
     ·         Integration with various libraries that let you generate PDF document and also parse XML

When coding PHP there is no need for separate files or anything as this goes directly in your HTML document this is done by making use of the PHP tags <?php……<?> the PHP engine processes whatever is between those tags. One though can do a separate file and then can include it using the include command ( <?php include(“calc.php”); ?> )  Also like other languages it is good to keep code clean and readable by making use of whitespaces and also comments. Like some other languages in PHP you don’t have to declare variables prior to using them. This language feature is called being ‘loosely typed’. As in other programing languages in PHP one can also find loops, if..else, while, switch case and for loop. Like any other language PHP also handles arrays. In PHP one can find 3 different kind of arrays which are the following: 
  • Numeric Array - Array with numeric index
$person[0] = "Billy";
$person[1] = "Lucy";
echo  $person [0] . "and" . $person[1]. " are my best mates.";

In this type of array you make use of the key and value array. The keys are the numbers used to specify in the array while the values are the names of the person. Each key represents a value. To display the result then one uses the echo, you get the value of key 1 and key 2 and are displayed. 
  • Associative Array - Array where each ID key is associated with a value
In the previous example we had an integer as a key but one can also have a string as a key.  In this type of array the key is associated with a value. 

$age = array("Billy"=>28, "Janne"=>85, "Joe"=>82, "Lucy"=>40, "Trish"=>36);;
  • Multidimensional Array - Array having one or more arrays
In this type of array each elements in the main array can also be an array and even element in the sub-arrays can be an array. Therefore each type of element can be an array.

This weeks task
For this weeks task we had to:
  • Using your browser, verify that PHP is working on your web server.
  • Create an associative array of user names and passwords and list the entire array in a table.
  • Explain in your blog the difference between the echo() and print() functions.
Explanation of tasks
To make sure that PHP works on my browser I simple created a plain page and placed in some PHP code. 

This code here shows some variables that were
PHP Code for test
declared and assigned some values to them. Values are of different type some of string while others are numbers and one is a date. Then they are printed it out on the screen. Then I saved and check if it works from localhost and the result is as shown below.
PHP result for test
Next the array was created, it is made out of two columns one for the user's username and the other for the user's password. This was created using associative array type. Lines 9-14 show the table being created for the array to be placed within it. A <th> tag was used for the headers of each column. The array was created by first showing that now we will put php code with HTML using <?php...?>. The array was then declared as seen in line 16. A loop was needed to iterate through the array so they will be displayed.
Code for Array
Array Created in a table
 Next in line was to explain the difference between echo() and print(). Both echo() and print() are used to display the output of the user but when do you use echo() and when do you use print(). One main difference between echo() and print() is that echo() is abit faster. I found some code on a site where it tested the time taken for each of them. Echo() was executed in: 0.057446947097778 while print() took: 0.072533121109009. The reason behind this is because print function returns a statues where it determines if it was successful or not while echo() just prints the output.


<?php
$t1 = microtime(true);
system('C:/php5/php.exe C:/www/echo.php');
$t2 = microtime(true);
$r = $t2 - $t1;
echo 'echo: '. $r;

$t1 = microtime(true);
system('C:/php5/php.exe C:/www/print.php');
$t2 = microtime(true);
$r = $t2 - $t1;
echo 'print: '. $r;
?>

Print can then operate as a function  which can be used for more complex operations. If you try using the echo() as a function you will get an error as follows: Parse error: syntax error, unexpected T_ECHO in /www/testPage.php on line 4.

Also another difference is that echo()  can handle multiple parameters.

Conclusion
Looking back this task PHP is a reall useful language that helps interact with the user and allows the use of databases. I personally think that PHP is a good language and even the fact that it is loosely typed it is an advantage for the programmer and makes it easier. Also it isn't that hard to learn it just like other languages one needs to do hands on to learn.


References: 

No comments:

Post a Comment