1

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

4
  • 3
    Why do you want do to this? Commented Nov 24, 2022 at 2:38
  • look at shebang to get started. Commented Nov 24, 2022 at 2:58
  • @YvesLeBorg, Alright will check for that, I hope you do understand what i mean? Commented Nov 24, 2022 at 3:01
  • @AlesiaThiel yep, your question was perfectly clear to me. I do that often in some of my projects. Commented Nov 24, 2022 at 11:15

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

it says this : bash: ./filename: /usr/bin/php^M: bad interpreter: No such file or directory
Its worked, i added this command , sed -i -e 's/\r$//' filename.php . Thank you

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.