I've been ranting about PHP quite a bit, demonstrating some of the ways I've used it in mashups and applications. I love it, because it's a great glue for ramming different bitz 'n' bobs into each other using the mimimum fuss.
I seem to have vomited scripts a bit too much, and thought it would be a good idea to get back to basics for anyone who doesn't know PHP but fancies learning - 10 simple things about it:
1 -- Variables! Put a dollar sign in front of any word. You'll need lots to hold all the data, e.g. $day for what day it is, $xml to hold an xml string, $salesLastWeek for.. you get the idea..
2 -- Arrays are variables that can hold lots of data - for example, an array $week which has seven values, the days of the week. So $week[0] is 'Sunday', $week[1] is 'Monday', or whatever you want really.
3 -- IF. If this, do that. If this, do that, if not, then if these, those, otherwise die. Or rather,
if($this == $everything)
$answer = that($it);
else if ($these == $those)
$answer = "those";
else
die();
or something along those lines...
4 -- Say Hello! echo makes stuff come out the other end. echo "Hello World!";
5 -- Instead of IF you can SWITCH.
switch ($today) {
case 'Saturday'
$txt = "I told those fucks down at the league office
a thousand times that I don't roll on Shabbos! ";
break;
case 'Friday'
$ txt = "cause it's Friday; you ain't got no job... and you ain't got shit to do.";
break;
default
$txt = "I can't think of a funny quote.";
}
6 -- So what day is it? idate gives you the bits you need. So:
echo(idate("Y"));
gives you the current year ie. 2009, and use a w intead of Y to get the day of the week. Sunday = 0 etc.
7 -- Make some IFS.
Greater than: if ($it > 45) { do_it($it); }
Or: if ($one > 1 || $two == 3) (do_something($one, $two); }
And is &&. Not is !, as in if (!$it > 45), otherwise known as <= 45.
8. Do Something. That was a function. You can tidy up your code by making bits of it functions. Particularily bits you use a lot or things that feel seperate.
function do_something($one, $two) {
$one.$two+12;
}
There. Now we can "do something" with $one and $two. The dot makes the variables write next to each other. So if $one = 1, and $two = 2, we get 12+12 = 24 when we say:
do something($one, $two);
9 -- SimpleXML. I work with data, and lots of data is XML. $xml = simplexml_load_file($file);
10 -- FOREACH - one of my favourite words. You can loop through each item in an array, or each node in an XML file. Check the link, as always w3schools is an excellent source for basic PHP reference.
There you have it! Did anyone learn anything? Let me know!
--CovertHolistic
Thursday, May 7
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment