如何在Windows机器上的命令提示符下运行PHP程序? [英] How to run a PHP program from command prompt on a Windows Machine?

查看:92
本文介绍了如何在Windows机器上的命令提示符下运行PHP程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是专业的 PHP开发人员.

我正在使用在 Windows 10家庭单一语言64位操作系统上运行的 Lenovo Ideapad笔记本电脑

我还在位置"C:\ xampp" 的位置安装了 XAMPP控制面板v3.2.2 ,以在<我的机器上的strong>网络浏览器.

"php.exe" 文件位于位置"C:\ xampp \ php" .

用于保存PHP文件的文档根目录位于位置"C:\ xampp \ htdocs" .

通过启动 XAMPP 软件,我可以运行创建并保存在目录 C:\ xampp \ htdocs \ html_playground 中的PHP程序.单击桌面上存在的XAMPP快捷方式),然后输入程序文件的网址,例如" http://localhost/html_playground/sample.php "在浏览器的地址栏中.

这样,我可以很好地运行PHP程序,但是我想从 Windows命令提示符

运行相同的程序

为此,我完成了以下步骤:

  • 进入高级系统设置(控制面板\系统和安全性\系统\高级系统设置)
  • 然后单击环境变量
  • 然后选择变量路径
  • 然后单击编辑... 按钮
  • 然后,在现有字符串的结尾分号之后,我在分号后添加了空格,从而添加了字符串"C:\ xampp \ php" .

最后的新字符串如下所示:

%USERPROFILE%\ AppData \ Local \ Microsoft \ WindowsApps; C:\ xampp \ php

  • 然后点击确定
  • 打开了命令提示符
  • 使用 cd 命令
  • 在命令提示符下转到路径 C:\ xampp \ htdocs \ html_playground
  • 键入 sample.php (包含我的PHP程序的文件)

然后,而不是显示程序的输出,而是在 Sublime Text (我用来编写代码的编辑器)中打开了相同的文件

sample.php 文件具有以下 PHP可执行代码:

<?php 
   echo 'Hello World!';
?>

我也附上命令提示符窗口的屏幕截图:

现在我的问题是

  • 为什么在命令提示符或Web浏览器中看不到输出?
  • 我在做任何错误还是做什么?
  • 是否有必要像通常那样启动XAMPP服务器以在Web浏览器中运行该程序以从命令提示符处执行该程序吗?
  • 设置环境变量时是否犯了任何错误?
  • 真的需要设置环境变量吗?如果是,为什么?如果没有,为什么?
  • 由于PHP应该是适合于Web开发的语言,因此我认为最好在Web开发人员的软件(即Web浏览器)中运行该程序.但是我不理解人们为什么坚持从命令行运行PHP程序而不是仅从Web浏览器运行同一程序的原因?

请有人回答我的问题并在命令提示符下帮助我运行程序,以帮助我.

即使我尝试重新启动PC并运行 php -v 命令,但也没有成功.以下是相同的屏幕截图:

解决方案

如果要运行CLI PHP,我强烈建议安装在 Composer (您将有很大的机会),它将在本地找到它.

现在您可以打开命令提示符并运行如下脚本

C:\php\php.exe C:\path\to\php\script.php

根据您的具体情况

  1. Why I'm not able to see the output at command prompt or a web browser?因为输出被发送到命令行.浏览器请求通过Web服务器发送到浏览器
  2. Is it necessary to start the XAMPP server like I normally do to run the program from command prompt too?对于CLI请求,否. PHP是可执行文件,可以单独运行.如果要发送浏览器请求,则需要运行XAMPP.
  3. Is there really a need to set environment variables? If yes why? If no why?并非如此. CLI仅会注意php.ini XAMPP配置主要用于Apache.
  4. But I'm not understanding the reason why people do insist for running the PHP programs from Command Line主要原因是自动化任务.我在执行cron工作时有几个工作流程.

I'm a PHP Developer by profession.

I'm using a Lenovo Ideapad laptop that runs on Windows 10 Home Single Language 64-bit Operating System

I've also installed XAMPP Control Panel v3.2.2 at location "C:\xampp" to execute PHP programs in a web browser on my machine.

The "php.exe" file is present at the location "C:\xampp\php".

The document root directory to save the PHP files is at the location "C:\xampp\htdocs".

I'm able to run the PHP programs that I created and saved in a directory C:\xampp\htdocs\html_playground by starting the XAMPP software(by double clicking on XAMPP shortcut present on my desktop) and entering the URL of a program file like this "http://localhost/html_playground/sample.php" in a browser's address bar.

This way I'm able to run the PHP programs finely but I want to run the same program from Windows Command Prompt

For it I done following steps :

  • Went to Went to Advanced System Settings (Control Panel\System and Security\System\Advanced System Settings)
  • Then clicked on Environment Variables
  • Then selected the variable Path
  • Then clicked Edit... button
  • Then after the ending semicolon of existing string I added the string "C:\xampp\php" by adding a blank space after the semicolon.

The final new string was looking like below :

%USERPROFILE%\AppData\Local\Microsoft\WindowsApps; C:\xampp\php

  • Then clicked on Ok
  • Opened command prompt
  • Went to the path C:\xampp\htdocs\html_playground on command prompt using cd command
  • Typed in sample.php(The file contaning my PHP program)

Then instead of showing the output of the program it opened the same file in Sublime Text(The editor I'm using to write the code)

The sample.php file has got the following PHP executable code :

<?php 
   echo 'Hello World!';
?>

For your reference I'm also attaching the screen-shot of the command prompt window :

Now my question is

  • Why I'm not able to see the output at command prompt or in a web browser?
  • Am I doing any mistake or what?
  • Is it necessary to start the XAMPP server like I normally do to run the program in a web browser for executing the program from command prompt too?
  • Did I make any mistake in setting environment variables?
  • Is there really a need to set environment variables? If yes why? If no why?
  • As PHP is supposed to be the suitable language for web development then I think it's always good to run the program in web developer's software i.e. a Web Browser. But I'm not understanding the reason why people do insist for running the PHP programs from Command Line rather than running the same program from web browser only?

Please somebody help me out by answering my queries and helping me in running the program from command prompt.

Even I tried restarting the PC and run the php -v command but it also didn't work out. Following is the screen-shot of the same :

解决方案

If you're wanting to run CLI PHP I would highly suggest installing the vanilla PHP found on windows.php.net. Let it install to C:\php The reason I recommend this is that if you ever want to use Composer (and there's a really good chance you will want to), it will natively find it there.

Now you can open a command prompt and run scripts like follows

C:\php\php.exe C:\path\to\php\script.php

To your specifics

  1. Why I'm not able to see the output at command prompt or a web browser? Because the output is sent to the command line. Browser requests are sent via the web server to the browser
  2. Is it necessary to start the XAMPP server like I normally do to run the program from command prompt too? For CLI requests, no. PHP is an executable and can run on its own. If you want to do browser requests then XAMPP needs to be running.
  3. Is there really a need to set environment variables? If yes why? If no why? Not really. The CLI will only pay attention to php.ini XAMPP configs are for Apache, mostly.
  4. But I'm not understanding the reason why people do insist for running the PHP programs from Command Line The main reason is automated tasks. I have several processes at work I run on a cron job.

这篇关于如何在Windows机器上的命令提示符下运行PHP程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆