How to execute PHP code from the command line?

前端 未结 5 922
逝去的感伤
逝去的感伤 2020-12-04 06:41

I would like to execute a single php statement like if(function_exists(\"my_func\")) echo \'function exists\'; directly with the command line without having to

相关标签:
5条回答
  • 2020-12-04 07:05

    If you're going to do PHP in the command line, i recommend you install phpsh, a decent PHP shell. It's a lot more fun.

    Anyway, the php command offers two switches to execute code from the command line:

    -r <code>        Run PHP <code> without using script tags <?..?>
    -R <code>        Run PHP <code> for every input line
    

    You can use php's -r switch as such:

    php -r 'echo function_exists("foo") ? "yes" : "no";'
    

    The above PHP command above should output no and returns 0 as you can see:

    >>> php -r 'echo function_exists("foo") ? "yes" : "no";'
    no
    >>> echo $? # print the return value of the previous command
    0
    

    Another funny switch is php -a:

    -a               Run as interactive shell
    

    It's sort of lame compared to phpsh, but if you don't want to install the awesome interactive shell for php made by facebook to get tab completion, history, and so on, then use -a as such:

    >>> php -a
    Interactive shell
    
    php > echo function_exists("foo") ? "yes" : "no";
    no
    php > 
    

    If it doesn't work on your box like on my box*es* (tested on Ubuntu and Arch), then probably your PHP setup is fuzzy or broken. If you run this command:

    php -i | grep 'API'
    

    You should see:

    Server API => Command Line Interface
    

    If you don't, this means that maybe another command will provides the CLI SAPI. Try php-cli, maybe it's a package or a command available in your OS.

    If you do see that your php command uses the CLI (Command Line Interface) SAPI (Server API), then run php -h | grep code to find out which crazy switch - as this hasn't changed for year- allows to run code in your version/setup.

    Another couple of examples, just to make sure it works on my boxes:

    >>> php -r 'echo function_exists("sg_load") ? "yes" : "no";' 
    no
    >>> php -r 'echo function_exists("print_r") ? "yes" : "no";' 
    yes
    

    Also, note that it is possible that an extension is loaded in the CLI and not in the CGI or Apache SAPI. It is likely that several PHP SAPIs use different php.ini files, e.g. /etc/php/cli/php.ini vs /etc/php/cgi/php.ini vs /etc/php/apache/php.ini on a Gentoo box. Find out which ini file is used with php -i | grep ini.

    0 讨论(0)
  • 2020-12-04 07:06

    You can use :

     echo '<?php if(function_exists("my_func")) echo "function exists"; ' | php
    

    The short tag "< ?=" can be helpfull too :

     echo '<?= function_exists("foo") ? "yes" : "no";' | php
     echo '<?= 8+7+9 ;' | php
    

    Tha closing tag "?>" is optional, but don't forget the final ";" !

    0 讨论(0)
  • 2020-12-04 07:17

    Using PHP from the command line

    use " instead of ' on windows when using the cli version with -r

    php -r "echo 1"
    

    -- correct

    php -r 'echo 1'
    

    -- incorrect

      PHP Parse error:  syntax error, unexpected ''echo' (T_ENCAPSED_AND_WHITESPACE), expecting end of file in Command line code on line 1
    
    0 讨论(0)
  • 2020-12-04 07:25

    On the command line:

    php -i | grep sourceguardian
    

    If it's there, then you'll get some text. If not, you won't get a thing.

    0 讨论(0)
  • 2020-12-04 07:28

    If you're using Laravel, you can use php artisan tinker to get an amazing interactive shell to interact with your Laravel app. However, Tinker works with "Psysh" under the hood which is a popular PHP REPL and you can use it even if you're not using Laravel(bare PHP):

    // Bare PHP:
    >>> preg_match("/hell/", "hello");
    => 1
    
    // Laravel Stuff:
    >>> Str::slug("How to get the job done?!!?!", "_");
    => "how_to_get_the_job_done"
    

    One great feature I really like about Psysh is that it provides a quick way for directly looking up the PHP docs from the command line. To get it to work, you only have to take the following simple steps:

    apt install php-sqlite3
    

    and then get the required PHP docs database and move it to the proper location:

    wget http://psysh.org/manual/en/php_manual.sqlite
    mkdir -p /usr/local/share/psysh/ && mv php_manual.sqlite /usr/local/share/psysh/
    

    now for instance:

    0 讨论(0)
提交回复
热议问题