Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
How to Execute PHP Code using Command Line?
Next article icon

How to Execute PHP Code using Command Line?

Last Updated : 27 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

PHP is mainly used for creating dynamic websites, but you can also run PHP scripts without a web server. This can be done using the PHP Command Line Interface (PHP CLI), which allows you to execute PHP scripts directly from the command line. This method is commonly used for testing, automation, or running background tasks by using the php command followed by the script name or inline code.

To execute PHP code using the command line, you can use several methods. Here's a guide with the most common ones:

Step 1: Installation of PHP

First, you need to install PHP on your system. Once PHP is installed, you can use the command line to execute PHP scripts using the PHP Command Line Interface (CLI). This allows you to run PHP code directly without needing a web server.

  • On Windows: Follow the step-by-step guide to install PHP from GeeksforGeeks.
  • On Linux: Check out the detailed installation steps for PHP on Linux through the GeeksforGeeks guide.
  • On Mac: Learn how to install PHP on macOS from the official documentation.

Step 2: Executing PHP code using the command line

After the successful installation of PHP in your system, you can now start the PHP code execution using the command line. It means that you are running PHP scripts or code directly in the terminal or command prompt without a web server.

To execute PHP code using the command line, follow these simple steps:

1. Execute a PHP Script File

  • Create a PHP File (e.g., script.php):
PHP
<?php   echo "Hello, World!"; ?> 
  • Open your terminal or command prompt.
  • Navigate to the directory containing the file.
  • Run the script using the php command:
PHP
<?php    php script.php ?> 

2. Execute PHP Code Inline Using -r

  • You can execute a small PHP code snippet directly from the command line without needing a script file.
PHP
<?php     php -r 'echo "Hello from the command line!";' ?> 

3. Passing Arguments to PHP Script

  • You can pass arguments to your script using the $argv array.
  • Example PHP file (args.php):
PHP
<?php   echo "Argument 1: " . $argv[1] . "\n";   echo "Argument 2: " . $argv[2] . "\n"; ?> 
  • Run the script with arguments:
PHP
<?php     php args.php value1 value2 ?> 

4. Interactive Mode (php -a)

  • To run PHP code interactively, use the -a option.
PHP
<?php     php -a ?> 

5. Running PHP Scripts in the Background

  • If you need to run a PHP script in the background (e.g., for long-running processes), append & to the command:
PHP
<?php     php script.php & ?> 
  • This will run the script in the background and free up the terminal.

6. Running PHP with Built-in Web Server (php -S)

  • PHP includes a built-in web server that you can use for testing purposes. This allows you to run a local server without a full web server setup.
PHP
<?php     php -S localhost:8000 ?> 
  • This starts a PHP server on localhost:8000. You can access it in your browser at http://localhost:8000.

Note:

  • Always use the complete path to your files to avoid mistakes, especially when running scripts from different folders.
  • Enable error reporting while testing to easily find any problems. Add ini_set('display_errors', 1) in your script.
  • For scripts that run for a long time, save the output to a log file using error_log() or file_put_contents() so you can check it later.

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


Next Article
How to Execute PHP Code using Command Line?

S

shubhamr238
Improve
Article Tags :
  • Technical Scripter
  • Web Technologies
  • PHP
  • PHP Programs

Similar Reads

    How to delete a file using PHP ?
    To delete a file by using PHP is very easy. Deleting a file means completely erase a file from a directory so that the file is no longer exist. PHP has an unlink() function that allows to delete a file. The PHP unlink() function takes two parameters $filename and $context. Syntax: unlink( $filename,
    2 min read
    How to Pop an Alert Message Box using PHP?
    The alert message just like a pop-up window on the screen. Using this you can alert to the user with some information and message. PHP doesn't support alert message box because it is a server-side language but you can use JavaScript code within the PHP body to alert the message box on the screen.Syn
    1 min read
    How to Zip a directory in PHP?
    ZIP is an archive file format that supports lossless data compression. A ZIP file may contain one or more files or directories that may have been compressed. The PHP ZipArchive class can be used to zipping and unzipping. It might be need to install the class if it is not present. Installation for Li
    2 min read
    How to set PHP development environment in windows ?
    What is PHP ? PHP (Hypertext PreProcessor) is a general-purpose programming language developed by Rasmus Lerdorf in 1994 for web development. This is one of the most popular programming language for beginners in web development because of its simplicity, large community and accessibility.Steps to se
    3 min read
    How to open a PDF files in web browser using PHP?
    Opening a PDF file in a web browser using PHP involves serving the PDF directly to the client’s browser. This is achieved by setting the appropriate HTTP headers (Content-Type and Content-Disposition) in PHP, which instructs the browser to display the PDF instead of downloading it.Note: PHP doesn't
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences