LogoPhly, boy, phly
the weblog and site of Matthew Weier O'Phinney

Monday, March 27. 2006

PHP error reporting for Perl users

On perlmonks today, a user was needing to maintain a PHP app, and wanted to know what the PHP equivalent of "perl -wc script.pl" was -- specifically, they wanted to know how to run a PHP script from the commandline and have it display any warnings (ala perl's strict and warnings pragmas).

Unfortunately, there's not as simple a way to do this in PHP as in perl. Basically, you need to do the following:

  • To display errors:
    • In you php.ini file, set "display_errors = On", or
    • In your script, add the line "ini_set('display_errors', true);"
  • To show notices, warnings, errors, deprecation notices:
    • In you php.ini file, set "error_reporting = E_ALL | E_STRICT", or
    • In your script, add the line "error_reporting(E_ALL | E_STRICT);"

Alternatively, you can create a file with the lines:

<?php
    error_reporting(E_ALL | E_STRICT);
    ini_set('display_errors', true);

and then set the php.ini setting 'auto_prepend_file' to the path to that file.

NOTE: do not do any of the above on a production system! PHP's error messages often reveal a lot about your applications, including file layout and potential vectors of attack. Turn display_errors off on production machines, set your error_reporting somewhat lower, and log_errors to a file so you can keep track of what's going on on your production system.

The second part of the question was how to run a PHP script on the command line. This is incredibly simple: php myscript.php. No different than any other scripting language.

You can get some good information by using some of the switches, though. '-l' turns the PHP interpreter into a linter, and can let you know if your code is well-formed (which doesn't necessarily preclude runtime or parse errors). '-f' will run the script through the parser, which can give you even more information. I typically bind these actions to keys in vim so I can check my work as I go.

If you plan on running your code solely on the commandline, add a shebang to the first line of your script: #!/path/to/php. Then make the script executable, and you're good to go. This is handy for cronjobs, or batch processing scripts.

All of this information is readily available in the PHP manual, and the commandline options are always available by passing the --help switch to the PHP executable. So, start testing your scripts already!

Posted by Matthew Weier O'Phinney in Perl, PHP at 23:10 | Comments (7) | Trackbacks (0)

Trackbacks
Trackback specific URI for this entry

No Trackbacks

Comments
Display comments as (Linear | Threaded)

Alternatively:
php -d "error_reporting=4095" script.php

( 4095 = E_ALL (2047) | E_STRICT (2048) )

S
#1 Sean Coates (Link) on 2006-03-28 00:03 (Reply)
D'oh! Missed the -d switch when scanning through the CLI options! Thanks for pointing this one out, Sean!
#1.1 Matthew Weier O'Phinney (Link) on 2006-03-28 09:09 (Reply)
I use the vim trick of piping the source of the script I'm editing (save changes first) through the CLI:

:!php -l % [syntax check]
:!php -f % [parse and execute]

The latter can be problematic if you're working on a script meant for the Web and the include_path is set per virtual host. The former is a huge time saver (I miss perl -cw from my CGI days).

Sean has a neat trick there too, thanks for that one.
#2 Douglas Clifton (Link) on 2006-03-28 02:14 (Reply)
I have "!php -l %" bound to -L in my vimrc :-)
#2.1 Matthew Weier O'Phinney (Link) on 2006-03-28 09:19 (Reply)
What about not using auto_prepend_file but a simple wrapper script:



and then call "php wrapper.php myscript.php"
#3 fa (Link) on 2006-03-28 07:13 (Reply)
The php CLI executable can only execute one script at a time; any other scripts provided as arguments become arguments to the second script. So the wrapper script would need to then do something like "include $argv[1];".
#3.1 Matthew Weier O'Phinney (Link) on 2006-03-28 07:21 (Reply)
arg, I got stripped

--
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
require $argv[1];
--
#4 fa (Link) on 2006-03-28 07:16 (Reply)

Add Comment

Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

 
 
  • Home
  • Resume
  • Blog
  • Phly PEAR Channel
  • Contact Me
  • About this site

ZCE

Zend Education Advisory Board Member

Add to Technorati Favorites

Calendar

Back July '08
Mon Tue Wed Thu Fri Sat Sun
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Quicksearch

Links

  • PHLY - PHp LibrarY
  • Paul M. Jones
  • Mike Naberezny
  • Shahar Evron
  • Planet PHP
  • Zend Where I now work
  • Garden.org Where I once worked

Archives

July 2008
June 2008
May 2008
Recent...
Older...

Categories

XML Linux
XML Personal
XML Aikido
XML Family
XML Programming
XML Perl
XML PHP

All categories

Syndicate This Blog

XML RSS 0.91 feed
XML RSS 1.0 feed
XML RSS 2.0 feed
ATOM/XML ATOM 0.3 feed
ATOM/XML ATOM 1.0 feed
XML RSS 2.0 Comments

Show tagged entries

xml best practices
xml books
xml conferences
xml dojo
xml dpc08
xml file_fortune
xml linux
xml mvc
xml pear
xml personal
xml php
xml programming
xml ubuntu
xml webinar
xml zendcon
xml zend framework
© 2004 - present, Matthew Weier O'Phinney
matthew-web <at> weierophinney.net