Home > Enterprise >  execute php files without typing php in front
execute php files without typing php in front

Time:11-24

Anyone can assist me on how to execute my php files without preceding php front Examples instead of typing 'php filename.php' in cli or cloud9

I want to execute them by just typing the file name

command: 'filename'

then the script runs

Well i was thinking of using the htaccess file but i'm not sure if that also works on vps or cli. I know of browser only

CodePudding user response:

Edit your PHP script and add this as the new first line, by itself, before your opening <?php tag:

#!/path/to/php

(Replace that with your actual path.)

Alternatively, if you have the env command, you can use that to figure it out for you:

#!/usr/bin/env php

So your script looks like this:

#!/usr/bin/env php
<?php
echo "Hello, world!\n";

Then make the file exectuable:

chmod u x filename

Now you can just "run" it directly in the shell:

% filename
Hello, world!

Note, you only want to do this if the script is CLI-only. If it's ever run via the web server, that first line will be output to the browser.

  •  Tags:  
  • php
  • Related