search for in the  
<packphp_strip_whitespace>
Last updated: Thu, 19 May 2005

php_check_syntax

(PHP 5)

php_check_syntax --  Check the PHP syntax of (and execute) the specified file

Description

bool php_check_syntax ( string file_name [, string &error_message] )

For technical reasons, this function is deprecated and removed from PHP. Instead, use php -l somefile.php from the commandline.

The php_check_syntax() function performs a syntax (lint) check on the specified filename testing for scripting errors. This is similar to using php -l from the commandline except php_check_syntax() will execute (but not output) the checked file_name. For example, if a function is defined in file_name, this defined function will be available to the file that executed php_check_syntax(), but output from file_name will be suppressed.

Parameters

file_name

The name of the file being checked.

error_message

If the error_message parameter is used, it will contain the error message generated by the syntax check. error_message is passed by reference.

Return Values

Returns TRUE if the lint check passed, and FALSE if the link check failed or if file_name cannot be opened.

ChangeLog

VersionDescription
5.0.5 This function was removed from PHP.
5.0.3 Calling exit() after php_check_syntax() resulted in a Segfault.
5.0.1 error_message is passed by reference.

Examples

php -l somefile.php

The above example will output something similar to:

PHP Parse error: unexpected T_STRING in /tmp/somefile.php on line 81



User Contributed Notes
php_check_syntax
phpdoc at michaeldouma dot com
15-Feb-2005 10:38
PROBLEM: Your page is blank, or you can not define any functions in an include.

As mentioned below, the php_check_syntax function will include your file. So you may not be able to include it again. If you have an include later in your code, and you define any functions, you will get an error, or in some cases a blank page.
Cainus
08-Jan-2005 10:06
In PHP4,

if (!function_exists('php_check_syntax')){
   function php_check_syntax($filename, &$errormessage){
       $command = 'php -l ' . $filename;
       $output = shell_exec($command);
       if (strpos($output, 'No syntax errors detected in') !== false){
           $errormessage = '';
           return(true);   
       } else {
           $errormessage = $output;
           return(false);   
       }   
   }
}

// Note: this function will have the same security constraints as shell_exec for a given system
dan dot ostrowski at gmail dot com
22-Oct-2004 04:50
This function is not documented all that well. The point they leave out is that php_check_syntax INCLUDES THE FILE for you as of php 5.0.2.  Not only that, but even if you use include_once( ) it will still give a redeclaration error. ( Provided of course, there's something declared in the file in question ).

It does NOT, as one might be led to believe, check the syntax just to let you know whether it's valid or not.

This will try to include the file twice, despite the fact that include_once is used:

<?php

$filename
= '/path/to/whatever.php';
$errs = (string) null;

if(
php_check_syntax( $filename, $errs ) ) {
  
// include
  
include_once( $filename );    // REDECLARATION ERROR
} else {
   print
"Syntax error";
   die( );
}

?>

**Editor's note: (sean)
Agreed, this function is poorly documented, but there is no clear direction for the docs, see:
http://bugs.php.net/27728
and
http://bugs.php.net/27406
philip
26-Aug-2004 10:18
This function is dangerous, read the following open bug report before using this function:
 * http://bugs.php.net/bug.php?id=27406

<packphp_strip_whitespace>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT