Skip to content

Latest commit

 

History

History
86 lines (72 loc) · 2.32 KB

cli-with-php.md

File metadata and controls

86 lines (72 loc) · 2.32 KB

➲ CLI With PHP:

PHP offers to execute script through command line.

☴ Overview:

  1. Executing PHP Script in Command Line
  2. Accepting Command Line Arguments
  3. Common PHP CLI Options

✦ Executing PHP Script in Command Line:

To execute PHP script in command line, use php scriptname.php in command line.

php test.php

✦ Accepting Command Line Arguments:

To PHP accepts command line arguments and deliver through the variable argv.

php test.php arg1 arg2 arg3
foreach ($argv as $arg) {
    echo $arg . "\n";
}

✦ Common PHP CLI Options:

  • Basic Options:

    • -a: Interactive mode. Reads and executes code interactively.
    • -c <path>: Specifies the path to the php.ini file.
    • -d <foo=bar>: Defines an INI entry.
    • -e: Generates extended information for debuggers/profilers.
    • -f <file>: Specifies the file to execute.
    • -h: Displays help information.
    • -i: Prints information about the PHP configuration.
    • -l: Performs a syntax check only for the script.
    • -m: Lists loaded modules.
    • -n: Disables the use of php.ini.
    • -r <code>: Executes a single line of PHP code.
    • -s: Outputs a syntax-highlighted source code.
    • -v: Prints the PHP version.
  • Additional Options:

    • -B <begin_code>: Executes code before processing input lines.
    • -E <end_code>: Executes code after processing all input lines.
    • -F <file>: Parses and executes a file for each input line.
    • -R <code>: Executes code for every input line.
    • -S addr:port: Runs a built-in web server for current directory.
    • -t <docroot>: Specifies the document root for the built-in web server.

Example:

  • Checking the PHP version:
    php -v
  • Running PHP interactively:
    php -a
  • Syntax checking a script:
    php -l my_script.php
  • Checking PHP configuration:
    php -i
  • Starting a built-in web server:
    php -S localhost:8000
  • Running PHP code directly:
    php -r "echo 'Welcome';"

⇪ To Top

❮ Previous TopicNext Topic ❯

⌂ Goto Home Page