
Even after a few years of experience, I am still discovering more hidden powers in PHP every once in a while. If you have a look at some of my older posts, you’ll see a sort of learning curve happening. Currently I’m in the process of mastering a more OO approach, even if it’s just the database handling. There are, however, a few things I would like to share with you, things you might have been doing the long way before, and now find out that those 15 lines of code can easily be just 3 lines.
One nifty function I’ve found is letting PHP display PHP code on your website in color-coded glory. PHP’s highlight_string() or highlight_file() take as the argument a string or PHP file path respectively, and output the contents, completely formatted with colors. The following example has been produced using the highlight_string() function (note: the colors are in css style, and did not show up on this page during previews. I suppose a blog would need more code-insertion freedom for the full effect):
<?php //Showing Code: Color-coded!
echo 'Hello, World!'; highlight_string("The Code Here!");?>
For a website like WebGuruGuide, this function could be extremely useful, even if it means you are using it on your apache server, just to generate the HTML code.
Something else I find extremely useful is PHP’s built in XML handler, as opposed to the DOM I’ve been messing around with before. The function: simplexml_load_file($path_to_the_xml_file) could not be more straightforward. You simply create an object and assign the XML file to it.
<?php $xml = simplexml_load_file('http://ralphvandenberg.com/complete_rss.xml');?>
Then you can easily access the elements in the XML file like this:
<?php echo $xml->channel->title;?>
And you can loop through items in the file with a foreach() loop:
<?php foreach($xml->channel->item as $item) { echo $item->title.'<br />'.$item->description.'<br />'; }?>
I hope this makes everything much simpler for those who like to keep updated information, dynamically on their website.
Lastly I would like to simplify the file reading and writing. The tradition way was to define a file, open it and specify what we’d do to it (‘r’ for read, ‘a’ for append, or ‘w’ for write), then we’d actually do that, and close the file when we’re done with it. Trimming this down to only a few lines, for reading a file for example:
<?php $data = file('path/filename.txt');
//Each line of the file is an element in the array
foreach($data as $line) {
echo $line ; }
?>
This makes it very simple. Especially if you like to do things such as array_pop() or array_shift().
I hope this makes the programming for you in the future simpler. If you know more handy little ways to trim down on lines of code, make sure you stick some examples in the comments below!
Comments
PHP can do everything
It's almost pointless to point out something that PHP can do, because I think it can do everything. It would be more newsworthy if you could find something it couldn't do...
---Ralph van den Berg
visit RalphvandenBerg.com