Solved

A PHP4 Alternative to PHP5's php_check_syntax()?

Posted on 2004-07-30
9
635 Views
Last Modified: 2008-01-16
Dear All,

Looking at the Misc page of the PHP function reference earlier, I found the page for php_check_syntax(). It looks like a very useful function, however I see that it is only for PHP5, and both my local server and my web host use PHP4. So my question is: is there some alternative code/function I could use in PHP4?

I would be very grateful for any assistance,

BigglesZX
0
Comment
Question by:BigglesZX
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 18

Assisted Solution

by:CrYpTiC_MauleR
CrYpTiC_MauleR earned 25 total points
ID: 11675501
I dont beleive there is an alternative, was hoping to find a PEAR class that would do it but didnt find any when I looked. Its basically the same thing as saving your file and then running it will look for parse errors making sure the sytax is ok. You can set your error reporting in your file for only parse errors and that will only show syntax errors.


Regards,
Nick
0
 
LVL 7

Expert Comment

by:zac_charles
ID: 11675534
Hi,

here is a quote from http://php4.globe.de/ChangeLog-5.php;

Added new functions:
dba_key_split() to split inifile keys in an array. (Marcus)
time_nanosleep() signal safe sleep (Magnus, Ilia)
headers_list(). (Sara)
php_strip_whitespace(). strip whitespace & comments from a script. (Ilia)
php_check_syntax(). check php script for parse errors. (Ilia)
image_type_to_extension(). return extension based on image type. (Ilia)
stream_socket_sendto() and stream_socket_recvfrom(). (Wez)
iconv_mime_decode_headers(). (Moriyoshi)
get_declared_interfaces(). (Andrey, Marcus)
sqlite_fetch_column_types(). (Ilia)

the php_check_syntax() function was only added in PHP5, and there is no alternative in PHP4, the only way to check the syntax would be to write or download a pretty damn complex function.

Zac Charles
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 11675535
Well you could emulate it by writing your file out and running 'php -l' on it using exec or similar, assuming that your host has a PHP CLI or CGI version available.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 7

Expert Comment

by:zac_charles
ID: 11675552
cryptic_mauler, while you are in the same thread as me, im sorry about that incedent before, i can explain it all to you in an email if you want?
0
 

Author Comment

by:BigglesZX
ID: 11675562
Ah, I think I see. So setting to only show parse errors - is that effectively the same as physically reading through a script to make sure all statements are syntactically correct - in so much as you can't actually test the running of the script because you can't check external links, includes and such?
If so, I understand :-) . Thank you.
0
 
LVL 25

Accepted Solution

by:
Marcus Bointon earned 100 total points
ID: 11675619
The problem with trying to syntax check PHP from within PHP is that doing something like an eval on an external file will produce a fatal error if there is a syntax error in it. This should do what you're after:

$path = './myfile.php';

$result = exec("php -l $path");
if (strpos($result, 'No syntax errors detected') !== false)
  echo "File $path is OK\n";
else
  echo "File $path is not OK: $result";
0
 

Author Comment

by:BigglesZX
ID: 11675706
zac_charles: What I meant in my original question was "is it possible to achieve the same functionality as php_check_syntax() by using other functions. I know it doesn't exist in PHP4, or I would be using it :-). Thank you though.

Squinky: Yes, that is an option for me. I will try it, thanks.
0
 

Author Comment

by:BigglesZX
ID: 11675751
Squinky: I'm lagging with replies here :) . I meant above that your CLI option is possible, but looking at your latest comment I think that will work nicely for me. Many thanks.
0
 

Author Comment

by:BigglesZX
ID: 11676062
Okay, going to split somewhat between Squinky and Cryptic_Mauler, since Squinky provided some nice code but C_M also provided a nice idea, both of which I might well use.

Thanks all, g'day.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP Query return divisible by 3 3 26
Creating Functions in phpMyAdmin 8 26
Undefined variable problem 5 23
PHP: Best way to scan folders and process files 10 40
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question