July 15, 2006

5 Things You Probably Didn't Know About PHP

Posted at July 15, 2006 03:58 PM in PHP .

I have been programming PHP for a few years now. In that time, I have been pleasantly surprised to find some interesting features of PHP or some quirks that most do not follow. Here are my favorites.

The ?> is optional

Every PHP file you have seen created has probably been of the format


<?php
...
?>

Did you know that the trailing ?> is optional? Remove it and everything still works! I don't know when this behavior was introduced or if it has always been this way. It definitely works in PHP 5.1.

There is a good argument either way for leaving it in or out. However, I don't know how many times I have seen somebody edit a PHP file file, only to accidentally leave a blank line before (or more commonly) after these tags. This brings havoc whenever you output XML. For this reason, my coding practice is to leave it out.

You can embed other programming languages in PHP

Java, Perl, Python, and .NET are examples of languages you can interface with in PHP. Perhaps one of the coolest is the Java interface. Support for other languages can be found in PECL. I have never had a use for interfacing with another programming language directly (IMO it is better programming practice to just execute another process, query a web service, etc), but I could see the use for this cool feature.

You can use array access syntax on objects, and more

Using the Standard PHP Library (SPL), it is possible to make objects look like arrays. You can do something like


$object = new MyObject();
$object['name'] = 'Hello World';

and


$object = new MyObject();

foreach ($object as $k=>$v) {
echo "$k = $v\n";
}

The above example makes use of the ArrayObject interface, while the bottom example makes use of the plain Iterator interface. I find myself using the Iterator interface quite frequently, especially for things like database query results, LDAP directory queries, etc.

require is faster than require_once

(but only until PHP 5.2). Using require instead of require_once can result in a sizeable speed increase, especially in larger applications. The reason is that require_once makes an extra system call to validate that you haven't really included this file before. require just blindly includes a file. Some developers will say, "Then how do you prevent re-defining classes?" To them I say, "Why are you still using require or require_once in your library files? The __autoload function, when properly used, negates the need for require_once. If you have the SPL extension installed, you can use the spl_autoload_* functions to create multiple autoload functions! See a Zend Framework mailing list thread for more technical information about require vs require_once.

Anything less than 5.1.0 is silly

The PHP 5.0.x branch is not being maintained anymore. The PHP 4.x branch is all but dead. Why aren't you running 5.1.x? Here are some reasons why you should move to 5.1, besides the lack of work going into the old branches.
  • Your PHP 4 applications will still work. That stuff about upgrading will break your applications is mostly FUD. PHP developers went to great lengths to make sure PHP 4 applications will run just fine in PHP 5. Most popular applications are fully compatible with PHP 5. If you maintain your own PHP 4 application, just do some grep'ing for the incompatible changes listed in the PHP 4 to 5 migration guide.
  • PHP Data Objects (PDO) allows applications to interface with multiple database types easily as well as easily protect against SQL injection. There is no need to write separate handlers for mysql_query, mssql_query, etc. Just use PDO. And using statements to bind parameters couldn't be easier.
  • The SPL extension does wonders never before imaginable in PHP.
  • Some popular applications are making the transition. MediaWiki 1.7 requires PHP 5.
  • The PHP 5 OO model resembles other OO languages, like Java. Proper programming practice is difficult to maintain when object variables and methods don't have an access modifier (public, protected, private) in front of them. PHP 5 has these. PHP 5 also supports abstract classes, interfaces, and magic methods. If your project is using the object-oriented approach rather than the procedural approach and it is coded in PHP 4, you are doing yourself an injustice.

I put this in the list because many hosting providers still don't support PHP 5. Because the amount of programs written strictly for PHP 5.1 is the minority, the average reader probably doesn't use PHP 5, which highlights their ignorance of the obviously superior language.

So there you have it, my list of 5 things you probably didn't know about PHP. I hope you learned something. If not, you probably should have been coding instead of reading this article, for you could have been making good money as a PHP developer.

Trackback

You can ping this entry by using http://blog.case.edu/gps10/mt-tb.cgi/8815 .

Gregory Szorc postou no seu blog um artigo curto mais interessante sobre PHP. Ao ver o titulo do arquivo, “5 coisas que você provavelmente não sabe sobre PHP”, pensei: “lá vem, quero só ver o que ele vai falar”, mas me surpr...

Trackbacked from PHP5 - 5 curiosidades on Rafael Dohms.

Cela fait maintenant quelques ann?es que je programme en PHP. Pendant tout ce temps, j'ai ?t? agr?ablement surpris de trouver quelques caract?ristiques surprenantes ... Le ?> est optionel Chaque fichier PHP que vous cr?? est sans doute

Trackbacked from 5 choses que vous ne savez probablement pas en PHP on Antoine.

Comments

There are quirks about PDO that keep me from fully using it over the MySQLi extension. It is awesome, but I'm going to put off using it completely for my production code until I do some more testing and figure out what I'm doing wrong, if anything.

I would also say that anything less than 5.1.x is silly. However, it is difficult to keep upgrading the minor versions every time they come out. What is also frustrating is that there are some cool and awesome features in the latest that most hosts aren't going to offer until a few months after it is released. PHP 5.2.0 is going to be wicked awesome. Oh and I hear you brother, I only code in PHP 5 using Iterators.

You also didn't post about the PECL and soon to be part of PHP filter extension. That alone is going to make coding is PHP 5 and later when PHP 6 comes out worth the transition. Oh yeah, my mouth is just drooling over how much time the extension is going to save. It is procedural form however, but meh, it will make it easier for newbies to grasp and use it.

Posted by SantosJ at July 17, 2006 08:56 AM

Yes, the filter extension is definitely worth mentioning. I'm in love with the similar component in the Zend Framework. Makes it so much easier to never trust the input.

Posted by Gregory at July 17, 2006 12:06 PM

PHP's Java implementation is slow because it starts a JVM process with every call to a Java object. You're better off using the PHP/Java Bridge (http://php-java-bridge.sourceforge.net/), or Zend's Platform (http://www.zend.com/products/zend_platform), which also has a PHP/Java Bridge. These bridges start the JVM once, and use the same JVM for all subsequent calls.

Posted by Jay at July 17, 2006 12:46 PM

Unser Abonnement casinos versehen Sie mit Informationen auf casinos von vielen der f?hrenden Experten. casinos http://gratis-kasino-spiele.kde8.de [URL=http://gratis-kasino-spiele.kde8.de]casinos[/URL]

Posted by casinos - 1151933 at November 16, 2006 12:27 PM

"The ?> is optional"

I didn't know that... Just starting to learn php and that little tidbit is not mentioned in any of my manuals!

Thanks for the tip!

Lisa

Posted by lisa at December 17, 2006 03:17 PM

ucilgws rtdw qokxai knpb wjqxz qdlh rtpv

Posted by cobmzr xwoi at January 13, 2007 11:59 PM

Hello! I am Billy Johnson Nice design. Enjoy! Good site! OK. 0n79p7k .

Posted by billys at January 23, 2007 09:06 AM

Post a comment










Remember personal info?