Comparing and Transforming Strings

String functionality and manipulation methods are an enormous part of the PHP language and should be studied heavily. The following functions are by no means an exhaustive list of what can be done with strings and the resources on the php site should be read over.

When comparing strings, it’s best to use strcmp() and strcasecmp() which is a case insensitive version of the former. Both of these functions will return a zero value to indicate that the strings are equal, less than zero if the first string is less than the second, and greater than zero if the first string is greater.

  2. int strcmp ( string $str1 , string $str2 )
  4. int strcasecmp ( string $str1 , string $str2 )

Also worth noting is the strncmp() function that works similar with one difference. It will only do a comparison on the first n characters.

  2. int strncmp ( string $str1 , string $str2 , int $n )

The strtr() function is used to translate characters or replace substrings. I can be given either two or three parameters, depending if you want to replace one or more values.

  2. string strtr ( string $str , string $from , string $to )
  3. string strtr ( string $str , array $replace_pairs )
  5. echo strstr (’abc’, ’a’, ’I’); //Ibc
  7. $subst = array (
  8.         ’1’ => ’I’,
  9.         ’2’ => ’S’,
  10. );
  12. echo strtr (123’, $subst); //IS3

Heredoc Syntax

Heredoc is a strange but necessary way to create a complex string. The functionality provided is almost identical to double quoted strings except there are no quotes.

The heredoc syntax uses the special operator ‘<<<' followed by a user defined identifier. The same identifier on a new line must be used to close the string followed by an optional semicolon. This identifier is restricted by the same rules as a regular variable name.

  2. $what = "Certification";
  3. echo <<<TEXT
  4. Zend PHP "$what"
  5. TEXT;

This will output ‘Zend PHP “Certification”‘.
Heredoc syntax can not be used for initializing class properties. Since PHP 5.3, this limitation is valid only for heredocs containing variables.

Stacks, Queues and Sets

Arrays can be used in a number of different ways. This can include stacks, queues, and sets. I personally don’t use these functions very often but It can’t hurt to go over them for before taking the Zend Certification exam.

Stacks are a Last in, First out (LIFO) structure which means that the first element you put on the stack is only available when all other elements are removed. Just like a stack of papers. This is acheived by using the array_push and array_pop functions.

Here is a simple little example:

  2. $stack_array = array();
  3. array_push($stack_array,"zend");
  4. //You can add multiple values at once
  5. array_push($stack_array,"php","certification");
  6. $last_in = array_pop($stack_array); //last_in will house "certification"

The array_push function is very similar to “$a[] = $value” and the latter is actually much faster because no function call is made.

Next up is the queue which is a first in first out data structure. Just like a line at a movie theater, the first elements added to the queue get their choice of seats. Queue manipulation is acheived using the array_shift, array_unshift, and any of the functions outlined above.

The array_shift function will remove and return the element from the front of the array and the array_unshift will prepend an element to the front of the array. If both these functions are used together then the array will work like a sort of backwards stack but if array_shift is used with array_push or if array_unshift is used with array_pop then the array will act as a queue.

  2. $stack_array = array(’zend’, ’php’, ’cert’);
  3. $first = array_shift($stack); //$first now houses ‘zend’
  4. array_unshift($stack, ’dont fail’); //adds ‘dont fail’ to the beginning of the array.

There are a few functions that mimic set functionality using php arrays. These are array_diff(), array_intersect, array_intersect_key(), array_intersect_assoc(),array_intersect_ukey(), and array_intersect_uassoc().

The function array_diff() is used to compute the difference between two arrays:

PHP Sorting Functions

In all the countless hours I’ve spent with php, I’ve maybe used three or four of these sorting functions. I really had no idea that there is a total of eleven functions used for sorting arrays. Anyway, I’m betting that it may be useful to have these memorized before I take the Zend PHP Certification Exam so here is a brief overview of each one.

A lot of the sorting functions have an optional flag that may be used to modify the sorting behavior.
The available flags are:

SORT_REGULAR – compare items normally (don’t change types)
SORT_NUMERIC – compare items numerically
SORT_STRING – compare items as strings
SORT_LOCALE_STRING – compare items as strings, based on the current locale.

All of these functions sort arrays. I don’re really think it’s necessary to type out an example for each one but I will go into greater detail about some. Each function will return a boolean which is true on success and false on failure.

  2. bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

This sort function will arrange the elements from lowest to highest.
Array indices do NOT maintain their correlation with the array elements they are associated with.

  2. bool rsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

This one will arrange the elements from highest to lowest (Reverse Sort).

  2. bool natsort ( array &$array )

This sorting function will sort the array alphanumerically. This is more “natural” for most people.
Here is a comparison between the regular sort and this natural sort from the site

  2. <?php
  3. $array1 = $array2 = array("img12.png", "img10.png", "img2.png", "img1.png");
  5. asort($array1);
  6. echo "Standard sorting\n";
  7. print_r($array1);
  9. natsort($array2);
  10. echo "\nNatural order sorting\n";
  11. print_r($array2);
  12. ?>
  13. Which will output:
  14. Standard sorting
  15. (
  16.     [3] => img1.png
  17.     [1] => img10.png
  18.     [0] => img12.png
  19.     [2] => img2.png
  20. )
  22. Natural order sorting
  23. (
  24.     [3] => img1.png
  25.     [2] => img2.png
  26.     [1] => img10.png
  27.     [0] => img12.png
  28. )
  2. bool natcasesort ( array &$array )

A case insensitive version of natsort.

  2. bool ksort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

Sorts an array by the key. It also maintains the key to data correlations.

  2. bool krsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

Sorts by key, but in reverse.

  2. bool asort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

This is very similar to the regular sort outlined above except the array indices maintain
their correlation with the array elements they are associated with.

  2. bool arsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

Similar to the asort but reversed.

  2. bool usort ( array &$array , callback $cmp_function )

This function allows for a custom comparsion function to create different sorting methods. This function should accept only two parameters and return an integer less than, equal to,
or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. Here’s an example:

  2. <?php
  3. function cmp($a, $b)
  4. {
  5.     if ($a == $b) {
  6.         return 0;
  7.     }
  8.     return ($a < $b) ? -1 : 1;
  9. }
  11. $a = array(3, 2, 5, 6, 1);
  13. usort($a, "cmp");
  15. foreach ($a as $key => $value) {
  16.     echo "$key: $value\n";
  17. }
  18. ?>
  2. bool uasort ( array &$array , callback $cmp_function )

This is similar to usort except the array indices maintain their
correlation with the array elements they are associated with.

  2. bool uksort ( array &$array , callback $cmp_function )

This last sort function works similar to the previous two where you can define a custom comparison function. The difference is that this one works on the keys and it also maintains the correlation with the elements.

There is also a nice comparison of all these functions at

PHP Array Cursors

I don’t recommend ever using these functions because a foreach loop is so much simpler and shorter. The only reason I’m writing about these is bacause I have almost never used them and they may just be on the Zend PHP Certification exam.

There are a number of functions designed specifically for manipulating array pointers. Most examples that I’ve seen use these functions during some sort of array iteration. When starting an iteration over an array, the first step is usually to reset the pointer to its initial position using the reset() function. After that, moving forward or backwards by one position is done using prev() and next(). At any given point, we can access the value of the current element using current() and its key using key(). Here’s a simple example:

  2. $array = array(’foo’ => ’bar’, ’baz’, ’bat’ => 2);
  3. function displayArray($array) {
  4.         reset($array);
  5.         while (key($array) !== null) {
  6.                 echo key($array) .": " .current($array) . PHP_EOL;
  7.                 next($array);
  8.         }
  9. }

The displayArray function above will display all the values in an array. Reset() is called first to return the internal array pointer to the first element. Next, using a while loop, the current key and value is displayed using the key() and current() functions. Finally, the array pointer is advanced, using next().

Since you can iterate back-and-forth within an array by using its pointer, you could—in theory—start your iteration from the last element (using the end() function to reset the pointer to the bottom of the array) and then making your way to the beginning:

  2. $a = array (1, 2, 3);
  3. end($a);
  4. while (key ($array) !== null) {
  5.         echo key($array) .": " .current($array) . PHP_EOL;
  6.         prev($array);
  7. }

So I guess that last example may be useful…but it’s still probably easier to reverse the entire array and use a foreach.

Anyway, if these are on the Zend PHP Certification exam, I shouldn’t have a problem.

Comparing Arrays in PHP

Array-to-array comparison is a relatively rare occurrence in the real world of PHP (at least for me), but it can be performed using a set of operators. Like for other types, the equivalence and identity operators can be used for this purpose:

  2. $a = array (1, 2, 3);
  3. $b = array (1 => 2, 2 => 3, 0 => 1);
  4. $c = array (’a’ => 1, ’b’ => 2, ’c’ => 3);
  5. var_dump ($a == $b); // True
  6. var_dump ($a === $b); // False
  7. var_dump ($a == $c); // False
  8. var_dump ($a === $c); // False

As you can see, the equivalence operator == returns true if both arrays have the same number of elements with the same values and keys, regardless of their order. The identity operator ===, on the other hand, returns true only if the array contains the same key/value pairs in the same order.

Similarly, the inequality and non-identity operators can determine whether two arrays are different:

  2. $a = array (1, 2, 3);
  3. $b = array (1 => 2, 2 => 3, 0 => 1);
  4. var_dump ($a != $b); // False
  5. var_dump ($a !== $b); // True

Once again, the inequality operator only ensures that both arrays contain the same elements with the same keys, whereas the non-identity operator also verifies their position.

Apparently arrays are covered heavily on the Zend PHP Certification Exam so the next few posts will cover arrays.

Default Values for By-Reference Parameters

Unlike PHP 4, PHP 5 allows default values to be specified for parameters even when they are declared as by-reference:

  2. function command_exists($command, &$output = null) {
  3.         $output = ‘whereis $cmd‘;
  4.         if (strpos($output, DIRECTORY_SEPARATOR) !== false) {
  5.                 return true;
  6.         } else {
  7.                 return false;
  8.         }
  9. }

In the example above, the $output parameter is completely optional—if a variable is not passed in, a new one will be created within the context of command_exists() and, of
course, destroyed when the function returns.

Function Name Case Sensitivity and Variable-Length Argument Lists

After spending years developing software, I’ve always assumed that most real programming languages are extremely case sensitive in almost every aspect. It turns out that PHP function names are not case-sensitive. I was extremely surprised to read this and I definitely don’t agree with it. Variables in PHP are case sensitive so it could be worse.

It was interesting to find out that PHP function parameters can be defined on the fly. PHP provides three built-in functions to handle variable-length argument lists: func_num_args(), func_get_arg() and func_get_args().

Here’s an example of how they’re used:

  2. function hello()
  3. {
  4.         if (func_num_args() > 0) {
  5.                 $arg = func_get_arg(0); // The first argument is at position 0
  6.                 echo "Hello $arg";
  7.         } else {
  8.                 echo "Hello World";
  9.         }
  10. }
  11. hello("Reader"); // Displays "Hello Reader"
  12. hello();         // Displays "Hello World"

The variable-length arguments will actually be useful instead of something that I’m just learning to get designated as a Zend Certified Engineer.

Backticks, EOL, and Break

While studying up for the Zend PHP Certification exam I came across a few more elements that I’ve never used before.

Firstly, there is a backtick operator. This makes it possible to execute a shell command and retrieve
its output. For example,the following will cause the output of the UNIX ls command to be stored inside $a:

  2. $a = ‘ls -l‘;

Secondly, PHP has an end of line constant. I’ve never seen PHP_EOL before but it seems to work similar to “\n”.
I don’t think I will ever use this due to the length when compared with “\n”.

Finally, a bit about the break function. I had no idea that both the break and continue statements take a
paramenter which can instruct them to break out of multiple loops. I felt a bit silly that I didn’t already know this.
Here is a simple example:

  2. for ($i = 0; $i < 10; $i++) {
  3.         for ($j = 0; $j < 3; $j++) {
  4.                 if (($j + $i) % 5 == 0) {
  5.                         break 2; // Exit from this loop and the next one.
  6.                 }
  7.         }
  8. }

There seems to be a lot that I’m going to have to study up on before I can become a Zend Certified Engineer.

PHP References

References in PHP are a way to access the content in a variable by different names. They are not at all like pointers in C. For instance, you cannot perform pointer arithmetic using them and they are not actual memory addresses.

I’ve found that references in PHP are fairly rare, but in the few cases I’ve seen them in the wild, I’ve always assumed that they were used for their faster performance but it turns out that by-reference activity is often slower than its by-value counterpart, because PHP uses a clever “deferred-copy” mechanism that actually optimizes by-value assignments.

Here is a simple php reference example:

  2. $a = 10;
  3. $b = &$a; // by reference
  4. $b = 20;
  5. echo $a; // Outputs 20

However PHP references are useful when used as a return value for a function. If they are used on something like
a database resource then you probably don’t want to be returning a copy:

  2. function &query($sql)
  3. {
  4.         $result = mysql_query($sql);
  5.         return $result;
  6. }

References can also be useful as a function parameter to pass by reference instead of by copy. So any changes that are made to the parameter survive the function.

One thing to watch out for when using references in a foreach loop is to make sure that you unset the reference. Otherwise you may inadvertently overwrite the last entry in the array.

  2. foreach($data_array as &$value)
  3. {
  4.         do_something_with_value();
  5. }
  6. unset($value);

I definitely expext at least a few questions to touch on references on the Zend PHP Certification Exam.