Matt Sigman

Hands-On Software Leader

Matt Sigman - Hands-On Software Leader

PHP5 substr() breaking change got me after all these years

I recently migrated a site from PHP4 to PHP5 and though I went through and updated all the code to be PHP5 compatible… I thought… I missed one minute detail buried in the documentation of PHP 5.2.2 (this change was not in PHP 5.0):

If the start parameter indicates the position of a negative truncation or beyond, false is returned. Other versions get the string from start.

What does this mean?  In my case I was truncating some line break characters from the end of a string, which had been working fine for at least 5 years on PHP4, like so:

$cur_row = substr($cur_row,'',-2);  //This works up to and including PHP 5.2.1

However as of 5.2.2 it started returning FALSE instead!  Talk about difficult to track down…

Anyhow, the updated syntax is as follows:

$cur_row = substr($cur_row,0,-2);  //This works on all versions

http://php.net/manual/en/function.substr.php

Category: PHP
  • plasticuarto.wordpress.com says:

    Hi i am kavin, its my first occasion to commenting
    anywhere, when i read this post i thought i could also create comment due to this good post.

    June 12, 2013 at 7:57 PM
  • commercial pest control services says:

    Great article! We will be linking to this great content on our website.
    Keep up the great writing.

    June 27, 2013 at 2:05 PM
  • More hints says:

    A fascinating discussion is worth comment. I do think
    that you ought to write more about this issue, it may not be a taboo
    matter but generally people do not discuss these topics.

    To the next! Best wishes!!

    July 10, 2013 at 4:11 PM

Your email address will not be published. Required fields are marked *

*