How to execute PHP code from the command line?

前端 未结 5 941
逝去的感伤
逝去的感伤 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: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:

提交回复
热议问题