Run function php command line
I have a file called address.php with a few functions in it. I want to call a specific function in that file from the command line. How?
The name of the function is called exportAddress and that function expects a single parameter.
asked Dec 7, 2012 at 11:10
By using the
There are no other options. A function in PHP can only be called by a PHP script.
answered Dec 7, 2012 at 11:12
Tim S.Tim S.
13.2k7 gold badges43 silver badges71 bronze badges
answered Dec 7, 2012 at 11:19
5,0648 gold badges40 silver badges69 bronze badges
Add this to the top of the file "/var/www/test/address.php"...
Then from the command line, execute:
answered Sep 4, 2014 at 5:12
You can make your file "somefile.php" organized as follows:
Then from the command line or a Linux cronjob, you run the following command:
answered Nov 18, 2015 at 9:50
Samer AtaSamer Ata
1,0171 gold badge12 silver badges11 bronze badges
As of PHP 5.1.0 (7.1.0 on windows) PHP can be executed from the shell by running
This starts PHP's interactive shell. More info here.
With PHP's shell, running you can require files the same way you would from a file.
So if the previous command was run from the folder containing the file:
If it is in a subfolder:
Then execute any function defined in myFile.php.
I guess you can use any variable defined in the same file or require any other file containing the variables needed, although I haven't tried it.
answered May 15, 2020 at 19:19
631 silver badge7 bronze badges
To extend on Samer Ata and Simon Rodan's answers, you can use the following to be able to execute any function with any amount of arguments:
This is done by removing the script and function name from
The operator is supported from PHP 5.6 onward (and 5.5 can do some of the things using functions). You can read more about it in the official PHP documentation. For completeness, this is also known as argument unpacking.
answered Feb 25, 2020 at 7:53
Steen SchüttSteen Schütt
1,21718 silver badges30 bronze badges
How do I run PHP from command line?
You just follow the steps to run PHP program using command line..
Open terminal or command line window..
Goto the specified folder or directory where php files are present..
Then we can run php code using the following command: php file_name.php..
How do I run a PHP script?
php” file is placed inside the “htdocs” folder. If you want to run it, open any web browser and enter “localhost/demo. php” and press enter. Your program will run.
Can we use PHP in command line?
As of version 4.3. 0, PHP supports a new SAPI type (Server Application Programming Interface) named CLI which means Command Line Interface. As the name implies, this SAPI type main focus is on developing shell (or desktop as well) applications with PHP.
What is command line 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.