In-depth analysis of php cli script code examples

Source: Internet
Author: User
Tags cli interpreter php cli

For

The first method is to use php-f/path/to/yourfile. php. Call the php cli interpreter and pass parameters to the script. In this method, you must first set the path of the php interpreter. Before Running CLI on Windows platform, you must set a command similar to path c: php, which also loses the meaning of the first line of the CLI script, therefore, this method is not recommended.

The second method is to first run chmod + x <script file name to be run> (UNIX/Linux environment) and set the PHP file to executable permissions, then add the declaration in the first line of the CLI script header (similar #! /Usr/bin/php or php cli interpreter location), and then directly execute the command line. This is the preferred method for CLI. We recommend that you use this method.

Let's take a look at how to compile the php cli script.

Write a CLI script

First, create a php script named myfile. PHP to run the php cli script. This script is very simple and only displays "Hello php cli !". The script code is as follows:

 
 
  1. #! /Usr/local/bin/php-q
  2. <? Php
  3. // On Windows, the uplink should be:
    #! C: phpphp.exe-q
  4. Echo "Hello php cli! ";
  5. ?>

Do not forget to set the executable permission for this file:

$ Chmod 755 myfile. php

Run the following command and press Enter:

$./Myfile. php

If you want to run this script in Windows, you can directly run the php cli script without setting file attributes.

Again: on the Windows platform, the first line of the client script must write the location of php.exe, as shown in the following figure. (In addition, if you want to add a comment statement to the CLI script, you must write the comment in the PHP tag, because CLI interpretation only recognizes the first line, and does not consider it as a syntax error in the PHP tag ):

#! C: phpphp.exe-q

In this way, you can see that the information in the command line has been printed to prove that the php cli script has been successfully run.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.