Using command line tools in PHP_PHP Tutorial
If you have used PHP, you will find that it is an excellent tool for creating feature-rich web pages. As a major scripting language, PHP:
·Easy to learn.
There are many powerful frameworks (such as CakePHP and CodeIgniter) that allow you to be as productive as a Rails programmer.
·Ability to communicate with MySQL, PostgreSQL, Microsoft SQL Server, and even Oracle.
·Ability to easily integrate with JavaScript frameworks such as script.aculo.us and jQuery.
But sometimes, you want to do more, or have to do more. What I mean is that you have to deal directly with the file system of the server where PHP is running. You will eventually need to work with files on the file system, learn about running processes, or perform other tasks.
First, you are satisfied with opening files in PHP using the file() command. But at some point, the only way to accomplish something is to run a shell command on the server and get a specific output. For example, you might want to know how many files a specific directory contains. Or you want to know how many lines were written to a certain set of log files. Or you want to manipulate the files, copy them to another directory, or use rsync to send them to another location.
In the article "PHP Command line? Yes, you can!" Roger McCoy demonstrates how to use PHP - No web browser required. In this post, I look at the same topic from another angle, showing you how to tightly integrate with the underlying shell commands and include return values into your interface and processes. These operations only work if you are running on Linux, Berkeley Software Distribution (BSD), or some other UNIX version. I'm assuming you're running on the Linux-Apache-MySQL-
PHP(LAMP) stack. If you are running another version of UNIX, the specific details may be different because the availability of the command line is different in each version. I know many people are still developing on Mac OS X (running some version of BSD), so I tried to keep the example commands generic to ensure easy portability.
Command line overview
PHPCommand Line Interface (CLI) Server Application Programming Interface (SAPI) was released in PHP V4.2.0 for experimental purposes. As of V4.3.0, it is fully supported and enabled by default. The PHP CLI SAPI allows you to develop PHP supported shell scripts, even desktop-based scripts. In fact, it is possible to use PHP to create tools that can be run directly from the command line. This way, PHP developers can be as productive as Perl, AWK, Ruby or shell programmers. This article explores the tools built into
PHPto let you understand the underlying shell environment and file system in which PHP runs. PHP provides a number of functions for executing external commands, including shell_exec(), exec(), passthru() and system(). These commands are similar but provide different interfaces for external programs you run. All these commands spawn a child process for running the command or script you specify, and each child process will write the command output to standard output ( stdout).
shell_exec()shell_exec() The
commandline is actually just a variation of the backtick (`) operator. If you've ever written a shell or Perl script, you know that you can capture the output of other commands inside the backtick operator. For example, Listing 1 shows how to use backticks to get the word count for each text (.txt) in the current directory. Listing 1. Counting words using backticks
#!/bin/sh
Number_of_words=`wc -w *.txt`
echo $number_of_words
#result would be something like:
#165readme.txt 388results.txt 588summary.txt
#andso on....
In your
PHP script, you can run this simple command in shell_exec(), as shown in Listing 2, and get the desired results. It is assumed here that there are some text files in the same directory. Listing 2. Running the same command in shell_exec() $results =shell_exec(wc -w *.txt); echo $results; ?> $results =shell_exec(wc -w *.txt); echo $results; > As you can see in Figure 1, the results obtained are the same as those obtained from the shell script. This is because shell_exec() allows you to run an external program through the shell and then returns the result as a string. Figure 1. The result of running the shell command through shell_exec() Note that just using the trailing apostrophe operator will give you the same result, as shown below. Listing 3. Using only the trailing apostrophe operator $results =`wc -w *.txt`; echo $results; ?> $results =`wc -w *.txt`; echo $results; > Listing 4 shows a simpler approach. List 4. A simpler method echo `wc -w *.txt`; ?> echo `wc -w *.txt`; > It’s important to know that a lot can be done with the UNIX command line and shell scripts. For example, you can use pipes to connect commands. You can even create a shell script in it using operators and just call the shell script (with or without arguments as needed). For example, if you only want to count the number of words in the first 5 text files in the directory, you can use a vertical bar (|) to connect the wc and head commands. Alternatively, you can place the output inside a pre tag to render it more beautifully in a web browser, as shown below. Listing 5. More complex shell Commands

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

The built-in quantization tools on the exchange include: 1. Binance: Provides Binance Futures quantitative module, low handling fees, and supports AI-assisted transactions. 2. OKX (Ouyi): Supports multi-account management and intelligent order routing, and provides institutional-level risk control. The independent quantitative strategy platforms include: 3. 3Commas: drag-and-drop strategy generator, suitable for multi-platform hedging arbitrage. 4. Quadency: Professional-level algorithm strategy library, supporting customized risk thresholds. 5. Pionex: Built-in 16 preset strategy, low transaction fee. Vertical domain tools include: 6. Cryptohopper: cloud-based quantitative platform, supporting 150 technical indicators. 7. Bitsgap:

Recommended cryptocurrency trading platforms include: 1. Binance: the world's largest trading volume, supports 1,400 currencies, FCA and MAS certification. 2. OKX: Strong technical strength, supports 400 currencies, approved by the Hong Kong Securities Regulatory Commission. 3. Coinbase: The largest compliance platform in the United States, suitable for beginners, SEC and FinCEN supervision. 4. Kraken: a veteran European brand, ISO 27001 certified, holds a US MSB and UK FCA license. 5. Gate.io: The most complete currency (800), low transaction fees, and obtained a license from multiple countries. 6. Huobi Global: an old platform that provides a variety of services, and holds Japanese FSA and Hong Kong TCSP licenses. 7. KuCoin

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json file. 1) parse composer.json to obtain dependency information; 2) parse dependencies to form a dependency tree; 3) download and install dependencies from Packagist to the vendor directory; 4) generate composer.lock file to lock the dependency version to ensure team consistency and project maintainability.

The methods to download the Hong Kong Digital Currency Exchange APP include: 1. Select a compliant platform, such as OSL, HashKey or Binance HK, etc.; 2. Download through official channels, iOS users download on the App Store, Android users download through Google Play or official website; 3. Register and verify their identity, use Hong Kong mobile phone number or email address to upload identity and address certificates; 4. Set security measures, enable two-factor authentication and regularly check account activities.

Binance Square is a social media platform provided by Binance Exchange, aiming to provide users with a space to communicate and share information related to cryptocurrencies. This article will explore the functions, reliability and user experience of Binance Plaza in detail to help you better understand this platform.

phpMyAdmin simplifies MySQL database management through the web interface. 1) Create databases and tables: Use graphical interface to operate easily. 2) Execute complex queries: such as JOIN query, implemented through SQL editor. 3) Optimization and best practices: including SQL query optimization, index management and data backup.

AI plays an important role in code generation and analysis: 1) generate code through machine learning and NLP, such as GitHub’s Copilot; 2) perform code analysis, identify bugs and optimization suggestions, such as SonarQube; 3) improve development efficiency and code quality, but requires manual review and optimization.
