0

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 :)

linuxnut
  • 58
  • 5
  • `./test.php` and `php test.php` are the same exact thing, only the former needs to be executable. – Vincent Savard Apr 18 '12 at 01:49
  • Agreed, I was just wondering if one way was more correct for php. It doesn't appear to me that one way is better than the other, and depends on what your doing with the code. – linuxnut Apr 18 '12 at 02:21

2 Answers2

0

If you are writing little php scripts for your own purposes, #! is fine.

If they are to be on some kind of world visible box (e.g. web server), then I'd say not so fine - since you have made them executable they are now a security risk.

I tend to use #! for perl, sh (which are always little private, non production things) and php somefile.php for PHP (which may or may not end up on a server).

John3136
  • 28,809
  • 4
  • 51
  • 69
  • I write alot of perl and bash/shell scripts/programs, and I understand the shebang line, and the security implications of making anything executable. The scripts/programs from the command line, wouldn't be "web facing", and be similar to shell scripts. I'm definately leaning towards chmod+x with the shebang line, so I can leave off the ".php" and still execute it, in case I decide to recode it in some other language, I can still use the same name. Thanks. – linuxnut Apr 18 '12 at 02:12
  • In that case: no difference (but why remove the extension? that just becomes a pain esp. if you are working with >1 language) – John3136 Apr 18 '12 at 02:15
  • If you have an executable, and put it in say /usr/local/bin/myprogram you can call it with just "myprogram" and not "myprogram.php". If at some point you decide to rewrite it in a different language, say C, your name can stay the same, and if it's used in other applications they wouldn't have to be re-written for a new name. Most executables I put into /usr/local/bin/ I leave off the extentions, be it perl/bash/ksh etc. – linuxnut Apr 18 '12 at 02:24
  • I can see the point, but in my experience that never happens - once it is written and it works, it stays as is ;-) But, that is a valid reason if it fits with your circumstances! – John3136 Apr 18 '12 at 03:16
0
#!/usr/bin/php

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

"#! /bin/sh"

, 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.php means 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

rob 23-Mar-2007 11:48

There are better expanations if you see for -q. here's another one

Community
  • 1
  • 1
ro ko
  • 2,906
  • 3
  • 37
  • 58
  • Your last link answered the question about the -q flag. "This only concerns the PHP interpreter built against the CGI SAPI." Testing, with the example they gave, it appears that php with cli, doesn't need the -q flag when run. Thanks. :) – linuxnut Apr 18 '12 at 02:19