This is part 3 of a 6 part series looking at the some of the wonderful features of PHP 5.5 and why it might remain a dominate web language for the future. This series was originally written for the Software Developer’s Journal.
In part 3 we are covering a couple of my favorite features introduced in the PHP 5.5 branch. The first one, constant array and string dereferencing, have been a staple of many other languages and I am glad to see that it has finally made its way to PHP. This feature will surely simplify a lot of code and will work great with loops that move through strings especially. The second feature is how to use empty() within expressions. Many developers have hit their head on this one for awhile, wanting to use it in a while loop condition but haven’t been able to until now. Let’s begin!
This is certainly one of the most sugary goodness features introduced in PHP 5.5. Again this feature provides no real design advantage that couldn’t have been done before. However, it cuts out several steps of dereferencing an array or a string by making it one step. One quick comparison is by accessing the characters of a string. Before you would usually do something like pulling out a one character substring from the initial string or split the string using str_split() to dump it to an array and then access the index of the character you want etc. This cuts through all that by allowing you to index the string directly as if the string was an array of characters (which is technically it is behind the scenes).
Let’s create a small demo to see how we could use this along with a generator to produce a list of vowels from any string. We may pass a string of any length to our generator and have no clue about how many vowels will be contained in it. We will let the generator tell us.
<?php // Generator which takes a string and will yield all vowels. function produceVowels($str) { $vowels = "aeiou"; // Notice in our loop we are accessing the characters directly using $str[$i] for ($i = 0; $i < strlen($str); $i++) { if (strpos($vowels,$str[$i]) !== false) { yield $str[$i]; } } } // Iterator over vowels in given string and print them. foreach(produceVowels("testing") as $vowel) { echo "$vowel<br/>"; } ?>
This code above is very basic but you can see here that we are essentially applying a filter to the text we provided. We are filtering out any characters which are not a vowel. As we are generating vowels we could even change the filter criteria and the generator can magically start generating new values without the developer having to keep track of where they are in processing of the string.
Have an idea where something like string dereferencing may come in handy? Perhaps an old game of hangman where the user has to guess the letters in a word? This was my thought when I first came across this feature. Direct string and array dereferencing makes it easier to peer into the string or the array of values and attack them directly without first needing to pull them out (in the case of a string) or store them as an array variable and then work on them. It certainly cuts out a lot of work.
As we march on our way to better syntax the empty() function got a bit of love this time around. This feature seemed like a no brainer to me and has annoyed many long time PHP developers. Empty() is used to verify whether a variable was empty or contained a value that you could see as having no value. But when you plugged in an expression, like a function call, that did return a value that would be considered an empty value, then it would complain. If you had a function that evaluated some user supplied data and returned a pass or fail type of boolean, you can now make that call from within empty() itself. This eliminates the need to first store the result into a variable and then evaluate the variable. This change is simply another cleanup of syntax and adds a smidge of sugar to make code more readable. It is surprising how a simple change like this can have such a large impact on the elegance of a program’s layout. It is a change I am glad finally happened.
In part 4 of our 6 part series we are going to change focus to the foreach looping construct and two great features. The first one being the use of foreach now accepting non-scalar keys and how it can also be used with List(). The excitement just keeps on rolling!
Thanks for reading! 🙂