Level: All users
Weather byYou open with this article, a brand new section for anyone wanting to push developement of its personnal weather site a little farther
PHP is the easy to learn, easy to understand web language. It has also the advantage to be included in the vast majority of today hosting packages. In this section, we will presume that you have at minimum Php version 5.0 or newer (If not, talk to your provider's representative, often they offer a Php version selector).
In its most basic use, PHP work in a similar way to Cumulus. In fact, analogy to Cumulus "Webtags" are evident because PHP use "variables" to be replaced by real informations before sending the page to the visitor. Exactly the same way as Cumulus goes in its page processing.
If we had to promote Php to newcomers, our main argument will probably be: with PHP, you'll never have "The white page syndrome!" Effectively, if you begin with Php, you could produce you first page in only 3 seconds! Take simply one of your .html page and save it with the .php extension... that's it! You have your very first PHP page!
Your page will be processed by web server as a regular PHP page... but will find no PHP command to execute inside this file, then will simply display it as a regular html file.
As you just seen above, PHP pages start often with a standard html file in which we add some PHP commands to add a bit of "life" into that page!
A html file is finally like a page of a book, once its writed, its content never change. If you want to change something in your html page you have to modify it and publish it once again in your website. If we want a page that change frequently as a weather page, we have to include a "mechanism" that will change the content for us at regular interval... as Cumulus do with your weather pages!
If you're now about to consider PHP to enhance your personnal weather site, you probably already know Cumulus webtags. Cumulus upload web pages to your website that has been previously processed ro simply replace Cumulus webtags by real weather informations.
If we take one of our childhood souvenir to illustrate this mechanism, remember these famous "dictated holed" which our language teachers give us at school. These little text where some _ _ _ _ _ were retired from text and replaced by dashes. These dash was processed by our schoolboy brain as a command to execute: Replace the hole with a word equal lenght to the number of dash.
Then in its most basic function PHP, like Cumulus, work this way. Like schoolboy, it take a given text and search for a "known sign" ("_" for our schoolboy) and replace it by an information that have to be found (for schoolboy _ _ _ _ _ = Find 5 letters word)
When Cumulus scan web pages, it search for "known signs" to replace weather informations read from your station. It search these signs <#...> indicating that the "special word" between these signs (the Cumulus webtag) has to be replaced by the value that it represent.
<p>Outdoor temperature is curently <#temp><#tempunit></p>
When Cumulus see the "known sign" <#...>, it interpret its content, temp, as a special word (a variable) to replace. Then Internet user see the final result like this:
<p>Outdoor temperature is curently 24,6°C</p>
In your "weather Pc", Cumulus monitor your weather station in real time and send new web pages at regular interval (eg. 15 minutes). Internet visitors are all reading the same page, until the next update.
On its side PHP works a little like Cumulus as we said before, by replacing "special words" with pertinent information, but it does it in real time on your provider server (or WAMP). Each time a page is read, it is processed by the web server to insert most recent informations. Therefore, in the rigor, page displayed could be different from page displayed 10 seconds before!
Counter to Cumulus who only do replacement of special words with information already prepared, PHP could read many words and launch some actions to calculate an information to display. PHP search 2 "known signs" indicating that things between these signs are "PHP commands" to process.
Then the sign <php indicate where is starting the PHP code, while ? show where is the end of a PHP code. The range of possible PHP codes to insert is vast and could count only a few words, entire pages or use external files or databases.
If we take a weather website powered by Cumulus as example. It could be interesting to display current date and time in heading of your page. Knowing Cumulus webtags a lot we can do it easily by adding the webtag <#time> at needed place, but time will stay the same until the next Cumulus update sent to your website!
With PHP, remember that it write result of an action in our page at the wanted place. To do so, PHP provide a vast array of "special words", like the command "echo" which means "write" and the function date() displaying dates, according to the arguments specified in parentheses.
For now just, keep this in mind:
- A command do an action
- A parameter guide this action
Then, put that together with our date!
We will tell PHP to insert date, in a particular format, in our page:
<?php echo date('H:i:s, l d-m-Y'); ?>
If we analyse our commands:
|<?php...?>||Indicate that this section has to be processed by PHP.|
|echo||Display result of the date() function.|
|date()||Return date in format specified by arguments (if any).|
|H:i:s||Hour, minutes, seconds, separated by ":"|
|,l||Display a comma followed by the day name.|
|d-m-Y||Display day, month and year on 4 positions, separated by "-".|
These command should display something like that: 07:49:06, Saturday 13-10-2012
As usual, PHP page has to be executed on a web server to display the date() function result.
Voilà! You're now initiated to PHP! With this tiny date() example, you have learned how to convert a regular html page to a PHP page and you have inserted a comman to display date and time in the desired format. I invite you to play with date arguments as we have often to insert a date, for more informations go to that link: Date arguments in PHP Manual
If this article has giving you some "craving" to do more... stay tuned for the next article: Cumulus and PHP!