0

0

怎么用php命令执行php代码_PHP命令行执行PHP脚本方法教程

星夢妙者

星夢妙者

发布时间:2025-11-10 14:16:04

|

726人浏览过

|

来源于php中文网

原创

Use php -r to execute inline PHP code without files, e.g., php -r "echo 'Hello, World!';". 2. Run a PHP file via php script.php. 3. Pass arguments accessible through $argv. 4. Make scripts executable with #!/usr/bin/env php and chmod +x. 5. Validate syntax with php -l.

怎么用php命令执行php代码_php命令行执行php脚本方法教程

If you want to execute PHP code or scripts from the command line, using the PHP CLI is a straightforward approach. Here are several methods to achieve this:

The operating environment of this tutorial: MacBook Pro, macOS Sonoma

1. Execute Inline PHP Code with -r Option

The PHP command-line interface provides the -r option to run PHP code directly without creating a file. This is useful for quick tests or small operations.

  • Open your terminal.
  • Type php -r "echo 'Hello, World!';" and press Enter.
  • You should see the output immediately in the console.

Note: Do not include <?php tags when using the -r flag.

立即学习PHP免费学习笔记(深入)”;

2. Run a PHP Script File

You can execute a standalone PHP file by passing its path to the php command. This is the most common way to run PHP applications via CLI.

Yodayo
Yodayo

一个专为动漫迷和vTuber打造的AI艺术创作平台、交流社区

下载
  • Create a file named test.php with content: <?php echo "Script executed successfully"; ?>
  • In the terminal, navigate to the directory containing the file using cd.
  • Run php test.php.
  • The script output will be printed directly to the terminal.

3. Pass Arguments to PHP Scripts

PHP CLI allows passing arguments to scripts, which can be accessed via the $argv and $argc variables inside the script.

  • Modify your test.php file to include: <?php var_dump($argv); ?>
  • Execute it with arguments: php test.php arg1 arg2.
  • The output will show an array of passed arguments including the script name.

4. Use Shebang for Direct Execution

On Unix-like systems, you can make a PHP script executable by adding a shebang line at the top and setting file permissions.

  • Add #!/usr/bin/env php as the first line in your test.php file.
  • Make it executable: chmod +x test.php.
  • Run it directly: ./test.php.

This method treats the PHP script like a native shell script.

5. Check PHP Syntax Without Execution

Before running a script, you can validate its syntax using the -l (lint) option to prevent runtime errors.

  • Run php -l test.php to check for parsing errors.
  • If the syntax is correct, you'll receive a message confirming no syntax errors were detected.

This is especially helpful when debugging large scripts or deploying code.

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
if什么意思
if什么意思

if的意思是“如果”的条件。它是一个用于引导条件语句的关键词,用于根据特定条件的真假情况来执行不同的代码块。本专题提供if什么意思的相关文章,供大家免费阅读。

847

2023.08.22

go中interface用法
go中interface用法

本专题整合了go语言中int相关内容,阅读专题下面的文章了解更多详细内容。

78

2025.09.10

console接口是干嘛的
console接口是干嘛的

console接口是一种用于在计算机命令行或浏览器开发工具中输出信息的工具,提供了一种简单的方式来记录和查看应用程序的输出结果和调试信息。本专题为大家提供console接口相关的各种文章、以及下载和课程。

420

2023.08.08

console.log是什么
console.log是什么

console.log 是 javascript 函数,用于在浏览器控制台中输出信息,便于调试和故障排除。想了解更多console.log的相关内容,可以阅读本专题下面的文章。

541

2024.05.29

macOS怎么切换用户账户
macOS怎么切换用户账户

在 macOS 系统中,可通过多种方式切换用户账户。如点击苹果图标选择 “系统偏好设置”,打开 “用户与群组” 进行切换;或启用快速用户切换功能,通过菜单栏或控制中心的账户名称切换;还能使用快捷键 “Control+Command+Q” 锁定屏幕后切换。

359

2025.05.09

unix和linux的区别
unix和linux的区别

unix和linux的区别包括发展历史、开源性、发行版本、内核、文件系统、应用程序兼容性和用户界面等。本专题为大家提供unix和linux相关的文章、下载、课程内容,供大家免费下载体验。

392

2023.09.22

C# ASP.NET Core微服务架构与API网关实践
C# ASP.NET Core微服务架构与API网关实践

本专题围绕 C# 在现代后端架构中的微服务实践展开,系统讲解基于 ASP.NET Core 构建可扩展服务体系的核心方法。内容涵盖服务拆分策略、RESTful API 设计、服务间通信、API 网关统一入口管理以及服务治理机制。通过真实项目案例,帮助开发者掌握构建高可用微服务系统的关键技术,提高系统的可扩展性与维护效率。

76

2026.03.11

Go高并发任务调度与Goroutine池化实践
Go高并发任务调度与Goroutine池化实践

本专题围绕 Go 语言在高并发任务处理场景中的实践展开,系统讲解 Goroutine 调度模型、Channel 通信机制以及并发控制策略。内容包括任务队列设计、Goroutine 池化管理、资源限制控制以及并发任务的性能优化方法。通过实际案例演示,帮助开发者构建稳定高效的 Go 并发任务处理系统,提高系统在高负载环境下的处理能力与稳定性。

38

2026.03.10

Kotlin Android模块化架构与组件化开发实践
Kotlin Android模块化架构与组件化开发实践

本专题围绕 Kotlin 在 Android 应用开发中的架构实践展开,重点讲解模块化设计与组件化开发的实现思路。内容包括项目模块拆分策略、公共组件封装、依赖管理优化、路由通信机制以及大型项目的工程化管理方法。通过真实项目案例分析,帮助开发者构建结构清晰、易扩展且维护成本低的 Android 应用架构体系,提升团队协作效率与项目迭代速度。

83

2026.03.09

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP课程
PHP课程

共137课时 | 13.4万人学习

JavaScript ES5基础线上课程教学
JavaScript ES5基础线上课程教学

共6课时 | 11.3万人学习

PHP新手语法线上课程教学
PHP新手语法线上课程教学

共13课时 | 1.0万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号