Execute php script in a bat file: Difference between revisions

Jump to navigation Jump to search
m
Text replacement - ": Image:Owl icon.jpg " to "{{Tips}} "
Tags: Mobile edit Mobile web edit
m (Text replacement - ": Image:Owl icon.jpg " to "{{Tips}} ")
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
How to execute php script in a bat file
How to execute php script in a batch (bat) file


== Case 1: Execute the PHP code directly ==


# Enter {{kbd | key=cmd}} to open Windows Command Line
== BAT and Bash File Examples ==
# Enter the following command in Windows Command Line or save as BAT file:
=== Example of BAT file for Windows ===
 
<pre>
c:\path\to\php.exe -r "echo 'Hello';"
</pre>
 
: [[Image:Owl icon.jpg]] If you do not known where is the path of php, you may open the DOS window ({{kbd | key=<nowiki>cmd</nowiki>}}) and key in {{kbd | key=<nowiki>where php</nowiki>}} to obtain the path of php.
 
== Case 2: Execute the PHP script file without passing parameters ==
<pre>
c:\path\to\php.exe -f "c:\path\to\php_script.php"
 
or
 
c:\path\to\php.exe "c:\path\to\php_script.php"
</pre>
 
== Case 3: Execute the PHP script file passing parameters ==
<pre>
c:\path\to\php.exe -f "c:\path\to\php_script.php" -- -var1=value1
 
or
 
c:\path\to\php.exe "c:\path\to\php_script.php" -var1=value1
</pre>
 
 
Options "-f <file>        Parse and execute <file>."<ref>[http://php.net/manual/en/features.commandline.options.php PHP: Options - Manual]</ref>
 
== Example of bat file ==
Condition 1: The DOS window was closed automatically when PHP script which implemented [http://php.net/manual/en/function.exit.php exit()] and return error code 1.
Condition 1: The DOS window was closed automatically when PHP script which implemented [http://php.net/manual/en/function.exit.php exit()] and return error code 1.
<pre>
<pre>
Line 52: Line 22:
</pre>
</pre>


== Example of bash file ==
=== Example of BASH file for Linux ===
PHP script which implemented [http://php.net/manual/en/function.exit.php exit()] and return error code 1<ref>[https://stackoverflow.com/questions/6827837/accessing-errorlevel-from-bash-script cmd - accessing ERRORLEVEL from bash script - Stack Overflow]</ref><ref>[https://www.tldp.org/LDP/abs/html/comparison-ops.html Other Comparison Operators]</ref><ref>[https://unix.stackexchange.com/questions/293940/bash-how-can-i-make-press-any-key-to-continue Bash - How can I make "Press any key to continue" - Unix & Linux Stack Exchange]</ref>.
PHP script which implemented [http://php.net/manual/en/function.exit.php exit()] and return error code 1<ref>[https://stackoverflow.com/questions/6827837/accessing-errorlevel-from-bash-script cmd - accessing ERRORLEVEL from bash script - Stack Overflow]</ref><ref>[https://www.tldp.org/LDP/abs/html/comparison-ops.html Other Comparison Operators]</ref><ref>[https://unix.stackexchange.com/questions/293940/bash-how-can-i-make-press-any-key-to-continue Bash - How can I make "Press any key to continue" - Unix & Linux Stack Exchange]</ref>.
<pre>
<pre>
Line 62: Line 32:
fi
fi
</pre>
</pre>
== Use cases ==
=== Case 1: Execute the PHP code directly ===
# Enter {{kbd | key=cmd}} to open Windows Command Line
# Enter the following command in Windows Command Line or save as BAT file:
<pre>
c:\path\to\php.exe -r "echo 'Hello';"
</pre>
{{Tips}} If you do not know where PHP installed, you may open the DOS window ({{kbd | key=<nowiki>cmd</nowiki>}}) and key in {{kbd | key=<nowiki>where php</nowiki>}} to find the path.
=== Case 2: Execute the PHP script file without passing parameters ===
<pre>
c:\path\to\php.exe -f "c:\path\to\php_script.php"
or
c:\path\to\php.exe "c:\path\to\php_script.php"
</pre>
=== Case 3: Execute the PHP script file passing parameters ===
<pre>
c:\path\to\php.exe -f "c:\path\to\php_script.php" -- -c=3
or
c:\path\to\php.exe "c:\path\to\php_script.php" -a=2
</pre>
Options "-f <file> Parse and execute <file>."<ref>[http://php.net/manual/en/features.commandline.options.php PHP: Options - Manual]</ref>


== Run the PHP script automatically at a specified time ==
== Run the PHP script automatically at a specified time ==
Line 110: Line 114:
* [http://php.net/getopt PHP: getopt - Manual]
* [http://php.net/getopt PHP: getopt - Manual]
* [[Boot up Tasks]] / [[Boot up Tasks#自動執行程式 | 自動執行程式]]
* [[Boot up Tasks]] / [[Boot up Tasks#自動執行程式 | 自動執行程式]]
* [[How to run a scheduled task in the background]]
* [https://errerrors.blogspot.com/2019/01/how-do-I-run-a-php-script-using-windows-system-scheduler.html 如何設定 Windows 排程自動執行 PHP 程式]
* [https://errerrors.blogspot.com/2019/01/how-do-I-run-a-php-script-using-windows-system-scheduler.html 如何設定 Windows 排程自動執行 PHP 程式]
* [[A simple bat file template]]


== References ==
== References ==

Navigation menu