DevtheWeb.NET

for Those Who Develop Something on the Web

Things You Probably Didn’t Know About PHP – Part 1

Date: 12 Jan 2010 Comments: 16 so far

I’ve spend some time reading about cool things in PHP. I found some interesting functions and tricks. Here’s the list:

1. You can compress/decompress long strings before storing them in a database. It can be done very easy using the built-in functions: gzcompress() and gzuncompress(). They use gzip algorithm and could compress a string up to 90%.

2. You can check if a email is valid by the checkdnsrr() function. It checks the email’s host address if it’s a valid DNS record.

For ex. tihomir@devtheweb12782.com is semantically a valid e-mail, but devtheweb12782.com is not a valid host. That’s why checkdnsrr() will return false.

3. Output formatted PHP code, you can do it by using the highlight_file() function. This function will return a HTML formatted string with nicely colored PHP inside it.

4. You don’t need to store IP addresses as strings.

They can be stored as integers using the ip2long(). The integer converted IP addresses can be converted again to string using the long2ip() function. Storing IP address as integers has some advantages:

  • reduce storage space – 4 bytes instead of 15 characters (15 bytes)
  • searching by IP address will be faster
  • easiest to check if IP address falls within IP ranges

5. Easily unpack numeric arrays using the well-know function … list :) Look at following code for swapping two variables’ values:

$a = ‘value 1′;
$b = ‘value 2′;

list($a, $b) = array($b, $a);

echo $a; // it will output value 2
echo $b; // it will output value 1

Have you ever seen better code for swapping two variables’ values !?

6. You can use composition of variables. I don’t how it can be useful, but the following code is valid:

${‘a’ . ‘b’} = ‘c';
echo $ab; // it will output c

7. You can handle the situation when a class that doesn’t exist is instantiated – just need to implement the __autoload function. This function is called transparently by PHP when a class that doesn’t exist is instantiated.

8. Split string to array of strings using preg_split function. It can be done using the explode function. But using preg_split function, the empty strings will be eliminated:

$str = “a,b,,c,,d,,e”;
$strArr = preg_split(“~,~”, $str, -1, PREG_SPLIT_NO_EMPTY); // it will returns array(‘a’,’b’,’c’,’d’, ‘e’)

I hope you’ve found something useful in the examples above :)

P.S. You can check out also: Things You Probably Didn’t Know About PHP – Part 2

  1. 16 Comments to “Things You Probably Didn’t Know About PHP – Part 1”

    1. Eric TF Bat says:

      A couple of errors: “will outputs” should be “will output”, “charactes” should be “characters” and “addres” should be “address”. Run a spell check over your article before posting!

      Also, your comment about the swap method being so simple marks you as a relative newbie. Python’s syntax for the same thing is nothing more than “a,b = b,a” and other languages have similar simple forms. Certainly it’s better than the syntax in C, C++, C# and Java, but that’s nothing new!

    2. Steve says:

      #5 “Have you ever seen better code for swapping two variables’ values !?”

      Yes…

      a=1
      b=2
      a, b = b, a

      (Python, Ruby, probably some more)

    3. Ken Snyder says:

      Cool stuff! Thanks

    4. sapphirecat says:

      I’d strongly discourage using ip2long. Storing the ipv4 address of a 6to4 router won’t be useful once a certain fraction of the internet goes IPv6.

      For autoloading, I recommend spl_autoload. It doesn’t conflict if your script wants to use two packages with different autoload conventions, and it protects you from someone else using spl_autoload and accidentally hiding your __autoload.

    5. tihomir_wwf says:

      First, Thanks for all replies!

      Eric TF Bat, thanks for correcting my errors. English is not my native language, but I promise to learn it more or at least always to run spell check before posting :)

      Steve, Eric TF Bat you’re right about #5. I should read more about other languages before claiming that something is best :)

    6. Andrew Ferk says:

      6. You can use composition of variables. I don’t how it can be useful, but the following code is valid:

      ${‘a’ . ‘b’} = ‘c’;
      echo $ab; // it will output c
      ^^^^^
      I know where this is useful, when using SimpleXML. If there is an xml tag , you can’t access it in PHP using $xml->list-item, instead you must use $xml->{‘list-item’}.

    7. Ythan says:

      > Have you ever seen better code for swapping two variables’ values !?

      I’ve always liked:
      $a ^= $b ^= $a ^= $b;

      :)

    8. Clay Borne says:

      Great Post. It’s amazing how versatile PHP is. The amount of libraries is also a great resource.

    9. monk.e.boy says:

      $class_name = function_to_get_class_name();
      $my_class = new $class_name();

      Zend_Framework does this a lot.

      monk.e.boy

    10. Number 2 is slightly misleading. It checks that the domain, IP, etc., resolve correctly (the proper records exist). It does not, however, confirm that an email address is in use.

      Furthermore, you cannot use “user@*” in the string passed to checkdnsrr().

    11. Frost says:

      @#1
      How do you expect to perform a search on your data through MySQL if you have it all compressed in gZip format?

    12. chase says:

      The last one could also be done like so:

      array_filter(explode(“,”,$string));

      I know preg_split is more expensive than explode, not sure if combining it with array_filter is going to change that though.

      be careful though, as written, it will also remove anything that evaluates to false. You can write your own callback to just remove empty strings though.

    13. Good informative article…

      Thanks for the stuff.

    14. Ben says:

      “You can compress/decompress long strings before storing them in a database”

      Bravo! So when it comes to query these records and use the gzip-compressed field in a WHERE clause…. you would go about doing that how?

      This sort of inanity is the reason PHP regrettably cops the reputation of being a second rate, amateur-hour language.

    15. Sebastian Wilson says:

      Ben, PHP is not responsible for your lack of imagination. You don’t always need to search within a field on a database… by the way, it was just an example!

      And you must be very ignorant to say something like that, unless you belive that “tougher is better” always

    16. r2d2 xbox says:

      I don’t play golf, but I swear to God? if I can get some friends to come I’m going to recreate this at the local course.

    Leave a Reply