#native_company# #native_desc#

The ABC’s of PHP – Part 10 – The Final Installment Page 3

By PHP Builder Staff
on June 2, 2009

PHP 4 introduced a foreach construct, much like
Perl and some other languages. This gives an easy way to
iterate over arrays. Foreach works only on
arrays, and will issue an error when you try to use it on a
variable with a different data type or an uninitialized
variable. There are two syntaxes; the second is a minor but
useful extension of the first:

foreach (array_expression as $value)
foreach (array_expression as $key => $value)

Basically, from PHP5 onward, you can use the second version
with ‘$key => $value‘ so instead of your loop
variable getting the value of the variable, it gets the name
of the array key, or the bit that sits inside the [ &
brackets when you create an array.
This got me thinking, and since I discovered that you can
also use spaces in array identifiers, I realized I could
store style rules in a normal array, then insert them into
the code using a loop like the following:

  foreach($styles as $key => $style)
    print " ." . $key . $styles[$key] . "n";

Which you can see round about line 111 in the script
attached to this article.
This opens up some interesting possibilities to generate
style sheets on the fly using database information and PHP
includes, which I’m going to explore at a later date, and
maybe even write an article on PHP builder about it.
The rest of the script are just a mass of print statements
that output the required HTML tags to create the page, and a
main loop that goes over the story’s array printing each
story one at a time, all pretty easy to understand.
I hope you all enjoyed this series, I would have loved to
keep going with it and moved onto more advanced things, such
as databases, and classes and a whole host of other things.
Unfortunately time constraints and other work won’t afford
me the time to do so.
I am a member on the PHPBuilder forums under the name
‘shawty’ so feel free to send me a PM or look out for my
posts in there. I do actively participate in the forum when
I have a few minutes here and there, I can also be found on
linked in at:http://www
. I also have an MSN live
spaces blog at: http://cid-
Where I jot down
little snippets of technical goodness and other geeky type
stuff, feel free to drop me a visit and leave your mark in
my guest book.
Please remember, programming is not about just creating
code, or about doing as other have done before, it’s a whole
art-form in itself. I made my first leap into this voyage of
discovery way back when I was just 7 or 8 years old, and
even now I still learn new things every day, the proof in
this is above where I discovered an extension to the
‘foreach’ command.
Always try to think outside the box, don’t be tied in to the
constrains of what something can do, because that’s what
it’s supposed to do, I’ve used PHP for some pretty bizarre
stuff over the years and developed tricks that you’ll never
find listed in any text books, and if you try something, and
it fails???? So what???? so you failed???? big deal, re-think what
you where attempting and learn from your mistakes, because
next time it’ll help you make more creative screw ups,
believe me ;-).
The biggest thing though?? is “Have fun doing what you do”
because when it’s fun, it makes the process all the more
enjoyable, and programming should always be fun.

Happy PHPing??