php-como-ejecutar-php-desde-la-consola-cli

How to Run PHP from the Console (CLI)

  • 3 min

PHP CLI is the interface that allows you to run PHP from the terminal, without relying on a browser or a web request.

So far we have seen PHP as a generator of web pages: the browser requests something and PHP delivers HTML. But PHP is much more than that. It is a general-purpose scripting language, just like Python or Bash.

PHP has an interface called CLI (Command Line Interface).

What is it for?

  1. Scheduled Tasks (Cron Jobs): Send emails at 3 AM, clean databases, generate PDF reports.
  2. Development Tools: Composer (which we will see later), automated tests, and framework commands like Laravel (artisan) run on PHP CLI.
  3. Maintenance Scripts: Move files, process images in bulk, etc.

Verifying Access

To use the CLI, your operating system must know where the php.exe executable is located. If you installed Laragon and open its terminal (or used the “Terminal” button in Laragon), this is already configured.

Open your terminal and type:

php -v
Copied!

If it responds with the version, we are in. If it says “command not recognized”, you need to add the PHP folder to your system’s Environment Variables (PATH) (or simply use the terminal that comes with Laragon, which is the easiest way).

Your First Console Script

Let’s create a new file called cli.php in your project folder.

<?php
echo "Hello, console.";
echo "This is not HTML.";
echo "Here we use real line breaks.";
Copied!

Now, instead of going to the browser, go to your terminal. Make sure you are in the folder where you saved the file (use the cd command to navigate if necessary) and run:

php cli.php
Copied!

The Format Problem (<br> vs \n)

If you run the above code, you will see something ugly: Hello, console.This is not HTML.Here we use real line breaks.

All together. Why? In the browser we use <br> to break lines because the browser interprets HTML. The console does not know what HTML is. The console only understands plain text.

For the console, we need the newline escape character: \n.

Modify your file:

<?php
echo "Hello, console.\n"; // \n means New Line
echo "This looks better.\n";
Copied!

Run php cli.php again and you will see the text perfectly ordered.

Remember that in an HTML response we use elements like <br> or <p>. In the console we use escape characters like \n or \t.

Interactive Mode (php -a)

Sometimes you want to test a quick function or do a math calculation without creating a file. PHP has an interactive mode.

Type in your terminal:

php -a
Copied!

The prompt will change to php >. Now you can write PHP code live:

php > $x = 10;
php > $y = 5;
php > echo $x * $y;
50
php >
Copied!

To exit, type exit or press Ctrl+C.

The Built-in Web Server

Remember when we said you needed Apache or Nginx to see your website? Well, that’s a half-truth.

Since version 5.4, PHP includes a lightweight web server for development. It is incredibly useful if you want to test something quickly without configuring Laragon or Docker.

In your terminal, inside your project folder, type:

php -S localhost:8000
Copied!

Note the capital S

The terminal will be “waiting”. Now open your browser and go to http://localhost:8000. There is your website!

The terminal will show a log of each visit in real-time. To shut down the server, return to the terminal and press Ctrl+C.

This server is for development only. Its default configuration is intended for local testing and does not offer the performance or hardening of a production server.