Heim Backend-Entwicklung PHP-Tutorial Einführung in die Verwendung der PHP-Befehlszeile

Einführung in die Verwendung der PHP-Befehlszeile

Aug 08, 2016 am 09:30 AM
file nbsp php the

Php ist eine sehr beliebte webserverseitige Skriptsprache. Tatsächlich kann PHP nicht nur im Webserver eine wichtige Rolle spielen. Es kann auch über die Befehlszeile ausgeführt werden.

In diesem Artikel stellt der Autor vor, wie man PHP in der Befehlszeile verwendet.

1. Überprüfen Sie die Version und Konfiguration von PHP

Geben Sie php -v in die Befehlszeile ein, um die aktuelle PHP-Version anzuzeigen.

Java-Code

  1. PHP 5.2.17(cli) (erstellt: Februar 2 2011 11:19:21)
  2. Copyright (c) 1997-2010 The PHP Group
  3. Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies
  4. mit Zend Optimizer v3.3.9, Copyright (c) 1998-2009, von Zend Technologies
  5. mit eAccelerator v0.9.6.1, Copyright (c) 2004-2010 eAccelerator, von eAccelerator

Andere Optionen sind: –m, -i. Der Autor wird hier keine Beispiele nennen.

-m zeigt die aktuell von PHP geladenen gültigen Module an.

-i gibt PHPInfo ohne HTML-Format aus.

Verwenden Sie die Option –ini, um die Nummer und Pfadinformationen der aktuell von PHP geladenen INI-Konfigurationsdateien auszugeben.

2. Führen Sie das PHP-Programm in der Befehlszeile aus

PHP über die Befehlszeile auszuführen ist sehr einfach. Es gibt jedoch einige Vorsichtsmaßnahmen, die Sie verstehen müssen. Servervariablen wie $_SESSION können nicht in der Befehlszeile verwendet werden und anderer Code wird genauso ausgeführt wie im Webserver^_^.

PHP-Code

  1. echo „PHP-Befehlszeile ausführenecho“
  2. ?>
Speichern Sie den obigen Code als hello.php. Geben Sie php –f hello.php in die Befehlszeile ein. Die Ergebnisse werden wie folgt angezeigt:


Einer der Vorteile der Ausführung von PHP-Dateien über die Befehlszeile besteht darin, dass Sie einige geplante Aufgaben über Skripte ausführen können. Sie müssen nicht über den Webserver gehen^_^.

Natürlich können wir den Code auch direkt in PHP debuggen: Geben Sie den Befehl php -r ein und ein „>“-Symbol erscheint. Dies bedeutet, dass Sie die PHP-Shell betreten haben und direkt Code schreiben und ausführen können.

Java-Code

    -bash-
  1. 3.2$ php -r '
  2. >
  3. for($i=0;$i<2;$i ){
  4. > echo
  5. "Nummer: {$i}n"; > }
  6. > '
  7. Nummer:
  8. 0
  9. Nummer: 1
  10. Sie können auch den Befehl php –a verwenden, um den interaktiven Modus zu öffnen, eine Codezeile eingeben und PHP gibt die Ergebnisse in Echtzeit aus.
3. PHP-Syntax erkennen und Ausgabe hervorheben

Ohne Code auszuführen, können wir Syntaxfehler in PHP-Dateien über die Befehlszeile erkennen.

Java-Code

-bash-3.2$ php -l hello.php
  1. Keine Syntaxfehler in hello.php erkannt
  2. Programmierer müssen häufig den PHP-Code hervorheben und ihn so ausgeben, wie er ist. Verwenden Sie dazu einfach php –s

Java-Code

  1. -bash-3.2$ php -s hello.php  
  2. "color: #000000">  
  3. "color: #0000BB"><?php
      
  4. "color: #007700">echo   
  5. "color: #DD0000">'ddd'  
  6. "color: #007700">;
  7. style="color: #0000BB">?>
      
  8.   

在浏览器中的显示效果

4、查看php手册

从php5.1.2开始,程序员们可以在php命令行下查看手册了,输入php –rf function。会打印出该函数的语法简介

Java代码  

  1. -bash-3.2$ php --rf strip_tags  
  2. Function [  function strip_tags ] {  
  3.    
  4. - Parameters [2] {  
  5. Parameter #0 [  $str ]  
  6. Parameter #1 [  $allowable_tags ]  
  7. }  
  8. }  

如果要查看类使用 –rc;查看扩展使用 –re。

Php命令行的使用方法介绍到这里就结束了。Enjoy It!

来源:http://www.cnphp.info/php-command-line-useage-intro.html

更多:http://www.tutorialspoint.com/unix_commands/php.htm

php - Unix, Linux Command

NAME

php - PHP Command Line Interface ’CLI’

SYNOPSIS

php [options] [ -f ] file [[--] args...]

php [options] -r code [[--] args...]

php [options] [-B code] -R code [-E code] [[--] args...]

php [options] [-B code] -F file [-E code] [[--] args...]

php [options] -- [ args...]

php [options] -a

DESCRIPTION

PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. This is the command line interface that enables you to do the following:

You can parse and execute files by using parameter -f followed by the name of the file to be executed.

Using parameter -r you can directly execute PHP code simply as you would do inside a .php file when using theeval() function.

It is also possible to process the standard input line by line using either the parameter -R or -F. In this mode each separate input line causes the code specified by -R or the file specified by -F to be executed. You can access the input line by $argn. While processing the input lines $argi contains the number of the actual line being processed. Further more the parameters -B and -E can be used to execute code (see -r) before and after all input lines have been processed respectively. Notice that the input is read from STDIN and therefore reading from STDIN explicitly changes the next input line or skips input lines.

If none of -r -f -B -R -F or -E is present but a single parameter is given then this parameter is taken as the filename to parse and execute (same as with -f). If no parameter is present then the standard input is read and executed.

OPTIONS

Tag Description

--interactive 
-a
Run PHP interactively. This lets you enter snippets of PHP code that directly get executed. When readline support is enabled you can edit the lines and also have history support.
--bindpathaddress:port|port
-baddress:port|port
  Bind Path for external FASTCGI Server mode (CGI only).
--no-chdir 
-C
Do not chdir to the script’s directory (CGI only).
--no-header 
-q
Quiet-mode. Suppress HTTP header output (CGI only).
--timing count 
-T count
Measure execution time of script repeated count times (CGI only).
--php-inipath|file 
-c path|file
Look for php.ini file in the directory path or use the specified file
--no-php-ini 
-n
No php.ini file will be used
--definefoo[=bar
-d foo[=bar]
  Define INI entry foo with value bar
-e Generate extended information for debugger/profiler
--file file 
-f file
Parse and execute file
--global name 
-g name
Make variable name global in script.
--help 
-h
This help
--hide-args 
-H
Hide script name (file) and parameters (args...) from external tools. For example you may want to use this when a php script is started as a daemon and the command line contains sensitive data such as passwords.
--info 
-i
PHP information and configuration
--syntax-check 
-l
Syntax check only (lint)
--modules 
-m
Show compiled in modules
--run code 
-r code
Run PHP code without using script tags ’
--process-begin code 
-B code
  Run PHP code before processing input lines
--process-codecode 
-R code
  Run PHP code for every input line
--process-filefile 
-F file
  Parse and execute file for every input line
--process-endcode 
-E code
Run PHP code after processing all input lines
--syntax-highlight 
-s
Output HTML syntax highlighted source
--version 
-v
Version number
--stripped 
-w
Output source with stripped comments and whitespace
--zend-extension file 
-z file
  Load Zend extension file
args... Arguments passed to script. Use ’--’ args when first argument starts with ’-’ or script is read from stdin
--rfunction name
--rf name Shows information about function name
--rclass name
--rc name Shows information about class name
--rextension name
--re name Shows information about extension name
--rextinfo name
--ri name Shows configuration for extension name
--ini Show configuration file names

DATEIEN

Tag-Beschreibung

php-cli.ini The configuration file for the CLI version of PHP.
php.ini The standard configuration file will only be used when php-cli.ini cannot be found.

BEISPIELE

Tag-Beschreibung

php -r ’echo "Hello Worldn";’
  This command simply writes the text "Hello World" to standard out.
php -r ’print_r(gd_info());’
  This shows the configuration of your gd extension. You can use this to easily check which image formats you can use. If you have any dynamic modules you may want to use the same ini file that php uses when executed from your webserver. There are more extensions which have such a function. For dba use:php -r ’print_r(dba_handlers(1));’
php -R ’echo strip_tags($argn)."n";’
  This PHP command strips off the HTML tags line by line and outputs the result. To see how it works you can first look at the following PHP command 'php -d html_errors=1 -i' which uses PHP to output HTML formatted configuration information. If you then combine those two 'php ...|php ...' you’ll see what happens.
php -E ’echo "Lines: $argin";’
  Using this PHP command you can count the lines being input.
php -R ’@$l =count(file($argn));’ -E ’echo "Lines:$ln";’
  In this example PHP expects each input line being a file. It counts all lines of the files specified by each input line and shows the summarized result. You may combine this with tools like find and change the php scriptlet.
php -R ’echo "$argnn"; fgets(STDIN);’
  Since you have access to STDIN from within -B -R -F and -E you can skip certain input lines with your code. But note that in such cases $argi only counts the lines being processed by php itself. Having read this you will guess what the above program does: skipping every second input line.

TIPPS

Sie können eine Shebang-Zeile verwenden, um PHP automatisch aus Skripten aufzurufen. Nur die CLI-Version von PHP ignoriert eine solche erste Zeile wie unten gezeigt:

#!/bin/php

// Ihr Skript

?>

SIEHE AUCH

http://www.php.net/manual/

FEHLER

Sie können die Liste der bekannten Fehler anzeigen oder jeden neuen Fehler melden, den Sie gefunden haben unter:

http://bugs.php.net

AUTOREN

Die PHP-Gruppe: Thies C. Arntzen, Stig Bakken, Andi Gutmans, Rasmus Lerdorf, Sam Ruby, Sascha Schumann, Zeev Suraski, Jim Winstead, Andrei Zmievski.

Zusätzliche Arbeiten für das CLI-Sapi wurden von Edin Kadribasic, Marcus Boerger und Johannes Schlueter durchgeführt.

Eine Liste der aktiven Entwickler finden Sie hier:

http://www.php.net/credits.php

Und nicht zuletzt wurde PHP mit Hilfe einer großen Anzahl von Mitwirkenden auf der ganzen Welt entwickelt.

VERSIONSINFORMATIONEN

Diese Manpage beschreibt PHP, Version 5.2.17.

URHEBERRECHT

Copyright © 1997-2010 The PHP Group

以上就介绍了php命令行用法简介,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助.

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat -Befehle und wie man sie benutzt
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

PHP 8.4 Installations- und Upgrade-Anleitung für Ubuntu und Debian PHP 8.4 Installations- und Upgrade-Anleitung für Ubuntu und Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 bringt mehrere neue Funktionen, Sicherheitsverbesserungen und Leistungsverbesserungen mit einer beträchtlichen Menge an veralteten und entfernten Funktionen. In dieser Anleitung wird erklärt, wie Sie PHP 8.4 installieren oder auf PHP 8.4 auf Ubuntu, Debian oder deren Derivaten aktualisieren. Obwohl es möglich ist, PHP aus dem Quellcode zu kompilieren, ist die Installation aus einem APT-Repository wie unten erläutert oft schneller und sicherer, da diese Repositorys in Zukunft die neuesten Fehlerbehebungen und Sicherheitsupdates bereitstellen.

So richten Sie Visual Studio-Code (VS-Code) für die PHP-Entwicklung ein So richten Sie Visual Studio-Code (VS-Code) für die PHP-Entwicklung ein Dec 20, 2024 am 11:31 AM

Visual Studio Code, auch bekannt als VS Code, ist ein kostenloser Quellcode-Editor – oder eine integrierte Entwicklungsumgebung (IDE) –, die für alle gängigen Betriebssysteme verfügbar ist. Mit einer großen Sammlung von Erweiterungen für viele Programmiersprachen kann VS Code c

Wie analysiert und verarbeitet man HTML/XML in PHP? Wie analysiert und verarbeitet man HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

Dieses Tutorial zeigt, wie XML -Dokumente mit PHP effizient verarbeitet werden. XML (Extensible Markup-Sprache) ist eine vielseitige textbasierte Markup-Sprache, die sowohl für die Lesbarkeit des Menschen als auch für die Analyse von Maschinen entwickelt wurde. Es wird üblicherweise für die Datenspeicherung ein verwendet und wird häufig verwendet

7 PHP-Funktionen, die ich leider vorher nicht kannte 7 PHP-Funktionen, die ich leider vorher nicht kannte Nov 13, 2024 am 09:42 AM

Wenn Sie ein erfahrener PHP-Entwickler sind, haben Sie möglicherweise das Gefühl, dass Sie dort waren und dies bereits getan haben. Sie haben eine beträchtliche Anzahl von Anwendungen entwickelt, Millionen von Codezeilen debuggt und eine Reihe von Skripten optimiert, um op zu erreichen

Erklären Sie JSON Web Tokens (JWT) und ihren Anwendungsfall in PHP -APIs. Erklären Sie JSON Web Tokens (JWT) und ihren Anwendungsfall in PHP -APIs. Apr 05, 2025 am 12:04 AM

JWT ist ein offener Standard, der auf JSON basiert und zur sicheren Übertragung von Informationen zwischen Parteien verwendet wird, hauptsächlich für die Identitätsauthentifizierung und den Informationsaustausch. 1. JWT besteht aus drei Teilen: Header, Nutzlast und Signatur. 2. Das Arbeitsprinzip von JWT enthält drei Schritte: Generierung von JWT, Überprüfung von JWT und Parsingnayload. 3. Bei Verwendung von JWT zur Authentifizierung in PHP kann JWT generiert und überprüft werden, und die Funktionen und Berechtigungsinformationen der Benutzer können in die erweiterte Verwendung aufgenommen werden. 4. Häufige Fehler sind Signaturüberprüfungsfehler, Token -Ablauf und übergroße Nutzlast. Zu Debugging -Fähigkeiten gehört die Verwendung von Debugging -Tools und Protokollierung. 5. Leistungsoptimierung und Best Practices umfassen die Verwendung geeigneter Signaturalgorithmen, das Einstellen von Gültigkeitsperioden angemessen.

PHP -Programm zum Zählen von Vokalen in einer Zeichenfolge PHP -Programm zum Zählen von Vokalen in einer Zeichenfolge Feb 07, 2025 pm 12:12 PM

Eine Zeichenfolge ist eine Folge von Zeichen, einschließlich Buchstaben, Zahlen und Symbolen. In diesem Tutorial wird lernen, wie Sie die Anzahl der Vokale in einer bestimmten Zeichenfolge in PHP unter Verwendung verschiedener Methoden berechnen. Die Vokale auf Englisch sind a, e, i, o, u und sie können Großbuchstaben oder Kleinbuchstaben sein. Was ist ein Vokal? Vokale sind alphabetische Zeichen, die eine spezifische Aussprache darstellen. Es gibt fünf Vokale in Englisch, einschließlich Großbuchstaben und Kleinbuchstaben: a, e, ich, o, u Beispiel 1 Eingabe: String = "TutorialPoint" Ausgabe: 6 erklären Die Vokale in der String "TutorialPoint" sind u, o, i, a, o, ich. Insgesamt gibt es 6 Yuan

Erklären Sie die späte statische Bindung in PHP (statisch: :). Erklären Sie die späte statische Bindung in PHP (statisch: :). Apr 03, 2025 am 12:04 AM

Statische Bindung (statisch: :) implementiert die späte statische Bindung (LSB) in PHP, sodass das Aufrufen von Klassen in statischen Kontexten anstatt Klassen zu definieren. 1) Der Analyseprozess wird zur Laufzeit durchgeführt.

Was sind PHP Magic -Methoden (__construct, __Destruct, __call, __get, __set usw.) und geben Sie Anwendungsfälle an? Was sind PHP Magic -Methoden (__construct, __Destruct, __call, __get, __set usw.) und geben Sie Anwendungsfälle an? Apr 03, 2025 am 12:03 AM

Was sind die magischen Methoden von PHP? Zu den magischen Methoden von PHP gehören: 1. \ _ \ _ Konstrukt, verwendet, um Objekte zu initialisieren; 2. \ _ \ _ Destruct, verwendet zur Reinigung von Ressourcen; 3. \ _ \ _ Call, behandeln Sie nicht existierende Methodenaufrufe; 4. \ _ \ _ GET, Implementieren Sie den dynamischen Attributzugriff; 5. \ _ \ _ Setzen Sie dynamische Attributeinstellungen. Diese Methoden werden in bestimmten Situationen automatisch aufgerufen, wodurch die Code -Flexibilität und -Effizienz verbessert werden.

See all articles