How to use PHP on the command line?

Asked

Viewed 5,439 times

6

Is it possible to use PHP on the command line? How does language use work in this environment? It is possible to develop scripts and run them as shell scripts?

  • 2

    I use a lot, by the way. Even, on my machine, the code editor is configured to call the executable directly, to facilitate development without needing a local page server running all the time. "How the use of language works in this environment?" - within what can be expected from the command line, works normally. What exactly you doubt?

2 answers

8


Hello all right? We can use php with command line.

Example: let’s use php in the command line to execute a script that displays the "Hello terminal" message; In case you create the php file, for example the name 'script.php', puts the message inside. After opening the terminal where your file is and executing any of the following commands (the -f in the second command comes from 'file', that is, the name of the file; but the two do the same thing, it is just another way to execute):

php script.php
php -f script.php

Example of what the code would look like: php script.

<?php

echo "Hello terminal!";

Output at command prompt: inserir a descrição da imagem aqui

Using php with command line we have two important global variables: argc and argv. The argc is the parameter counter passed and while argv is a global array containing the parameters passed (by default the first index always contains the name of the script being executed).

We can pass the parameters and use them to our advantage. Let’s do an example of passing two numbers and printing the multiplication between them, but if less than necessary parameters are passed we will print an error message.

Soon, our script looks like this:

<?php

// O primeiro índice é o nome do arquivo logo temos 3 parâmetros: 0 é o nome do arquivo, 1 é o primeiro número e 2 é o segundo número
if ($argc < 2) {
    echo "Parametros invalidos para executar o script!";
    exit(1);
}
$mult = $argv[1] * $argv[2];

echo "\n O resultado da multiplicacao entre os numeros informados eh: $mult";
echo "\n A quantidade de parametros aceitos e que foram informados sao: $argc";
echo "\n Parametros informados para o script:";
print_r($argv);

Our terminal exit is:

inserir a descrição da imagem aqui

For more information see documentation about Php used in command line

  • 1

    Thank you for the reply.

5

It is possible, has manual on this. So just call the language interpreter by passing the script who intends to execute and possibly arguments for it.

php -f script.php
  • Thank you for the reply.

  • What’s the difference with and without -f ?

  • @Isac do not know :) I am from the time that neither was so, I think some combination can complicate without the -f, but i basic can use without. You know how it is, right? PHP has nothing documented in an organized way, would have to look in depth and try to find out, if you have something specified.

  • 1

    @Isac has situations that you have options and arguments, usually in this case uses php -opcao1 -f arquivo -opcao2 -opcaoN -- argumento1 argumento2 and will only be passed to PHP what comes after --. Already, without the -f up to the -- is treated as argument. And, as is PHP, in both cases it is inconsistent when you pass something before the -- which is not one of PHP’s options :D (at least PHP is consistent as far as not being consistent :P )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.