I want to be able to type into the command line, and have the following PHP script execute like so:
$ hello.php
Question is, where do I save my hello.php
file to run it like this. I see a lot of tutorials showing this way, but they do not mention where to save the actual file. If I save it on my desktop then it will run when I type in:
~/Desktop/hello.php
I want to be able to just type in the file name regardless of what directory I'm in and have it execute.
Do I have to setup an alias? Or can I just put it in a specific folder.
hello.php
<?php
echo 'Hello! This is a test';
All you have to do is add a shebang line:
#!/usr/bin/php
<?php
echo 'Hello! This is a test';
And then:
chmod +x hello.php
Then you can run it from the same directory by just typing:
./hello.php
If you want to be able to run it from anywhere, put it somewhere in $PATH
, such as /usr/local/bin
. You can either actually put the file there, or change your PATH, or create a symbolic link. Then, you can run it as:
hello.php
chmod +x file_name.php
to make it runnable.just like this:
#!/usr/bin/php
<?php
var_dump($argv);
?>
this one is a bit better (see this question):
#!/usr/bin/env php
<?php
var_dump($argv);
?>