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.

categories PHP
tags php-5

Related Posts

  • Upgraded to WordPress 1.5
  • Even the Big Guys Get Validation Wrong
  • Exceptional PHP
  • goto in PHP
  • PHP Framework Consolidation?
You can leave a response, or trackback from your own site.

14 Responses to “Improved Error Messages in PHP 5”

  1. #1 Alex Netkachov responds...
    October 7th, 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. #2 Sascha responds...
    October 8th, 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 trackbacked on October 8th, 2007 at 5:04 am
  4. #4 Jeff responds...
    October 8th, 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 pingbacked on October 8th, 2007 at 9:25 am
  6. #6 Dagfinn Reiersøl responds...
    October 9th, 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. #7 sf devblog responds...
    October 24th, 2007 at 12:42 pm

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

  8. #8 woal responds...
    November 21st, 2007 at 6:03 pm

    I love XDebug. Nothing better than it.

  9. #9 retry responds...
    February 12th, 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. #10 Khumaer responds...
    March 31st, 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. #11 ennah responds...
    May 7th, 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. #12 Iron responds...
    June 26th, 2008 at 1:38 pm

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

  13. #13 James Benson responds...
    July 1st, 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. #14 ddos responds...
    August 3rd, 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.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

code: use [code=php][/code].

Comment Preview

  • Search

  • Subscribe

    Subscribe All Posts
    Subscribe All Comments
    Subscribe All Bookmarks
    Subscribe with Bloglines Subscribe with My Yahoo Add to netvibes Subscribe in NewsGator Online Add to Google
  • Share This

  • Categories (Home)

    • Agile Methods (14)
    • Mac (14)
    • Misc (16)
    • Open Source (14)
    • PHP (95)
    • Software Design (28)
    • Usability (14)
    • WACT (7)
    • Web Design (20)
  • Recent Comments

    • PHP Development From Java Architects Eye  12
      peter, qmenqyqh, big [...]
    • Why is PHP Popular?  34
      portfelmake.ru, finance-x.ru, economnedv.ru [...]
    • The PHP scalability saga continues  35
      mvyqfarib, fuck, sorwostajufe [...]
    • Delphi for PHP  29
      Shawn White, Chris MacPherson, Pieter Viljoen [...]
    • How to Transfer Mac OS X Application Data between Computers  41
      ??????????, Edward Gan, Kamut Torok [...]
    • php | tek Wrapup  7
      Sergey Brin, Learn Programming, PHP Guru [...]
    • Working with PHP 5 in Mac OS X 10.5 (Leopard)  116
      Ya, Paul, Diana [...]
    • A WordPress bug fix  5
      Juggigomugs, LkM, wordpress quickstart guy [...]
    • The value of MVC  11
      wan optimization, Ron, Caren Goodman [...]
    • php | tek 2008  3
      CharnaD, NatureLimit, Scott
    • Why is PHP Code Considered Hard to Maintain?  26
      Tom, , Visitor338 [...]
  • Pages

    • Tags
  • Recent Posts

    • php | tek Wrapup
    • php | tek 2008
    • Sarah Snow Stever
    • Benchmarking PHP’s Magic Methods
    • The Endpoints of the Scale of Stupidity on Video
    • Working with PHP 5 in Mac OS X 10.5 (Leopard)
    • Keywords and Language Simplicity
    • Improved Error Messages in PHP 5
    • Michigan Taxes Graphic Design Services
    • Ruby versus PHP or There and Back Again
  • Archives

    • 2008: May
    • 2007: Jan Feb Mar Apr May Sep Oct Nov
    • 2006: Jan Feb Mar Apr May Jun Jul Oct Nov Dec
    • 2005: Jan Feb Mar Apr May Sep Oct Nov Dec
    • 2004: Apr May Jun Jul Aug Sep Oct Nov
  • Menu

    • Register
    • Log in