strict checks with php -l

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hello all,

Is there any way to trigger "undefined variable/constant" error notices
when running `php -l' over a source file? Or even better, when using
php_check_syntax() over the file?

So far I've noticed that I only get those errors when actually *running*
the script, and I'm beggining to suspect there's little more (if at all)
to do without getting dirty with a PHP language parser. For example,
when I run the following script:

,----[ bin/phpcheck ]
| <?php
| ini_set('include_path', ini_get('include_path').":". ".");
| ini_set("display_errors","1");
| error_reporting(E_ALL | E_STRICT);
| array_shift($argv);
| foreach ($argv as $file) {
|     if (!php_check_syntax($file, $errors))
|         echo $errors;
| }
| ?>

over a file like this:

,----[ tmp/testerr.php ]
| <?
| error_reporting(E_ALL | E_STRICT);
| echo $a; // non-defined
| a; // WTF is that?
| ?>

I get:

| $ bin/phpcheck tmp/testerr.php
| $  

(i.e., nothing)

Same story with `php -l':

| $ php -l tmp/testerr.php  
| No syntax errors detected in tmp/testerr.php

But I get all those wonderful notices when actually running the script:

| $ php -f tmp/testerr.php  
| Notice: Undefined variable: a in /path/to/tmp/testerr.php on line 4
| Notice: Use of undefined constant a - assumed 'a' in
| /path/to/tmp/testerr.php on line 5

PHP (installed from source) & OS versions follow:

| $ php -v
| PHP 5.0.4 (cli) (built: May 18 2005 14:21:02)
| Copyright (c) 1997-2004 The PHP Group
| Zend Engine v2.0.4-dev, Copyright (c) 1998-2004 Zend Technologies
| $ cat /etc/fedora-release  
| Fedora Core release 2 (Tettnang)

I appreciate any hints on this.


Cristian Gutierrez                          /* */
One cannot be l33t and clueful at the same time !
-- James Edwards, full-disclosure

Re: strict checks with php -l

Cristian Gutierrez wrote:

Quoted text here. Click to load it


Those notice messages are only available on runtime cause only
then one can check for sure if some variable was set (or some constant
was defined). Those notices have nothing to do with syntax.

For example your "testerr.php" script could be included by other
script which would (prior to including) set value of variable $a and
define constant "a", so there would be no notices.


Re: strict checks with php -l

Este lunes, dijo:
Quoted text here. Click to load it


Quoted text here. Click to load it

You're right. Then I realize that it's not a syntax checker what I look
for (`php -l' is just fine for that), but the equivalent of `use strict'
in Perl. I guess some day `phplint' [1] will parse PHP5 completely.


[1] <

Cristian Gutierrez                          /* */
CChheecckk yyoouurr dduupplleexx sswwiittcchh..

Site Timeline