![]() |
A Crash Course in PHPfor skins. |
Intro | PHP
APIs: Playlist | Playback | Collection |
Skins are implemented in a very well-known and successful language called PHP. PHP is like Perl minus the ability to write illegible gibberish that nevertheless works. For people who aren't skilled programmers, the language is very easy to read and modify. As your skill deepens, you can teach yourself more of the language.
<html><head><title> My Title </title></head><body> Hello! </body></html>You can embed PHP code within <? and ?> tags.
<html><head><title> My Title </title></head><body> <? echo "Hello, World!"; ?> </body></html>PHP has variables like integers, floating point, arrays, and hashes:
<? $x = 2; $pi = 3.14159265359 * $x; $greeting = "Hi there!"; $money[0]=5; $money[1]=20.5; $ceo['Sun'] = 'Scott McNealy'; $ceo['Be'] = 'Jean-Louis Gassee'; # this is a comment # ' and " can be used as string delimiters. # The contents of " strings is evaluated for variables: $count = 7; $good = "Snow White and the $count dwarves"; # $count gets replaced by 7 $bad = 'Snow White and the $count dwarves'; # $count is untouched ?>PHP has the usual control structures plus some others you might not have seen before:
<? if ($expenses > $assets) { go_bankrupt(); } for ($i=0; $i<10; $i++) { print $i; } while (inventory_left()) { sell_inventory(); } $ceo['Sun'] = 'Scott McNealy'; $ceo['Be'] = 'Jean-Louis Gassee'; foreach ($ceo AS $company=>$person) { print "$person is in charge of $company"; } ?>
<form action="next-page.html" method=get> What's your first name? <input type=text name="firstname" size=20> <input type=submit> </form>When the user clicks on the "Submit" button, the web server will load and run "next-page.html". The form variable firstname will show up automatically in that script as the variable $firstname:
<? print "Hello, $firstname"; ?>If you were to look at your URL bar once you arrived on next-page.html, you would see something like http://mycomputer/next-page.html?firstname=Steve. The web server takes everything after the ? and calls it the "query string". This is one way variables can be communicated between pages. For instance, you could use the following code in first-page.html to pass the information on to a third page:
<a href="another-page.html?firstname=<? echo $firstname ?>"> Click here for another page. </a>This would produce a link like another-page.html?firstname=Steve. Remember, you're allowed to pop into PHP mode even in the middle of some HTML.
Another technique involves passing information around using hidden variables in forms:
<form action="last-page.html"> <input type=hidden name="firstname" value="<? echo $firstname ?>"> What's your last name? <input type=text name="lastname"> </form>Which can be read in last-page.html with:
<? print "Hello $firstname $lastname"; ?>This technique lets you build up information through a series of forms, rather than having to make one massive one.