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);
Related
So my PHP file is meant to check if a variable is null, if so than echo and output, and stop there
Here is that code:
if(is_null($ip)){
echo "IP is not valid";
clean_all_processes();
}
So when I try to test this script using the insomnia rest client it outputs the "IP is not valid" but also gives a "500 internal server error"
In my error_log file it spits out this every time
Uncaught Error: Call to undefined function clean_all_processes()
Note: I am using php 7.3
There is no such function called clean_all_processes() in PHP. The answer you linked to used it as an example name of a function you could call.
If you want a hard stop of your script use die(). This is not recommended! You should structure your code in such a way that you should almost never need to use this approach.
There is no way to break out of if statement, because such thing makes no sense. An if statement is already a condition. You either execute the code or don't.
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
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.
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
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.