Why are comments preceeded by // not ignored?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • deko

    Why are comments preceeded by // not ignored?

    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.


  • Andy Hassall

    #2
    Re: Why are comments preceeded by // not ignored?

    On Sun, 28 Mar 2004 00:53:07 GMT, "deko" <dje422@hotmail .com> wrote:
    [color=blue]
    >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[/color]
    ^^^^^ ^

    Eek! This is dangerous. Log in as a user, not root, unless you really have to.
    [color=blue]
    >./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?[/color]

    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 <andy@andyh.co. uk> / Space: disk usage analysis tool
    http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

    Comment

    • Jay Moore

      #3
      Re: Why are comments preceeded by // not ignored?

      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

      Comment

      • deko

        #4
        Re: Why are comments preceeded by // not ignored?

        > No; as with all shell scripts, you need to tell the system what to interpret[color=blue]
        > it with. Add:
        >
        > #!/usr/bin/php -q[/color]

        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




        Comment

        • Andy Hassall

          #5
          Re: Why are comments preceeded by // not ignored?

          On Sun, 28 Mar 2004 01:40:07 GMT, "deko" <dje422@hotmail .com> wrote:
          [color=blue][color=green]
          >> No; as with all shell scripts, you need to tell the system what to interpret
          >> it with. Add:
          >>
          >> #!/usr/bin/php -q[/color]
          >
          >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[/color]
          [color=blue]
          >but now I'm getting this:
          >
          >Error in argument 1, char 3: option not found[/color]

          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 <andy@andyh.co. uk> / Space: disk usage analysis tool
          http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

          Comment

          • deko

            #6
            Re: Why are comments preceeded by // not ignored?

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

            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.


            Comment

            • Riddic

              #7
              Re: Why are comments preceeded by // not ignored?

              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

              Comment

              • deko

                #8
                Re: Why are comments preceeded by // not ignored?

                > #!/usr/bin/php -q[color=blue]
                > <?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();
                > ?>[/color]

                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?


                Comment

                Working...