By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,631 Members | 1,693 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,631 IT Pros & Developers. It's quick & easy.

Why are comments preceeded by // not ignored?

P: n/a
Problem occurs only when running php script at the command line.

Here is an example:

<?php
//myscript.php
//-should be called from some page.
//-sets cookie on visitor's first visit - expires in 2 days (default).
//-does this and that (and also the other thing).
//records special data in /var/blah.txt (when using another-script.php).
//////////files//////////
$blah = '/var/blah.txt'
/////////////////////////
phpinfo()
?>

When I try to run this script at the command line, the comments produce errors:

rh9 [/root] # ./myscript.php

../myscript.php: line 1: ?php: No such file or directory
../myscript.php: line 2: //myscript.php: No such file or directory
../myscript.php: line 3: //-should: No such file or directory
../myscript.php: line 8: unexpected EOF while looking for matching `''
../myscript.php: line 12: syntax error: unexpected end of file

Is there something I need to set in php.ini, or am I missing something?

Thanks in advance.
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Sun, 28 Mar 2004 00:53:07 GMT, "deko" <dj****@hotmail.com> wrote:
Problem occurs only when running php script at the command line.

Here is an example:

<?php
//myscript.php
//-should be called from some page.
//-sets cookie on visitor's first visit - expires in 2 days (default).
//-does this and that (and also the other thing).
//records special data in /var/blah.txt (when using another-script.php).
//////////files//////////
$blah = '/var/blah.txt'
/////////////////////////
phpinfo()
?>

When I try to run this script at the command line, the comments produce errors:

rh9 [/root] # ./myscript.php ^^^^^ ^

Eek! This is dangerous. Log in as a user, not root, unless you really have to.
./myscript.php: line 1: ?php: No such file or directory
./myscript.php: line 2: //myscript.php: No such file or directory
./myscript.php: line 3: //-should: No such file or directory
./myscript.php: line 8: unexpected EOF while looking for matching `''
./myscript.php: line 12: syntax error: unexpected end of file

Is there something I need to set in php.ini, or am I missing something?


No; as with all shell scripts, you need to tell the system what to interpret
it with. Add:

#!/usr/bin/php -q

... as the first line of the script. (Or wherever the php binary is - use
'which php' if you don't know).

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #2

P: n/a
I have the same question. I completely commented out a script with /*
and */ so it wouldn't run, but when I called the script, it produced an
error when it got to the line creating a new class object because the
class didn't exist due to the fact the require with the class in it was
commented out.

If that's not clear, here's a code snippet:

<?php
/*
require_once('/path/to/file/with/class/info.php');
$foo = new bar;
*/
?>

Yet when I ran it, I got a "Unable to blah class blah on line X" error.

Why?

-Jay

Jul 17 '05 #3

P: n/a
> No; as with all shell scripts, you need to tell the system what to interpret
it with. Add:

#!/usr/bin/php -q


Thanks for the reply.

I checked the location of php:

rh9 [/root] # which php
/usr/bin/php
rh9 [/root] #

And added the she-bang:

#!/usr/bin/php -q
<?php
//myscript.php
//-should be called from some page.
//-sets cookie on visitor's first visit - expires in 2 days (default).
//-does this and that (and also the other thing).
//records special data (in thisscript.php).
//////////files//////////
$blah = '/var/blah.txt'
/////////////////////////
phpinfo();
?>

but now I'm getting this:

Error in argument 1, char 3: option not found
Error in argument 1, char 3: option not found
Usage: php [-q] [-h] [-s [-v] [-i] [-f <file>] | {<file> [args...]}
-q Quiet-mode. Suppress HTTP Header output.
-s Display colour syntax highlighted source.
-w Display source with stripped comments and whitespace.
-f <file> Parse <file>. Implies `-q'
-v Version number
-C Do not chdir to the script's directory
-c <path> Look for php.ini file in this directory
-a Run interactively
-d foo[=bar] Define INI entry foo with value 'bar'
-e Generate extended information for debugger/profiler
-z <file> Load Zend extension <file>.
-l Syntax check only (lint)
-m Show compiled in modules
-i PHP information
-h This help


Jul 17 '05 #4

P: n/a
On Sun, 28 Mar 2004 01:40:07 GMT, "deko" <dj****@hotmail.com> wrote:
No; as with all shell scripts, you need to tell the system what to interpret
it with. Add:

#!/usr/bin/php -q
Thanks for the reply.

I checked the location of php:

rh9 [/root] # which php
/usr/bin/php
rh9 [/root] #

And added the she-bang:

#!/usr/bin/php -q
<?php

but now I'm getting this:

Error in argument 1, char 3: option not found


What did you edit it with? I bet you've got Windows line endings on that
instead of Unix ones. Convert it with 'dos2unix' or similar, or maybe your
editor has an option for this.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #5

P: n/a
> What did you edit it with? I bet you've got Windows line endings on that
instead of Unix ones. Convert it with 'dos2unix' or similar, or maybe your
editor has an option for this.


I used :set list and :set nu in vi to get this output:

rh9 [/root] # vi myscript.php

1 #!/usr/bin/php -q$
2 <?php$
3 //myscript.php$
4 //-should be called from some page.$
5 //-sets cookie on visitor's first visit - expires in 2 days (default).$
6 //-does this and that (and also the other thing).$
7 //records special data (in thisscript.php).$
8 //////////files//////////$
9 $blah = '/var/blah.txt'$
10 /////////////////////////$
11 phpinfo();$
12 ?>$
~
~
~

As you can see, no Windows line breaks (I believe I would see ^M if there were).

I will keep troubleshooting this... open to any suggestions.

PS. the reason this (not the script above, but another script) needs to be run
from shell is because it needs to be scheduled in a crontab - it will update
several web pages.
Jul 17 '05 #6

P: n/a
try:

#!/usr/bin/php -q
<?php
//myscript.php
//-should be called from some page.
//-sets cookie on visitor's first visit - expires in 2 days (default).
//-does this and that (and also the other thing).
//records special data (in thisscript.php).
//////////files//////////
$blah = '/var/blah.txt';
/////////////////////////
phpinfo();
?>

Since PHP didn't specify the line where it stubbed it's toe, I figured
it was that one ';' you forgot after the $blah declaration ;)

- Riddic
Jul 17 '05 #7

P: n/a
> #!/usr/bin/php -q
<?php
//myscript.php
//-should be called from some page.
//-sets cookie on visitor's first visit - expires in 2 days (default).
//-does this and that (and also the other thing).
//records special data (in thisscript.php).
//////////files//////////
$blah = '/var/blah.txt';
/////////////////////////
phpinfo();
?>


The above script worked okay when run from the shell using ./myscript.php

But I am still having trouble with the real script. Is there anything I need to
do differently, aside from the she-bang, when running a php script from shell -
i.e. from a crontab? Are there things I can do in a web page that I cannot do
from shell? Other considerations?
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.