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:

  1.  
  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
  9.  

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:

  1.  
  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
  6.  

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:

  1.  
  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. }
  10.  

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:

  1.  
  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"
  13.  

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:

  1.  
  2. $a = ‘ls -l‘;
  3.  

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:

  1.  
  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. }
  9.  

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:

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

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:

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

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.

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

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

Variable Variables and Variable Functions in PHP

Variable Variables and Function Variables in PHP

PHP variable variables are almost never used because they greatly obfuscate any application
that you’re trying to write and can very easily inject a number of bugs into a script.
That being said, this interesting little feature just may be on the Zend PHP Certification Exam.

Variable variables use a variable as the name of another variable.
You may think that these are kind of like a pointer in other languages and I guess they are
except you can’t do any of the fun pointer arithmetic. Here’s a simple example of using the
value of another variable for a variable’s name.

  1.  
  2. $name = ’foo’;
  3. $$name = ’bar’;
  4. echo $foo;
  5. // Displays ’bar’
  6.  

This could also be used to have a number as a variable name (which normally isn’t allowed):

  1.  
  2. $name = ’123’;
  3. $$name = ’456’;
  4. echo ${123};
  5.  

While on the topic of PHP pointer like stuff, it is also possible to use a variable as a function pointer
like in the following example.

  1.  
  2. function myFunc() {
  3.         echo ’myFunc!’;
  4. }
  5. $f = ’myFunc’;
  6. $f(); // will call myFunc();
  7.  

I don’t think I’ll be using any of these features in my applications any time soon but hopefully I
won’t be clueless when taking the Zend PHP Certification Exam to become a Certified Zend Engineer.

Print, Echo, and Deprecated PHP Tags

One of the first unavoidable first steps of writing php is outputting to the screen using echo or print. These simple functions are invaluable to every script that I’ve ever written and I’m sure they will be all over the Zend PHP certification exam.

Turns out that in most cases, using the echo function instead of the print function is a bit faster. Usually this won’t really make any difference in your day to day code but it’s interesting nonetheless. It’s faster because it doesn’t return a value.

The print function is good when you need to use it as a function, for example:

  1. while(print("this will return stuff"))
  2. {
  3.     // Important PHP code here.
  4. }

Another basic element is the PHP script tags. I’ve really only ever used the normal tags but I’ve always known about the others. Turns out that they are all slowly being phased out.
Short tags, script tags and ASP tags are all considered deprecated and their use is strongly discouraged. Here’s an example of each type of tag that you shouldn’t be using:

  1. //Short Tags:
  2. <?
  3. code
  4. ?>
  5. <?= $variable ?>
  6.  
  7. //Script Tags:
  8. <script language=“php”>
  9. code
  10. </script>
  11.  
  12. //ASP Tags:
  13. <%
  14. code
  15. %>

Lastly I came across this odd little statement:

  1. echo (int) ((0.1 + 0.7)*10);

This will echo 7. Whaaa?

This happens because the result of this simple arithmetic expression is stored internally as 7.999999 instead of 8; when the value is converted to int, PHP simply truncates away the fractional part, resulting in a rather significant error (12.5%, to be exact).

I’ve read that the Zend exam likes to use odd little questions like this to try and trip up developers. Hopefully knowing how this works will help me on my way to becoming a Zend Certified Engineer.

 

 

Zend PHP Certification Exam Decision

I’ve decided to take the Zend PHP Certification exam. I haven’t decided when yet. I’ve been programming in PHP for many years and I know that I’m great at it, but I figured a certification emphasizing my expertise couldn’t hurt. As far as I have read, the exam is fairly thorough with it’s material and it will definitely try to throw a number of Gotchas at you. If you haven’t been programming in PHP for at least a few years then you will really want to study up hard.

The whole point of this blog is to share my study notes as I progress through all the material. I do not plan on posting basic information such as how to write a for loop or anything as trivial as that. I will be posting anything that I feel I need to study up on. I’ve already been reading up for a few days and I’ve come across numerous topics that I’ve never even seen before in my day to day operations so I don’t think I’ll have a shortage of material to write about.

From what I’ve googled about zend php certification exam, it seems that a lot of people fail. I’m not sure if that is just because the people that pass don’t really post their success or if the test is really that hard. I’m hoping for the former but I’ll have to wait to find out.

This PHP exam covers PHP 5.3 and can be taken in one of 4700+ Pearson VUE testing centers around the world. Upon completion, I will apparently receive a copy of Zend Studio and a Zend Server Developer license along with being listed among the top PHP developers in the Niagara region which is alright I guess… More importantly, the successful completion of this Zend PHP exam will get me the designation of a Zend Certified Engineer. I’ll probably also get a nice shiny certificate that I can plan to frame and hang on the wall, but realistically put in a drawer and forget about.

Lastly, the exam is only $200 so it’s really not that big of a drain on the finances and if I do end up failing, I’m not out a few thousand dollars like some exams.