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';

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.