Is there a way, from a bash script and/or a terminal, to run php interactively and load in a predefined file at the same time?
Essentially, I want to do the following two steps in a single step:
shell# php -a
Interactive mode enabled
php > require_once('ABSOLUTE_PATH_TO_FILE');
I tried using php -a --file='ABSOLUTE_PATH_TO_FILE'
but the functions I want to load do not become available in interactive mode.
The CLI SAPI provides an interactive shell using the -a option if PHP is compiled with the --with-readline option. As of PHP 7.1. 0 the interactive shell is also available on Windows, if the readline extension is enabled. Using the interactive shell you are able to type PHP code and have it executed directly.
To pass command line arguments to the script, we simply put them right after the script name like so... Note that the 0th argument is the name of the PHP script that is run. The rest of the array are the values passed in on the command line. The values are accessed via the $argv array.
PHP's Command Line Interface (CLI) allows you to execute PHP scripts when logged in to your server through SSH. ServerPilot installs multiple versions of PHP on your server so there are multiple PHP executables available to run.
If you have a test.php
file with this contents
<?php
function asd() {
echo "Hi!";
}
?>
You must use:
php -a -d auto_prepend_file=test.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With