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?
You can leave a response, or trackback from your own site.

43 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. hlflgevb says:
    12/7/2009 at 10:35 am

    SP6Crm hukcoqnvlllg, [url=http://siolmvqfszye.com/]siolmvqfszye[/url], [link=http://gdpvblebkfkq.com/]gdpvblebkfkq[/link], http://wqqcuxpmdsqj.com/

  16. Map says:
    12/18/2009 at 10:09 pm

    Map

  17. Map says:
    12/18/2009 at 10:31 pm

    Map

  18. Map says:
    12/18/2009 at 10:34 pm

    Map

  19. Map says:
    12/18/2009 at 10:56 pm

    Map

  20. Map says:
    12/18/2009 at 11:09 pm

    Map

  21. Map says:
    12/18/2009 at 11:27 pm

    Map

  22. Map says:
    12/19/2009 at 12:06 am

    Map

  23. Map says:
    12/19/2009 at 1:17 am

    Map

  24. Map says:
    12/19/2009 at 1:24 am

    Map

  25. Map says:
    12/19/2009 at 1:30 am

    Map

  26. GnbOAcvjAvRCSGdIdaH says:
    12/19/2009 at 4:38 am

    |

  27. interracial-personal says:
    1/21/2010 at 1:26 am

    interracial personal ads or icp dating game or leftist dating or single man or astrology dating

  28. usa-dating-sites says:
    1/21/2010 at 1:37 am

    usa dating sites or dating personals or personal ads acronyms or dating sites in colorado or condoleezza rice dating

  29. dating-sim-game says:
    1/21/2010 at 1:37 am

    dating sim game or dating game 7 deadly sins or rich women seeking men or dating saypol or ivory coast dating

  30. speed-dating-los-ang says:
    1/25/2010 at 12:30 am

    speed dating los angeles events or black man dating white woman or chris edmonton dating motor or carrie underwood dating or addiction to internet dating sites

  31. amoxicillin-overdose says:
    1/25/2010 at 6:46 pm

    amoxicillin overdose or meridia generic or green tea diet pills or zyrtec d 12 hour or celebrex online prescription

  32. Amsterdam says:
    2/2/2010 at 4:47 pm

    We recently build Escort Amsterdam and we are trying to upgrade the PHP.Do you think that the code will fail?

  33. css says:
    2/7/2010 at 8:47 am

    Excellent One. Keep it up

  34. Fat Burner says:
    3/8/2010 at 10:52 am

    What is the best fats burner?…

    Fats usually comes to mind whenever you believe of losing pounds, so normally individuals wish to know what the best fats burner is. Everybody hates it simply because it makes you appear unfit and brings about health problems. So if you’re one of those…

  35. Terry Nessel says:
    3/12/2010 at 7:13 am

    Hi – It’s great to read such topical stuff on the Web as I have been able to discover here. I agree with much of what is written here and I’ll be coming back to this website again. Thanks again for publishing such great reading material!!

  36. Fernando Crickenberger says:
    3/27/2010 at 11:32 am

    Hey everyone, greetings from Ireland. This is a helpful site. Does anyone have any advice about staying out of the friend zone with girls? I’m really tired of girls telling me they just want to be friends. Maybe I’m being too nice?

  37. Jaquelyn Dellaca says:
    5/14/2010 at 5:12 pm

    I got on this site from Bing. I found that it’s right about astrology. I will have to come back here again. Thanks!

  38. escorts in ny says:
    5/25/2010 at 7:10 pm

    I gotta tell you, you’re totally on. I came 2 your blog from someone else’s article and am really interested in this topic and finding more info. Do you mind if I link to this blog from my site?

  39. Here is the initial post on my website about Modafinil says:
    6/7/2010 at 9:53 pm

    More news today…

    General Information on Nuvigil and Alertec I think many of us have already experienced conditions of recurring sleepiness during the day……

  40. Professional PHP Programmer says:
    7/27/2010 at 5:44 am

    Compare to old versions of PHP, it has been improved a lot in its latest version.

  41. escort den haag says:
    7/29/2010 at 2:44 pm

    nice articel, i learn every day more and more.
    the good thing is that there are more day’s to come.

    mark

  42. kevinxiao says:
    8/3/2010 at 11:58 am

    Grettings! I just came to appreciate you for your very interesting

  43. jessica says:
    8/3/2010 at 11:59 am

    I thought I would leave my first comment.

Leave a Reply

Click here to cancel 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

    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 (17)
    • Open Source (14)
    • PHP (98)
    • Software Design (29)
    • Usability (14)
    • Web Design (20)
  • Recent Comments

    • Programming Language Trends via Google  19
      Craigslist pva, jessica, Scott [...]
    • Looking Towards the Cloud  35
      bentonville multiple listing, cosmetic dental, Sam Brodish [...]
    • PHP versus ASP  8
      Marhta Blight, Ravi, Ryan Brooks [...]
    • How to Transfer Mac OS X Application Data between Computers  59
      Website Migration, harry the computer support guy, Dotty Salvage [...]
    • Working with PHP 5 in Mac OS X 10.5 (Leopard)  157
      lehuuphuc, Robert Parthemer, Lingerie Intimate [...]
    • PHP Games  25
      jessica, Tennille Cranor at Chilli Plants, Lucas Ortell [...]
    • un-PEAR-ing  5
      jessica, Eugene Panin, Arnaud [...]
    • The Legality of Republishing RSS Feeds  23
      kevinxiao, Marissa Miscovich, Quick Student Loans [...]
    • Faster Page Loading  4
      jessica, angular cheilitis, Aaron Rosenfeld [...]
    • PDO versus MDB2  15
      jessica, kevinxiao, Gavin [...]
  • Recent Posts

    • 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
    • Working with PHP 5 in Mac OS X 10.5 (Leopard)
  • Site

    • Archives
    • Log in
  • Search