Is there a correct way to use php from the command line…or rather…is one way more correct than another ?
If you create a file, say test.php with the following code:
#!/usr/bin/php
<?php
print "This is a test".PHP_EOL;
print "This is another test!!";
?>
then chmod +x text.php (make it executable on linux).
You can then run in the following ways…..
./test.php
or
php test.php
I prefer just using ./test.php, but often see php test.php in examples.
ALSO
is the following correct syntax for the shebang line
#!/usr/bin/php
or is this more correct
#!/usr/bin/php -q
I’ve seen both, and see that the -q flag is to quiet the html stuff, but was wondering if
php compiled with cli compatibility really needs the -q flag ???
Thanks for your help 🙂
On the first line of an interpreter script, the “#!”, is the name of a program which should be used to interpret the contents of the file. For instance, if the first line contains
, then the contents of the file are executed as a shell script.
In your case it means excute the script using the php file in /usr/bin location.
Using
php test.phpmeans that you are running your test.php with php so u don’t need the first line. where as for ./test.php your file needs to be executable and first line is required.And about the -q flag look at this
specifically on
There are better expanations if you see for -q. here’s another one