Shiphp The PHP Developer's Guide

Writing a PHP Command Line Script

To write a PHP command line script, you will need PHP installed on your Mac, Windows, or Linux machine, a terminal command line tool like Bash, and a text editor like Sublime.

  1. To write a command line script in PHP, open up a new blank file your favorite text editor.
  2. Add an opening <?php tag to the very beginning of the new file.
  3. On the next line, save some text to a PHP variable: $text = "Hello World!";
  4. Add a new line below that will output the text: echo $text;
  5. Now, save the file as hello.php. Be sure you know where the file is saved so that you can find it in your command line terminal.
  6. Open your command line terminal and navigate to the PHP file you just created.
  7. Run the file from your terminal by typing php hello.php and then hitting the enter or return key.

If you did everything correctly, you should see Hello World! in the command line of your terminal.


1. hello.php


$text = "Hello World!";

echo $text;

Like this Post?

Learn to build your first Dockerized PHP application.

In this book, PHP developers will learn everything they need to know to start building their applications on Docker, including:

  • Installing dependencies using Composer.
  • Getting data from a third-party API.
  • Saving data to a MySQL database.
  • Using a web framework (SlimPHP) for routing.
  • Storing environmental variables securely.
  • And much more!

You can buy this book on Leanpub today.

Buy it Now!