micksulley Posted December 30, 2021 Share Posted December 30, 2021 When trying to diagnose a problem with php I often run it from command line to see the "unexpected < at line 123" or similar message. This is very useful for simple scripts but if I have includes etc it shows them as an error as the path is different. I have done a bit of research and understand why this happens, but is there a way to overcome this? I don't know of any other way to get anything like a trace of execution. Running PHP Version 7.3.29-1~deb10u1 on Linux Mint 20.2 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/314368-running-php-from-command-line/ Share on other sites More sharing options...
gw1500se Posted December 30, 2021 Share Posted December 30, 2021 No need to run it from command line, you can just as easily debug PHP using a browser. Just be sure to turn on errors: error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/314368-running-php-from-command-line/#findComment-1593069 Share on other sites More sharing options...
micksulley Posted December 30, 2021 Author Share Posted December 30, 2021 I have ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(E_ALL); at the start of my code. That picks up some errors but not all. I have just opened a web page and it is completely blank, run it in command line and it tells me PHP Parse error: syntax error, unexpected '}', expecting end of file in /home/pi/v04_0/www/model/index.php on line 17 Easy to fix in this case, but for more involved code it is difficult to track down problems. Quote Link to comment https://forums.phpfreaks.com/topic/314368-running-php-from-command-line/#findComment-1593071 Share on other sites More sharing options...
mac_gyver Posted December 30, 2021 Share Posted December 30, 2021 you must find and set those three php error settings in the php.ini on your system, because - your main code file never runs upon a php syntax error, so putting the settings in your code won't do anything for this type of error. the display_startup_errors setting does nothing when in your php code because php has already started. while you are changing settings in the php.ini, find the output_buffering setting and set it to OFF. php's output buffering should only be used when you want to buffer output and it being on hides non-fatal php errors and any output from your code when you have header statements in your code. by putting these in the php.ini, you have one location where you can change them and you don't need to edit any code when you move your project to a live/public server. stop and start your web server to get any changes made to the php.ini to take effect and then use a phpinfo() statement in a .php script that you open as a web page to conform that the settings actually got changed in case the php.ini that you are changing is not the one that php is using. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314368-running-php-from-command-line/#findComment-1593072 Share on other sites More sharing options...
ginerjm Posted December 30, 2021 Share Posted December 30, 2021 I use a little stub of a script to debug those errors that prevent a script from running. if (isset($_GET['call'])) { // test the script named as the 'call' arg in the url $call = $_GET['call']; require $root . $call; } else { echo "Must supply a call= argument for the script to be tested"; echo "root is $root<br>"; } //******************************** exit(); Be sure to add the error setting lines at the top of this and then save it. I call mine 'redirect.php' and add a ?call=scriptname in the command line when I use it. Note that it won't run if you already have get parms in your problem script so simply provide only the script name. Quote Link to comment https://forums.phpfreaks.com/topic/314368-running-php-from-command-line/#findComment-1593081 Share on other sites More sharing options...
requinix Posted December 30, 2021 Share Posted December 30, 2021 Just make sure not to put ginerjm's code onto a real server running on the internet. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314368-running-php-from-command-line/#findComment-1593082 Share on other sites More sharing options...
micksulley Posted December 31, 2021 Author Share Posted December 31, 2021 Thanks for the replies, settings in php.ini makes much more sense:) ginerjm - I think I am missing something. I put ph tags around your code to get it to run, but it just reports 'Undefined variable: root' do I need to put something in there? Mick Quote Link to comment https://forums.phpfreaks.com/topic/314368-running-php-from-command-line/#findComment-1593095 Share on other sites More sharing options...
ginerjm Posted December 31, 2021 Share Posted December 31, 2021 (edited) $root is the path to your domain's root. As in: $root = $_SERVER['DOCUMENT_ROOT'] . "/"; Sorry - didn't see that was in there. And don't "put tags around" the code. All one ever needs is the <?php tag. As already mentioned the ?> tag is not needed. And don't forget to add the lines that enable error checking to this as I noted. Edited December 31, 2021 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/314368-running-php-from-command-line/#findComment-1593105 Share on other sites More sharing options...
gizmola Posted January 6, 2022 Share Posted January 6, 2022 There shouldn't ever be a situation where a syntax error causes your page not to work, if you use a php aware editor that includes syntax checking/lint. Of course there are many other types of runtime errors that can happen, and those settings are good for development. There's also xdebug you can use for debugging, again with a modern PHP editor. The most popular commercial/pro editor is PHPStorm. If you want to go free, I'd suggest using Visual Studio Code, with some PHP plugins, like Inteliphense, PHP Debug and maybe one of the plugins that wrap formatting tools, php cs fixer as an example. You do have to figure out how to get plugins like that working with a local install of the formatting tool. Quote Link to comment https://forums.phpfreaks.com/topic/314368-running-php-from-command-line/#findComment-1593212 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.