Jump to content

Running PHP from Command Line


micksulley

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

you must find and set those three php error settings in the php.ini on your system, because -

  1.  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.
  2. the display_startup_errors setting does nothing when in your php code because php has already started.
  3. 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.
  4. 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.

  • Like 1
Link to comment
Share on other sites

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.  

Link to comment
Share on other sites

$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 by ginerjm
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.