How to find PHP parse errors - php

I have a PHP script, util.php, that I call from jQuery:
$("#galleryContent").load("util.php",
{op : 'get_thumbs'
},
function() {
$('.galleryThumb').draggable(thumb_dragOps);
}
);
Anything the script echoes shows up in #galleryContent.
But the script has acquired a bug such that nothing happens now when it's called. Even if I put an
echo ("In util.php");
at the very top, this doesn't show up in #galleryContent. If I delete all of the code after the echo down to ?> then the echo does show up. So something is keeping util.php from parsing and running. There's a missing ";" a missing "}", or something like that.
My question is, why isn't something telling me were that bug is? I have
ini_set('display_errors', 1);
error_reporting(E_ALL);
at the top of the script but these seem to only help with run time errors, not parse errors. What can I use to find a parse error?
Thanks.

Parsing errors cannot be configured in run-time via ini_set or error_reporting, you need to use php.ini set display-startup-errors to 1 and restart apache
second - you can try run php -l <filename> to get syntax errors

Related

php Error message not browsed inside an include() function

I have a problem when testing my php code file. Despite having error handling in test mode (I mean, 8191 E_ALL), the errors of the code that is added within an included file are not browsed, and the execution of the script is stopped. If the included file is large, I spend many time until I find the error, what can I do to display the error even if it is inside an included file?
Example:file code.php
echo '<p>beginning of the script</p>';
include_once('code2.php');
echo '<p>end of the script</p>';
The file code2.php has a parse error:
echo 'hello'
The script is stopped before browsing "end of the script", but no error messages are browsed.
Many thanks,
This code does not execute bcoz php thinks code2.php is a variable, to fix that surround it with single quotes 'code2.php' .
In order to see errors messages make sure you have <?php not just <? and error_reporting(E_ALL);

suppress parse errors in PHP

so I'm trying to prevent fatal errors from stopping my script from running
so I set error report to 0:
error_reporting(0);
then I added some junk code afterwards..
junk code~~~~trololololololol
However, PHP ends up returning parse error nonetheless:
Parse error: syntax error, unexpected 'require_once' (T_REQUIRE_ONCE) in etc
is there a way to prevent PHP from ceasing to execute when there are parse errors or is this a hopeless endeavor?
is there a way to prevent PHP from ceasing to execute when there are parse errors or is this a hopeless endeavor?
Yes, that's hopeless. It's impossible to resume execution after junk was found, there just isn't any predictable state left for the compiler to adhere to. It might be missing random functions, variable definitions and what not, so all it can logically do is stop working.
You can suppress the error but not force it to continue working after the digital equivalent of a violent heart attack.
You can't. PHP first reads the text in your script from beginning to end (parses it) and converts it into some form of low-level bytecode that can be executed. If it fails to parse, then there's no executable code at all and it can only fail.
With runtime errors, your code is already executing and PHP already knows if you've got some way to handle that error. But with a parse error, there's no program there to begin with.
Yes, you can handle parsing errors. Take a look at this example:
index.php
<?php
ini_set('display_errors', '0');
register_shutdown_function('err_handler');
$modules = array('module1.php', 'module2.php', 'module3.php');
load_modules($modules);
function load_modules(&$modules) {
foreach ($modules as $key => $module) {
unset($modules[$key]);
include $module;
}
}
function err_handler() {
global $modules;
load_modules($modules);
}
module1.php
<?php junk code~~~~trololololololol;
module2.php
<?php junk code~~~~trololololololol;
module3.php
<?php echo "Surprise!\n";
Wisely using the register_shutdown_function(), set_error_handler() and set_exception_handler() you can make your script absolutely immune to all type of PHP errors. This code can be treated as an example of defensive programming technique in PHP.
If there is a parse error then php can't execute the script at all, so it won't run the error_reporting(0);, and so it will fall back to whatever the setting was for error_reporting and display_errors in the php.ini file. So you CAN prevent the parsing error being shown by editing the php.ini file, but then that will switch it off globally.
If you want the display of errors to be on by default globally (not good for production!), then you might be able to switch them off locally with a .htaccess setting, since this is read before parsing the php script.

importing files in php using require() not working

i tried the following code to import two files
<?php echo "php";
require('../globalvasr.php') or die("error");
require('../newcosn.php') or die("error2");
$config = new GlobalConfigs();
?>
It does not shows error and it just simply displays a blank page.Also i am unable to use the variable defined in those two files.
Like $config->DBNAME.
I dont know whats wrong in this.
Please help me find it.
Thank you.
require, in contrast to include, automatically dies and does not have a return value.
This means the or die() is bad. Better:
<?php echo "php";
require('./globalvasr.php');
require('./newcosn.php');
$config = new GlobalConfigs();
?>
Require generates a fatal error when require fails, causing the script execution to stop immediatly.
As you seem to be running in a web env, your output (all echo or print statements) is buffered until the end of the script.
So here the require fails, causing a fatal error (that should be available in the error log) before the output buffer is emptied, preventing your first "echo" to be sent to the browser. that's why you get a blank page.
Try replacing the require with an include, you will get a warning instead of the fatal error.
if you have blank page, set error_reporting: E_ALL and Display_errors: On in php.ini or put on start of your script these lines
error_reporting(E_ALL);
ini_set('Display_errors','On');
then you will see errors

HTTP Error 500 when running php scripts

I am running a PHP page and as soon as I introduce calls like this: $_GET('') then everything goes wrong and I get an error 500.
This code goes not work:
echo $_GET('username');
echo $_GET('password');
?>
This code does:
<?php
phpinfo();
?>
The above code has syntax errors - you need to use square brackets.
The web server's error logs will show you those errors if you have access to them.
Use this:
echo "Username: ".$_GET['username']."<br />Password: ".$_GET['password'];
Since $_GET is a array and not a function, you need to use [square brackets] instead of (normal brackets) to retrieve the data out of a array.
To figure out what the problem is you need to turn on php error reporting. You do this by running this the first thing you do in your php-file:
ini_set('display_errors',1);
error_reporting(E_ALL);
E_ALL means the interpreter will show you errors, warnings and notices. After that, everything will be pretty obvious since php will tell you what went wrong.

PHP turn off errors - in one file only

I am well aware about error_reporting(0); & ini_set('display_errors', "Off"); to make error messages go away.
What would be an appropriate way to do this - for a specific file or part of code only?
Surpressing errors with #'s seems like a bad idea since it apparently slows the code down...
The reason? We have a number of memcached servers in a development LAN that is really unreliable due to the network settings, thereby we are recieving errors multiple times every hour and there's nothing we can do about it except stop using memcache or turning off errors for the whole application, which would be giving us a headache - in the middle of the development stage :)
<?php
// normal code
// error_reporting returns the old error code
$old_error_reporting = error_reporting(0);
// your errorful code
// reset error_reporting to its old value
error_reporting($old_error_reporting);
// normal code
Although it would be a good idea to fix what is actually causing the errors.
You've kind of answered your own question. To do it for a specific file, error_reporting(0); will turn off errors. You can also call it multiple times in a script, I think.
You can also use php exceptions to 'catch' errors over a block of code. For example:
try {
// code to ignore errors for here
} catch {
// you can output a custom error here, but putting nothing here will effectively mean errors are ignored for the try block
}
The script will continue running past the try block, even if there is an error within it. See the PHP Manual Entry for more information.
You can change the error reporting level during runtime:
<?
error_reporting(E_ALL);
... some code ....
error_reporting(0);
... some more code ....
error_reporting(E_ALL);
I know of no other way but I can't think of a case where this wouldn't be sufficient. Can you?
That's really a long time ago but someone like me would maybe use my answer.
When i need to do this kind of stuff, i just put # before the variable in order to NOT display the errors coming from this variable.
example:
switch(#$var!="e") {
....
}

Categories