Running PHP script from the command line
You should check your server configuration files. Look for lines that start with LoadModule php
...
There probably are configuration files/directories named mods
or something like that. Start from there.
You could also check output from php -r 'phpinfo();' | grep php
and compare lines to phpinfo();
from web server.
To run php
interactively:
(So you can paste/write code in the console.)
php -a
To make it parse a file and output to the console:
php -f file.php
Parse a file and output to another file:
php -f file.php > results.html
Do you need something else?
To run only a small part, one line or like, you can use:
php -r '$x = "Hello World"; echo "$x\n";'
If you are running Linux then do man php
at the console.
If you need/want to run PHP through fpm (FastCGI Process Manager), use cli fcgi:
SCRIPT_NAME="file.php" SCRIP_FILENAME="file.php" REQUEST_METHOD="GET" cgi-fcgi -bind -connect "/var/run/php-fpm/php-fpm.sock"
Where /var/run/php-fpm/php-fpm.sock is your php-fpm socket file.
Run PHP file in Command Line using PHP script
Use include_once 'next-file.php';
to import and run the code.
Then related to PHP Doc, here is the definition of exit()
:
Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit is called.
So add this code line into the shutdown function as below:
function shutdown()
{
include_once 'next-file.php';
echo "next-file is successfully running", PHP_EOL;
}
register_shutdown_function('shutdown');
Be sure to record this function before the exit()
.
How can I to run php script from powershell-commandline?
I'm assuming windows since you said powershell. You can just install php on windows but that means also installing apache or enabling IIS.
Or there's apparently a built-in webserver for command-line functionality that might minimize the amount of headache involved in configuring that stuff.
This might help get you going also:
http://php.net/manual/en/install.windows.legacy.index.php#install.windows.legacy.commandline
Run php script from command line with variable
Script:
<?php
// number of arguments passed to the script
var_dump($argc);
// the arguments as an array. first argument is always the script name
var_dump($argv);
Command:
$ php -f test.php foo bar baz
int(4)
array(4) {
[0]=>
string(8) "test.php"
[1]=>
string(3) "foo"
[2]=>
string(3) "bar"
[3]=>
string(3) "baz"
}
Also, take a look at using PHP from the command line.
Running php file from command prompt and taking input
you can do this:
$input = trim(fgets(STDIN));
fgets takes input from STDIN and waits for a linebreak.
The trim function makes sure you don't have that line break at the end.
So to add this in your example:
echo "ENTER YOUR NUMBER";
$num = trim(fgets(STDIN));
echo "Enter your message";
$msg = trim(fgets(STDIN));
echo $num;
echo $msg;
Run .php file in Windows Command Prompt (cmd)
If running Windows 10:
- Open the start menu
- Type
path
- Click Edit the system environment variables (usually, it's the top search result) and continue on step 6 below.
If on older Windows:
Show Desktop.
Right Click My Computer shortcut in the desktop.
Click Properties.
You should see a section of control Panel - Control Panel\System and Security\System.
Click Advanced System Settings on the Left menu.
Click Enviornment Variables towards the bottom of the System Properties window.
Select PATH in the user variables list.
Append your PHP Path (C:\myfolder\php) to your PATH variable, separated from the already existing string by a semi colon.
Click OK
Open your "cmd"
Type PATH, press enter
Make sure that you see your PHP folder among the list.
That should work.
Note: Make sure that your PHP folder has the php.exe. It should have the file type CLI. If you do not have the php.exe, go ahead and check the installation guidelines at - http://www.php.net/manual/en/install.windows.manual.php - and download the installation file from there.
Run PHP script with command line globally on win10
I finally solved it:
-Create a .bat file where your php file in, it contains:
@ECHO OFF
php %~dp0/update.php %*
-And save it as "update.bat"
-Add the directory name into "Path" in "Environment Variables"
Now you can write "update var1=abc var2 var3=def" freely in command prompt!
How can I execute PHP code from the command line?
If you're going to do PHP in the command line, I recommend you install phpsh, a decent PHP shell. It's a lot more fun.
Anyway, the php
command offers two switches to execute code from the command line:
-r <code> Run PHP <code> without using script tags <?..?>
-R <code> Run PHP <code> for every input line
You can use php
's -r switch as such:
php -r 'echo function_exists("foo") ? "yes" : "no";'
The above PHP command above should output no
and returns 0
as you can see:
>>> php -r 'echo function_exists("foo") ? "yes" : "no";'
no
>>> echo $? # print the return value of the previous command
0
Another funny switch is php -a:
-a Run as interactive shell
It's sort of lame compared to phpsh, but if you don't want to install the awesome interactive shell for PHP made by Facebook to get tab completion, history, and so on, then use -a as such:
>>> php -a
Interactive shell
php > echo function_exists("foo") ? "yes" : "no";
no
php >
If it doesn't work on your box like on my boxes (tested on Ubuntu and Arch Linux), then probably your PHP setup is fuzzy or broken. If you run this command:
php -i | grep 'API'
You should see:
Server API => Command Line Interface
If you don't, this means that maybe another command will provides the CLI SAPI. Try php-cli; maybe it's a package or a command available in your OS.
If you do see that your php
command uses the CLI (command-line interface) SAPI (Server API), then run php -h | grep code
to find out which crazy switch - as this hasn't changed for year- allows to run code in your version/setup.
Another couple of examples, just to make sure it works on my boxes:
>>> php -r 'echo function_exists("sg_load") ? "yes" : "no";'
no
>>> php -r 'echo function_exists("print_r") ? "yes" : "no";'
yes
Also, note that it is possible that an extension is loaded in the CLI and not in the CGI or Apache SAPI. It is likely that several PHP SAPIs use different php.ini files, e.g., /etc/php/cli/php.ini
vs. /etc/php/cgi/php.ini
vs. /etc/php/apache/php.ini
on a Gentoo Linux box. Find out which ini file is used with php -i | grep ini
.
Related Topics
How to Extend the Zend Navigation Menu View Helper
Get Refunded Orders and Refunded Order Items Details in Woocommerce 3
Select MySQL Rows But Rows into Columns and Column into Rows
PHP Regex for Validating a Url
PHP Using Declare? What Is a Tick
Php/Pdo/Mysql: Inserting into Mediumblob Stores Bad Data
Fatal Error: Call to Undefined Function Openssl_Random_Pseudo_Bytes()
Override External Product Url to "Add to Cart" Product Button
Opposite of Nl2Br? Is It Str_Replace
Mysqli_Fetch_Array Returning Only One Result
How to Make Sure That Values from MySQL Keep Their Type in PHP
Looping a SQL Insert Statement with Dates
Submit Multiple Forms with One Button
Differencebetween the | and || Operators
Nested PHP Ternary Trouble: Ternary Output != If - Else
Laravel Gmail Not Working, "Username and Password Not Accepted. Learn More..."
Create Programmatically a Product Using Crud Methods in Woocommerce 3
Inspect Xml Created by PHP Soapclient Call Before/Without Sending the Request