Linux: Executable PHP Files

Another nice thing about Linux is that you don’t need to use a program command to execute a PHP script, if you have the PHP-CLI version installed.

On Windows and for a while on Linux, I used:

php -f myscript.php

This is perfectly fine, but this executable file method is best for simple tool-scripts that you want to run manually on your system.

All you need to do is include this line on the very first line of your PHP file:

#!/usr/bin/php

Note that you still need to use the <?php ?> tags, but they go after that line.

Once you edited your script, you make it executable:

chmod +x myscript.php

And to run it you go to your script’s path and use this:

./myscript.php

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: