stop checking code syntax after some command? - php

echo $a;
exit();
die();
eecho $b; // line 40
Error - syntax error on line 40...
Why exit or die don't work in such a case?
How to stop checking code syntax after some command?

One method you can use is to comment the rest of the code.
echo $a;
/* Comment rest of the file
exit();
die();
eecho $b; // line 40
Or if you want to "enable" the code on line 41 again you put */ to stop the comment.
You don't have to put a */ in the code if you only use it for debugging (as I do).
It will create an notice:
Warning: Unterminated comment starting line 4 in /in/jkqu0 on line 4
But as long as it's only debugging then it's no harm done.
https://3v4l.org/jkqu0

eecho $b; ????
Because php script execution works that way. php parser first check for php syntax in the script if it found any error it throws it before interpret it.
Steps of php script execution.
Lexing
Parsing
Compilation
Interpretation

Related

remove last line(s) if they are empty from a bunch of files

Some hosting provider I work with is very strict, if the last line(s) of a php file are empty it throws a fatal error and the site is broken.
The problem is that I've got a lot of php files where the last line might be empty and now I am looking for a way to automatically remove those lines...
I've tried to use sed:
sed -i '${/^$/d}' bla.php
but all i get is an error
sed: 1: "bla.php": extra characters at the end of d command
btw: i am using iTerm on the Mac
You can do something like this within your loop.
file_put_contents('[target_file]', implode('', file('[file_being_processed]', FILE_SKIP_EMPTY_LINES)));
The target_file would be your new copy of the file you're processing and the file_being_processed is self explanatory. You would need to add more logic into the loop to keep the same names.
Bare in mind the above code will exclude all blank lines no matter where they are, so you will also need to add some logic to only apply the above code to the last line of the file.
Have you tried turning off error_reporting?
Insert the following on the first line of your php files
<?php error_reporting(0); ?>

How do I get ini_set('display_errors') to work properly? [duplicate]

This question already has answers here:
error_reporting(E_ALL) does not produce an error
(7 answers)
Closed 6 years ago.
In PHP 5.5.9 ini_set('display_errors', 1) can fail, as in:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
//nosuchfunction();// Display Errors succeeds. "Fatal error: Call to undefined function"
$test=array(1 2 ); // Display Errors fails! No error displayed. HTTP ERROR 500
?>
How can I fix this?
tl;dr Your first sample error—a call to an undefined function—is one you can display by using ini_set() in the same script. The second error—a syntax error in array(1 2 )—cannot be displayed using ini_set() in the same script because PHP never even starts to execute the broken script. To view that error, you have to do one of three things: (1) change the config file, (2) call ini_set() in another script and include the broken script, or (3) just look at the server logs instead of displaying the error on-screen.
The Problem with the Script
Your problem isn't with ini_set(); it's a syntax error with this line:
$test=array(1 2 );
As you know, that is invalid PHP because you just have 2 adjacent numbers, with no comma separator. From the manual (emphasis added):
An array can be created using the array() language construct. It takes any number of comma-separated key => value pairs as arguments.
You probably meant:
$test=array(1, 2 );
// ^ Notice the comma. This is important!
Why You See No Output
The syntax error causes a parser error, a/k/a the "white screen of death." The parser gives up and dies at the parsing stage. That means it never runs a single line of the script, including your ini_set() or error_reporting() calls. So, you don't get any output, though the error would still show up in your server logs.
The critical point: The PHP engine never runs any part of a script with a syntax error. This means ini_set() and error_reporting() calls don't run, either. You have to set the configuration option before PHP chokes on the broken script. You can do that in php.ini or in another file. You cannot do it in the same file that contains the syntax error.
How to Force On-Screen Output of the Error
Based on the comments, it sounds like you are trying to cause a parser error and then display it on the screen. You can't do this with ini_set() in the same script because PHP fails at the parsing stage. That is, it can't even interpret the script safely, so it stops trying, and no lines of code in that script are ever executed.
To work around this, you need to do one of two things, as explained in this canonical answer:
Add these lines to your php.ini file:
error_reporting = E_ALL
display_errors = 1
or
wrap the broken script in an include or require, like this:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
include("./broken-script.php");
Nota bene: Never do this in production. If you want to do it in development or QA, fine, but displaying raw PHP errors on-screen in production is a security risk.

Backtrace to mysql_close warnings in PHP

I'm redoing a large portion of my website to use older mysql_* extensions because they execute faster. Yes I know people say its not a good idea to use older extensions, but I need to go for speed since this code is part of the back-end operations to my website and I want to serve as many people as possible.
I do have a small issue after converting things over. Sometimes when I try to close a database connection I get a PHP warning of:
PHP Warning: mysql_close(): 33 is not a valid MySQL-Link resource in /path/to/script.php on line #
and the line refers to the code below:
function DBclose($c){
if (isset($c) && !mysql_close($c)){
error_log("DB handle is invalid/null. Called from ".debug_backtrace()[2]['function'].'->'.debug_backtrace()[1]['function']);
}
}
This only happens sometimes.
What I want to do is change only this warning so that it includes the function that called it. Maybe to something more like:
PHP Warning: mysql_close(): 33 is not a valid MySQL-Link resource in /path/to/script.php on line # called from parent function <function-name>
How do I fix this and still make it so that other function calls I make that begin with # will have no errors printed on screen?
I don't think you can change the message, however, you can check if it's a resource using is_resource()

PHP is calling error on code that in not being run?

I was troubleshooting some code and I took it out of the execution path so that I could look in other places.
PHP continued to call out the code as an error despite it not being executed. It is in a function that is not called.
Is this expected behavior?
// not in execution path
$temp1 = $this->hash;
$temp = array('1', $temp1); // this is line 121
$this->database_object->_pdoQuery('none', 'update_picture', $temp );
syntax error, unexpected T_LNUMBER, expecting ']' in
/home/foo/public_html/dev/foo/source/class.MAppPicture.php
on line 121
The entire file has to be parsed prior at the time of access or include. This is generating an error because it is a parse time error and php can't understand the file as a whole because of the syntax error.

How to close a string the way that bash does in php CLI or suppress error output?

If you create a simple php script with this code:
shell_exec('"');
And run it with:
php myscript.php
It gives the following error in bash on my Mac:
sh: -c: line 0: unexpected EOF while looking for matching `"'
sh: -c: line 1: syntax error: unexpected end of file
I've tried everything I can think of:
ob_start();
#shell_exec('"');
ob_end_clean();
#shell_exec('" 2> /dev/null');
But no matter what I try, I can't suppress the message. The problem is that I'm creating a unit test to stress test $argc, $argv and the getopt() command for a general purpose tool, and I need to call the script hundreds of times with various inputs containing random characters like '"=: etc.
So I need to be able to either suppress the error output, or detect imbalanced quotes and append either a ' or " to the end of the string. I found this regex to detect single and double quoted strings:
PHP: Regex to ignore escaped quotes within quotes
But I'm having trouble visualizing how to do this for a general bash command that has a mix of quoted and unquoted arguments. Is there a built-in command that would tell me if the string is acceptable without throwing an error? Then I could try appending either "'" or '"' and I'm fairly certain that one of them would close the string. I'm only concerned with preventing the error message for now, because I'm just throwing random input at the script at this point.
Thanks!
The child shell process is writing the error message to STDERR, which it inherited from the parent PHP process. You could close the parent's STDERR file handle in the PHP script before running shell_exec().
<?php
fclose (STDERR);
shell_exec('"');

Categories