38

How we run php script using Linux bash?

php file test.php

test.php contains:

<?php echo "hello\n" ?>
1
  • In addition to answers below, if you are using PHP short tags (<? … ?>) make sure they are enabled in php.ini. Commented Sep 7, 2014 at 15:56

9 Answers 9

52

From the command line, enter this:

php -f filename.php

Make sure that filename.php both includes and executes the function you want to test. Anything you echo out will appear in the console, including errors.

Be wary that often the php.ini for Apache PHP is different from CLI PHP (command line interface).

Reference: https://secure.php.net/manual/en/features.commandline.usage.php

Sign up to request clarification or add additional context in comments.

Comments

33

First of all check to see if your PHP installation supports CLI. Type: php -v. You can execute PHP from the command line in 2 ways:

  1. php yourfile.php
  2. php -r 'print("Hello world");'

Comments

22

There are two ways you can do this. One is the one already mentioned, i.e.:

php -f filename.php

The second option is making the script executable (chmod +x filename.php) and adding the following line to the top of your .php file:

#!/path/to/php

I'm not sure though if a webserver likes this, so if you also want to use the .php file in a website, that might not be the best idea. Still, if you're just writing some kind of script, it is easier to type ./path/to/phpfile.php than having to type php -f /path/to/phpfile.php every time.

3 Comments

The hash bang approach will only work if you make the permissions on the script executable
You can also run which php > filename.php then chmod +x filename.php then edit the file.
Note that @YzmirRamirez's comment will overwrite filename.php if it exists
4

Simply this should do:

php test.php

Comments

4

just run in linux terminal to get phpinfo .

   php -r 'phpinfo();'

and to run file like index.php

    php -f index.php

Comments

1
php -f test.php

See the manual for full details of running PHP from the command line

Comments

1
php test.php

should do it, or

php -f test.php

to be explicit.

1 Comment

what is the difference between those two?
1

I was in need to decode URL in a Bash script. So I decide to use PHP in this way:

$ cat url-decode.sh
#!/bin/bash
URL='url=https%3a%2f%2f1%2fecp%2f'
/usr/bin/php -r '$arg1 = $argv[1];echo rawurldecode($arg1);' "$URL"

Sample output:

$ ./url-decode.sh 
url=https://1/ecp/

Comments

1

just to add another thing related to php and terminal: one way to play with php and test code immediately in the terminal is with

php -a

which will open an interactive php mode where you can test without having to open, save and run files. Its handy for figuring things out like new functions you've never used before.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.