Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Interactive - Load File From Command Line

Tags:

bash

shell

php

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.

like image 842
sgcharlie Avatar asked Oct 20 '14 19:10

sgcharlie


People also ask

How do I run the interactive PHP shell from the command line interface?

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.

How do I pass a command line argument in PHP?

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.

What is CLI PHP?

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.


1 Answers

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
like image 71
Jorge Fuentes González Avatar answered Sep 23 '22 12:09

Jorge Fuentes González