Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running php script (php function) in linux bash

How we run php script using Linux bash?

php file test.php

test.php contains:

<?php echo "hello\n" ?> 
like image 657
chrithccmc Avatar asked Apr 05 '11 09:04

chrithccmc


People also ask

How do I run a PHP file in bash?

Show activity on this post. Put that at the top of your script, make it executable ( chmod +x myscript. php ), and make a Cron job to execute that script (same way you'd execute a bash script). You can also use php myscript.

Can I run PHP script from command line?

A PHP script can be executed from the command line even without having any web server software installed. To run the PHP script from the command line you should just have a PHP CLI (PHP Command Line Interface) installed on your system.


2 Answers

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

like image 195
Dunhamzzz Avatar answered Oct 03 '22 03:10

Dunhamzzz


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");'
like image 34
Ali Avatar answered Oct 03 '22 02:10

Ali