Welcome

To my corner on the net... Warning, this is a techie blog! Non-techie people may suffer bouts of epilepsy on viewing this blog. The author cannot be held responsible.

PHP - Starting at the beginning

Tuesday, 5 April 2011

Browser History
Although I must admit I did not find the in depth information on browser history particularly interesting, I happen to be old enough to have used most of the versions mentioned. I go right back to when Netscape was still for sale !

Although I appreciate the evolution of browsers, I find it surprising that most of them fail to adhere to the standards faithfully, inevitably causing countless cross-browser issues.

Php
I had been looking forward to learning php. I have been using applications that are built with Php in my work as a web designer for a number of years and have many times modified scripts to be able to achieve the desired results. These changes were however limited to simple changes as I never had the time and opportunity to learn php in a formal accademic way.

The course material was enough to get me going. I was amazed how similar the php scripting language is to other languages and I look forward to practicing this new scripting language.

Checking if php is working 
The most obvious way for testing if php is working on my local server is to actually write some php code in a basic php file and test it in a browser. So, to start off I created the following file :



Once the above php file is displayed in the browser, the date is in fact shown which means that the php has been processed by the local php installation as shown in the screen shot below.

A little reading also revealed that a special instruction exists that will prompt php to display information about the php installation. The instruction is phpinfo(). The result of adding this instruction to the above script resulted in the following result on the browser. (only a portion of the result has been displayed here)





Creating an Associative Array

I actually spent a lot of hours working with arrays, mainly not only because they are the subject of this part of the coursework but also because I had to get my hands dirty with some php. The experience was quite interesting and I think I have really started getting to grips with the syntax.

I spent some time trying out simple arrays and listing their contents and I ended up with the code below. 



The code first created the associative array called $user and populates it with some data. The html defines a simple table. The for each loop is then listing the contents of the array. I spent quite some time searching for a way to list the key value of an array rather than its contents. I finally realised that the array_search function could be used easily.








Alternate Row colors 

I then decided that I wanted to try coloring the rows of the table alternately in two different colors. I decided to use embedded CMS to achieve this.


The code above achieved the result shown here. I added CSS styling in the header that will later be used to colour the rows of the table.

I added the variable i that will be incremented with each iteration of the foreach loop. I introduced I to have a number to check for odd or even and use the css classes accordingly. The DIV or % operator is used to check if division by 2 leaves a remainder. In the row number is even there will be no remainder and the darker green row is displayed.

Finally I also added a new column to display the row number which is of course the value of i.



Difference Between Echo and Print
After reading up about the subject there seem to be very minor differences between Echo and Print. Echo claims to be marginally faster because it does not return a value but again the performance advantage is negligible. Echo can also accept multiple parameters whereas Print does not. Example :

<?php
          echo "Item 1", "Item 2";
     ?>













0 comments:

Post a Comment