How we run php script using linux bash ?

php file test.php

test.php contain.

<?php echo "hello\n" ?>
share|improve this question

6 Answers

up vote 3 down vote accepted

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).

share|improve this answer

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.

share|improve this answer
The hash bang approach will only work if you make the permissions on the script executable – symcbean Apr 5 '11 at 9:31
@symcbean Good point, edited my answer. – Darhuuk Apr 5 '11 at 9:42
You can also run which php > filename.php then chmod +x filename.php then edit the file. – Yzmir Ramirez Oct 12 '12 at 2:14

Simply this should do:

php test.php
share|improve this answer

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");'
share|improve this answer
php -f test.php

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

share|improve this answer
php test.php

should do it, or

php -f test.php

to be explicit.

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.