1

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';
Will
  • 24,082
  • 14
  • 97
  • 108
ifusion
  • 2,163
  • 6
  • 24
  • 44

2 Answers2

2

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
Will
  • 24,082
  • 14
  • 97
  • 108
2
  1. add a shebang to your executable file.
  2. 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);
?>
Community
  • 1
  • 1
cozyconemotel
  • 1,121
  • 2
  • 10
  • 22