Archive - October, 2009

PHP Boolean Type

The boolean data type in PHP is very easy to use (and why should it be difficult, it is either True or it is False).

The basic way to set a boolean value is to use the true and false keyword. Unlike other programming languages, true and false are case insensitive so you can either use true, True, TrUe, or any other variation to get the same effect.

For example:

<?
$yes = true;
$yes2 = True;

$no = false;
$no2 = False;
?>

These are all valid ways to set a variable to true or false.

PHP has some interesing ways of determining if a variable is true or false. Since PHP is a dynamically typed language, nearly any type can be evaulated as true or false. The way you can keep this straight in your head is if the variable’s value is at its default state, it will likely be evaulated as false.

To be more official, here is how PHP evaulates true or false from various values:

  • the boolean FALSE itself
  • the integer 0 (zero)
  • the float 0.0 (zero)
  • the empty string, and the string “0″
  • an array with zero elements
  • an object with zero member variables (PHP 4 only)
  • the special type NULL (including unset variables)
  • SimpleXML objects created from empty tags

Text taken from PHP manual at: http://www.php.net/manual/en/language.types.boolean.php

Ternary Operator – Your Best Friend

If you have never used the Ternary Operator, or ? operator, your life is about to change. The ternary operator is used as a shorthand for if .. else structures that can really clean up your code. Some may want to classify this under a intermediate or advanced skill, but it is so useful that I think it should be included with the most basic tutorials.

Take this for example:

<?
if( $use_live_server ) {
     $url = "http://www.thelivesite.com"
} else {
     $url = "http://www.thedevsite.com";
}
?>

This block of code sets which URL we want to connect to based on if a variable called $use_live_server. While the block of code is perfectly valid, there is a much better way to do the exact same thing in a single line of code.

<?
$url = $use_live_server ? "http://www.thelivesite.com" : "http://www.thedevsite.com";
?>

That one line of code does the exact same thing and once you understand how this works. Here is how the ternary operator works:

<?
$variable = $expression_to_evaulate ? true : false
?>

You do not necessarily have to have a variable that is true or false. You can do any expression you need to.

dBug. Awesome PHP Debugger

I just ran into this awesome PHP class called dBug. It works like print_r, but does so much more. You can expand and collapse anything in the structure you use it with. You know no idea how nice this will be to parse large data structures with. So nice. Check it out at: http://dbug.ospinto.com/

The Ultimate PHP CSV Parser

Lately I have been trying to find cool PHP utilities that I can use to handle my day to day tasks. Lately parsing CSV files in PHP has been something I have had to do over and over again. Such a basic task can be such a pain considering all of the different formats that they can be in (mostly the quotes around some of the values). I would do find and replace on the file to remove all the extra stuff and hope that I did not corrupt the data.

There had to be a better way to do this. I did some searching and I found Ming Hong Ng’s CSV Parsing class. It worked perfectly. I have parsed a ton of CSV files with no problems at all. You can download this class from: http://minghong.blogspot.com/2006/07/csv-parser-for-php.html

It is so simple to use too! Simply create an instance and call ParseFromFile or ParseFromString. The data is stored in the data instance variable. So simple!

This is something that should be apart of any PHP programmers toolkit.

Use cURL to Connect To Another System

cURL is a very useful tool used to pragmatically connect and interact with other systems to do various tasks like download a Web Page or RSS feed. It is also used to send information to another computer and get a response. A common example of using cURL is when you are purchasing something online. When you get your shipping quote or authorizing your credit card, you are very likely using something like cURL.

<?
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec ($ch);
?>

These 5 lines of code do a lot. In line 1 you begin creating your transaction by calling curl_init() and passing the URL you want to connect to (in this case we have a variable called $url).

Lines 2-4 set different options for the transaction. The CURLOPT_HEADER option is set to zero to signify we do not want the header information in the response. CURLOPT_RETURNTRANSFER tells cURL to assign the data we receive to a variable rather than simply output it to the screen. CURLOPT_POSTFIELDS sets what data we want to send in the header of our request. This is a very important option when interacting with other systems as this is generally where you will put your XML or other header data that is required by the system you are connecting with.

Line 5 actually executes the request. It will reach out to the $url, with $fields in the POST areas of variables and putting the data we received into the variable $result. With this variable you can do whatever you need in order to make your application work.

Page 1 of 41234»