Professional PHP

PHP Programming, Web Development, PHP Advocacy and PHP Best Practices.
« Michigan Taxes Graphic Design Services
Keywords and Language Simplicity »

Improved Error Messages in PHP 5

October 7th, 2007

Sometimes its the little things that make a difference. If you run the this test program in PHP 4 (tested on 4.4.7):

< ?php
function test($arg) { echo "talk like a pirate."; }
test();
?>

You get the following message:

Warning: Missing argument 1 for test() in /usr/bin/- on line 2

The error message here is reported at the position of the definition of the function, but really the error was in how the function was called. The required parameter to test was not passed. This error can be annoying, forcing you to consult a stack trace to find the actual error location. Something some beginners may not know how to do.

However, if you run the same message in PHP 5 (tested on 5.2.2):

Warning: Missing argument 1 for test(), called in /Users/jeff/- on line 3 and defined in /Users/jeff/- on line 2

Sweet improvement!

One more reason to ditch PHP 4 and go php 5.

Filed Under

  • PHP

Related Posts

  • Upgraded to WordPress 1.5
  • Even the Big Guys Get Validation Wrong
  • Exceptional PHP
  • goto in PHP
  • PHP Framework Consolidation?
Both comments and pings are currently closed.

16 Responses to “Improved Error Messages in PHP 5”

  1. Alex Netkachov says:
    10/7/2007 at 11:51 pm

    Hello,

    I consider that it is better to convert all warnings into exceptions as follows:
    http://www.alexatnet.com/node/23

     
    function errorHandler($errno, $errstr, $errfile, $errline) {
    	throw new Exception($errstr, $errno);
    }
    set_error_handler('errorHandler');
     

  2. Sascha says:
    10/8/2007 at 4:50 am

    Yes, and there are more of them, especially when working with objects. And xdebug2 makes this even better as it displays a full stracktrace.

  3. PHPDeveloper.org says:
    10/8/2007 at 5:04 am

    Jeff Moore’s Blog: Improved Error Messages in PHP

  4. Jeff says:
    10/8/2007 at 9:01 am

    Sascha, XDebug is really handy.

    Alex,
    I don’t recommend converting php errors or warnings to exceptions. If you are integrating code from multiple parties, or running mixed code that targets php 4, it won’t be aware of the exceptions. That code won’t expect the exceptions and might not work correctly for common error conditions. I talk about this issue in my December 2006 column on exceptions in php | architect.

  5. developercast.com » Jeff Moore’s Blog: Improved Error Messages in PHP says:
    10/8/2007 at 9:25 am

    [...] Moore mentions a “sweet improvement” he noticed when comparing the error message from a PHP4 script to [...]

  6. Dagfinn Reiersøl says:
    10/9/2007 at 2:37 am

    I requested this improvement a couple of years ago through the PHP bug database after becoming frustrated with the responses I got when using type hints. You made my day with this blog post. :-)

  7. sf devblog says:
    10/24/2007 at 12:42 pm

    I have used php5 for 2 yeaers :) I’m happy.

  8. woal says:
    11/21/2007 at 6:03 pm

    I love XDebug. Nothing better than it.

  9. retry says:
    2/12/2008 at 11:21 pm

    Nobody should be writing in php4 anymore. If you have legacy apps that have OOP in them, OK, I weep for you. But php5 has been mature enough for years and plenty of good webhosts support it 100%. It’s time to put php4 to bed and let the php4 centric anti-php arguments fade off into irrelevance.

  10. Khumaer says:
    3/31/2008 at 11:34 am

    As of PHP 5 new error reporting constant E_STRICT was introduced with value 2048. It enables run-time PHP suggestions on your code interoperability and forward compatibility, that will help you to keep latest and greatest suggested method of coding. E.g. STRICT message will warn you on using deprecated functions.

    E_ALL does not include E_STRICT so it’s not enabled by default

    To enable full error reporting (recommended for development boxes) use:

    use error_reporting(E_ALL | E_STRICT);

    or in php.ini:

    error_reporting = E_ALL | E_STRICT

  11. ennah says:
    5/7/2008 at 8:14 am

    I’m just wondering. Have you ever tried to use symfony, a framework of php? I am a computer science student and my professor said that it’s good to use that framework in one of the systems that we will be doing. I have googled up blogs about PHP and i was directed to this blog.

  12. Iron says:
    6/26/2008 at 1:38 pm

    This made me switch my local server to PHP 5 (finally).

  13. James Benson says:
    7/1/2008 at 6:29 pm

    I’m trying to migrate all clients onto PHP5 but it’s actually fairly hard when they have tons of existing code built around PHP4, sure it will mostly work but migrating it all over is not an easy process or quick process and likely a lot of PHP4 code and servers will remain for a fair few years yet.

  14. ddos says:
    8/3/2008 at 10:55 am

    PHP 5 is a huge step forward compared to PHP 4, that’s for sure. I agree to retry, that no one should be writing PHP 4 anymore. listing all the things why you should switch to PHP5 would probably take too much coffee than human organism could handle, error reporting just being one of them.
    I truly agree, that error messaging improvement is a great and necessary update to PHP language.

  15. Steven says:
    8/1/2011 at 1:55 am

    Here’s the best reference for setting error reporting:

    http://www.bx.com.au/tools/ultimate-php-error-reporting-wizard

  16. PHP Reference Links | kabayview.com says:
    10/10/2011 at 8:24 am

    [...] Improved Error Messages in PHP – Jeff Moore [...]

    Subscribe Feed
    Share Subscribe to this blog…
    Share Bookmark or share this page…
  • About

    My name is Jeff Moore. I'm a PHP programmer living in San Francico and working for a startup.

    More about me…

  • Categories (Home)

    • Agile Methods (14)
    • Mac (14)
    • Misc (18)
    • Open Source (14)
    • PHP (99)
    • Software Design (29)
    • Usability (14)
    • Web Design (20)
  • Recent Comments

    • rsync to remote server via ssh  37
      Petr Halounek, Penni Tomasino, Rodney Kohnen [...]
    • WordPress BBCode Plugin  30
      wepniveth, Pamella Philipps, evakuat [...]
    • PEAR Templates  18
      Sang Bellotti, Kandice Sansing, car insurance estimates for teenagers [...]
    • Extreme Simplicity  15
      Gilbert Moatz, Roni Beauregard, Barb Geyer [...]
    • Manual Memory Management is Dead  6
      Grass Fed Filet Mignon, Kellie Carello, PAPANDOR [...]
    • Friendster wrapup: does MySQL scale  38
      Ollie Joya, nfl jersey on sale, selling scrap gold [...]
    • The Coding Apprentice  51
      fkawau, Annamae Mccane, Boca Raton Personal Injury [...]
    • The Legality of Republishing RSS Feeds  30
      dasfdsfsd, reebok authentic nfl jersey, Tory Rennemeyer [...]
    • Exceptional PHP  7
      Sports, The Click, Laraine Waterhouse [...]
    • PDO versus MDB2  42
      selling silver coins, Oliver Luongo, ddkoaorpa [...]
  • Recent Posts

    • Richard Thomas
    • ZendCon: Writing Maintainable PHP Code
    • Looking Towards the Cloud
    • Holiday Tech Support
    • Closures are coming to PHP
    • php | tek Wrapup
    • php | tek 2008
    • Sarah Snow Stever
    • Benchmarking PHP’s Magic Methods
    • The Endpoints of the Scale of Stupidity on Video
  • Site

    • Archives
    • Log in
  • Search