简体   繁体   English

Bash中的PHP命令不执行代码

[英]PHP command in Bash does not execute code

Ok, so I have a problem with this thing I found on a MacBook Air. 好的,所以我在MacBook Air上发现的这个东西有问题。 It's called terminal and you can do crazy stuff on it. 它被称为终端,您可以在上面做疯狂的事情。 So anyways, when I enter the command "php" it gives me a multiline console but it doesn' do anything when I run a line of php! 所以无论如何,当我输入命令“ php”时,它会给我一个多行控制台,但是当我运行一行php时,它什么也不会做! For instance, I type echo "Hello World" but it just returns it like a typewriter and nothing happens! 例如,我键入echo“ Hello World”,但它像打字机一样返回,什么也没有发生! Can someone please tell me what is going on, and is there a way to exit this? 有人可以告诉我发生了什么,有办法退出吗?

I believe if you just type php into the terminal it will start a php server process on the local mac. 我相信,如果您只是在终端中输入php,它将在本地mac上启动php服务器进程。 If you want to run a script you have to cd in to to the working directory and type php filename.php . 如果要运行脚本,则必须cd进入工作目录并键入php filename.php

如果要直接使用PHP代码,则必须输入不a标志的php -a ,您只需要在php环境中输入即可。

When running the php command directly from your command line, it behaves as it is reading a .php from the command line 当直接从命令行运行php命令时,它的行为与从命令行读取.php时的行为相同

You noticed that it just echo's back any thing you write toward it, this is the same as it is executing a .php file, its echoíng any html back to the browser 您注意到,它只是回显您对其写的任何内容,这与执行.php文件相同,它会将任何html回显到浏览器

Example: 例:

$ php
sss
<?PHP
echo 'Hello!';
?>
More data!

ctrl + d (to signal that the end of the filehas reached) ctrl + d (表示已到达文件末尾)

Output: 输出:

sss
Hello!More data!

Firstly check if you have php properly installed: 首先检查您是否已正确安装php:

type in console: 在控制台中输入:

php -v

you should see version of installed php for ex.: 您应该看到安装的PHP的版本,例如:

PHP 7.3.3 ... PHP 7.3.3 ...

to run single line of code from console you do it this way: 要从控制台运行单行代码,您可以这样做:

php -r 'echo "\nHello World\n";'

where \\n is new line character. 其中\\ n是换行符。

to enter interactive mode and run multiple lines of code: 进入交互模式并运行多行代码:

php -a

and once you see: 一旦看到:

Interactive mode enabled 启用互动模式

php > php>

type: 类型:

echo "\nHello World\n";

and hit [Enter] key. 然后按[Enter]键。

that's it. 而已。

to leave interactive mode type: 离开互动模式类型:

exit

note lack of ; 注意缺乏; at the end. 在末尾。

If you want to run from console a php code that you have in a file: 如果要从控制台运行文件中包含的php代码,请执行以下操作:

php -f <path-to-the-file>

but this is a default behavior of php so if you miss flag -f and just type: 但这是php的默认行为,因此,如果您错过标志-f,只需键入:

php

it will do nothing, expecting you providing it a path to file after the php like in the example with flag -f : 它什么也不会做,希望您像在带有标志-f的示例中那样,在php之后为它提供文件的路径:

php <path-to-file>

So if a programmer intention is to enter an interactive mode but he types only php without any flags, the php will not warn about missing path to the file so programmer may have the impression he's into php interactive mode as he wanted but this is not true. 所以,如果一个程序员的意图是进入一个互动模式,但他的类型只有php没有任何标志,PHP 不会警告失踪的文件路径,以便程序员可能有这样的印象,他是到PHP交互模式,因为他想,但事实并非如此。

to see all possible options in php cli, type: 要查看php cli中的所有可能选项,请输入:

php --help

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM