The False String
Article Index
The False String
Solution

In some situations compact code is worse than  confusing - it's wrong! Have a go at puzzling out this gotcha in PHP - and then see our solution and the pattern that avoids it.

Banner

Background

We all like to write compact code but this is usually a mistake. Compact code can be more difficult to read and understand and while you might think it is clever at the moment when you return to it you will be wondering what is going on. In this particular puzzle it not only turns out to be confusing -  the shorter code is the incorrect code.

PHP is a weakly typed language and like many weakly typed languages it does its best to help you by automatically converting types when it makes sense. For example, many utility functions will return a non-null value if they work and false otherwise. A non-null value is usually converted to true (this is usually called  "type juggling) and so you can test for success using:

if(function()){
do something with the value of function
}

You can even make use of the fact that the assignment operator returns the result of the assignment to write something like:

if($result=function(){
 echo($result);
}

It has to be admitted that, without any other problems arising, this isn't a good idea. The reason is simply that the difference between:

$result=function()
$result==function()

and

$result===function()

is difficult to spot just on the basis of one, versus two versus three equal signs!

However, even though there are style reservations, the approach is good PHP and it sort of works.

Puzzle

The strpos(string,tartget)  function is very useful. It will find a search target within the specified string and return the target's position within the string. If it doesn't find the target it returns false.

One adventurous PHP programmer decided to take cleverness a step too far with the following short program extract:

$string="abcdefg";
$target="d";
if ($result=strpos($string,$target)){
echo("found ".$result);
}else{
echo("not found ".$result);
}

 

The idea was based on the fact that if the target was found then the result is automatically treated as true and the whole thing works.

Of course this extract is just a simplification of the real code in the real application and testing it was much more difficult than changing a single line to set the target - which was provided by the contents of a web page.

It all seemed to work and then a single test case caused the code to fail. The test target was indeed in the string but the result was a very clear "not found".

What is going on?

What is the test case that causes the code to fail?

Turn to the next page when you are ready to find out.

 

Banner

<ASIN:0470527587>

<ASIN:1430210117>

<ASIN:0596804377>

<ASIN:0321442490>

<ASIN:1590598628>



 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.