Cannot use "-" in variable names?

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

    Cannot use "-" in variable names?

    The following gives an error:

    <?php
    $super-man=100;
    ?>

    Parse error: parse error, unexpected '=' in /test.php on line 2

    I understand that the "-" sign is seen as an operator.
    is there a way to get around this? I need to have "-" in variable names.

    Thanks,
    Ross
  • Nicholas Sherlock

    #2
    Re: Cannot use &quot;-&quot; in variable names?

    ross wrote:[color=blue]
    > The following gives an error:
    >
    > <?php
    > $super-man=100;
    > ?>
    >
    > Parse error: parse error, unexpected '=' in /test.php on line 2
    >
    > I understand that the "-" sign is seen as an operator.
    > is there a way to get around this? I need to have "-" in variable names.[/color]

    No, you cannot use the "-" sign in variables. You can use "_".

    Cheers,
    Nicholas Sherlock

    Comment

    • Kim André Akerø

      #3
      Re: Cannot use &quot;-&quot; in variable names?

      ross wrote:
      [color=blue]
      > The following gives an error:
      >
      > <?php
      > $super-man=100;
      > ?>
      >
      > Parse error: parse error, unexpected '=' in /test.php on line 2
      >
      > I understand that the "-" sign is seen as an operator.[/color]

      Correct, the "-" sign is considered as a minus sign.
      [color=blue]
      > is there a way to get around this? I need to have "-" in variable
      > names.[/color]

      Why?

      You can safely use "_" (underscore) instead. If you need to use it in a
      query string or form field, use $_POST, $_GET or $_REQUEST.

      --
      Kim André Akerø
      - kimandre@NOSPAM betadome.com
      (remove NOSPAM to contact me directly)

      Comment

      • Justin Koivisto

        #4
        Re: Cannot use &quot;-&quot; in variable names?

        Kim André Akerø wrote:[color=blue]
        > ross wrote:
        >
        >[color=green]
        >>The following gives an error:
        >>
        >><?php
        >>$super-man=100;
        >>?>
        >>
        >>Parse error: parse error, unexpected '=' in /test.php on line 2
        >>
        >>I understand that the "-" sign is seen as an operator.[/color]
        >
        >
        > Correct, the "-" sign is considered as a minus sign.
        >
        >[color=green]
        >>is there a way to get around this? I need to have "-" in variable
        >>names.[/color]
        >
        >
        > Why?
        >
        > You can safely use "_" (underscore) instead. If you need to use it in a
        > query string or form field, use $_POST, $_GET or $_REQUEST.[/color]

        - characters in those arrays are replaced by _ automagically.. .

        --
        Justin Koivisto, ZCE - justin@koivi.co m

        Comment

        • Kim André Akerø

          #5
          Re: Cannot use &quot;-&quot; in variable names?

          Justin Koivisto wrote:
          [color=blue]
          > Kim André Akerø wrote:[color=green]
          > > ross wrote:
          > >[color=darkred]
          > > > The following gives an error:
          > > >
          > > > <?php
          > > > $super-man=100;
          > > > ?>
          > > >
          > > > Parse error: parse error, unexpected '=' in /test.php on line 2
          > > >
          > > > I understand that the "-" sign is seen as an operator.[/color]
          > >
          > > Correct, the "-" sign is considered as a minus sign.
          > >[color=darkred]
          > > > is there a way to get around this? I need to have "-" in variable
          > > > names.[/color]
          > >
          > > Why?
          > >
          > > You can safely use "_" (underscore) instead. If you need to use it
          > > in a query string or form field, use $_POST, $_GET or $_REQUEST.[/color]
          >
          > - characters in those arrays are replaced by _ automagically.. .[/color]

          I wasn't aware of that. Then again, I've never had the need/use to have
          a "-" in a variable (either in a form or as a query string).

          --
          Kim André Akerø
          - kimandre@NOSPAM betadome.com
          (remove NOSPAM to contact me directly)

          Comment

          • Wayne

            #6
            Re: Cannot use &quot;-&quot; in variable names?

            On Fri, 09 Dec 2005 14:20:25 -0500, ross <aoxo_oxoa@yaho o.com> wrote:
            [color=blue]
            >I understand that the "-" sign is seen as an operator.
            >is there a way to get around this? I need to have "-" in variable names.[/color]

            Why do you need to have "-" in variable names?!?
            (answering this question might help us find you an alternative)

            Comment

            • Ian

              #7
              Re: Cannot use &quot;-&quot; in variable names?

              ross wrote:[color=blue]
              > The following gives an error:
              >
              > <?php
              > $super-man=100;
              > ?>
              >
              > Parse error: parse error, unexpected '=' in /test.php on line 2
              >
              > I understand that the "-" sign is seen as an operator.
              > is there a way to get around this? I need to have "-" in variable names.
              >[/color]
              Why? No one else does.

              Ian

              Comment

              • Malcolm Dew-Jones

                #8
                Re: Cannot use &quot;-&quot; in variable names?

                Justin Koivisto (justin@koivi.c om) wrote:

                : Kim André Akerø wrote:
                : >
                : > You can safely use "_" (underscore) instead. If you need to use it in a
                : > query string or form field, use $_POST, $_GET or $_REQUEST.

                : - characters in those arrays are replaced by _ automagically.. .

                There must be a config setting to do that, cause I have never observed
                that behaviour. The following uses - in the html parameter names.


                <?php

                echo "the-name = [";
                echo $_REQUEST['the-name'];
                echo "]\n";


                echo "hidden-name = [";
                echo $_REQUEST['hidden-name'];
                echo "]\n";

                ?>

                <form>

                <input type="text" name="the-name" />

                <input type="hidden" name="hidden-name" value="a hidden value" />

                <input type=submit />

                </form>


                Comment

                • Justin Koivisto

                  #9
                  Re: Cannot use &quot;-&quot; in variable names?

                  Malcolm Dew-Jones wrote:[color=blue]
                  > Justin Koivisto (justin@koivi.c om) wrote:
                  >
                  > : Kim André Akerø wrote:
                  > : >
                  > : > You can safely use "_" (underscore) instead. If you need to use it in a
                  > : > query string or form field, use $_POST, $_GET or $_REQUEST.
                  >
                  > : - characters in those arrays are replaced by _ automagically.. .
                  >
                  > There must be a config setting to do that, cause I have never observed
                  > that behaviour. The following uses - in the html parameter names.
                  >
                  >
                  > <?php
                  >
                  > echo "the-name = [";
                  > echo $_REQUEST['the-name'];
                  > echo "]\n";
                  >
                  >
                  > echo "hidden-name = [";
                  > echo $_REQUEST['hidden-name'];
                  > echo "]\n";
                  >
                  > ?>
                  >
                  > <form>
                  >
                  > <input type="text" name="the-name" />
                  >
                  > <input type="hidden" name="hidden-name" value="a hidden value" />
                  >
                  > <input type=submit />
                  >
                  > </form>
                  >
                  >[/color]

                  Now I wonder if I have that mixed up with something else dealing with
                  hyphens in the field names of posted forms... (PDF forms perhaps?)

                  I know it came up once in a script I was debugging a couple years ago,
                  so maybe it was something from a previous version, or maybe it was if
                  register_global s = 1

                  --
                  Justin Koivisto, ZCE - justin@koivi.co m

                  Comment

                  • Chung Leong

                    #10
                    Re: Cannot use &quot;-&quot; in variable names?

                    Malcolm Dew-Jones wrote:[color=blue]
                    > Justin Koivisto (justin@koivi.c om) wrote:
                    >
                    > : Kim André Akerø wrote:
                    > : >
                    > : > You can safely use "_" (underscore) instead. If you need to use it ina
                    > : > query string or form field, use $_POST, $_GET or $_REQUEST.
                    >
                    > : - characters in those arrays are replaced by _ automagically.. .
                    >
                    > There must be a config setting to do that, cause I have never observed
                    > that behaviour. The following uses - in the html parameter names.[/color]

                    I would have sworn to god that that's how it work, at least in the case
                    of register_global s = 1 and extract(). Yet that's not the behavior when
                    I tried. Dashes remain dashes in $_GET and the variable isn't set.
                    Weird...

                    Comment

                    • Justin Koivisto

                      #11
                      Re: Cannot use &quot;-&quot; in variable names?

                      Chung Leong wrote:[color=blue]
                      > Malcolm Dew-Jones wrote:
                      >[color=green]
                      >>Justin Koivisto (justin@koivi.c om) wrote:
                      >>
                      >>: Kim André Akerø wrote:
                      >>: >
                      >>: > You can safely use "_" (underscore) instead. If you need to use it in a
                      >>: > query string or form field, use $_POST, $_GET or $_REQUEST.
                      >>
                      >>: - characters in those arrays are replaced by _ automagically.. .
                      >>
                      >>There must be a config setting to do that, cause I have never observed
                      >>that behaviour. The following uses - in the html parameter names.[/color]
                      >
                      >
                      > I would have sworn to god that that's how it work, at least in the case
                      > of register_global s = 1 and extract(). Yet that's not the behavior when
                      > I tried. Dashes remain dashes in $_GET and the variable isn't set.
                      > Weird...[/color]

                      Good, I'm not completely loosing it them... ;)

                      --
                      Justin Koivisto, ZCE - justin@koivi.co m

                      Comment

                      • Chung Leong

                        #12
                        Re: Cannot use &quot;-&quot; in variable names?


                        Justin Koivisto wrote:[color=blue]
                        > Chung Leong wrote:[color=green]
                        > > Malcolm Dew-Jones wrote:
                        > >[color=darkred]
                        > >>Justin Koivisto (justin@koivi.c om) wrote:
                        > >>
                        > >>: Kim André Akerø wrote:
                        > >>: >
                        > >>: > You can safely use "_" (underscore) instead. If you need to use it in a
                        > >>: > query string or form field, use $_POST, $_GET or $_REQUEST.
                        > >>
                        > >>: - characters in those arrays are replaced by _ automagically.. .
                        > >>
                        > >>There must be a config setting to do that, cause I have never observed
                        > >>that behaviour. The following uses - in the html parameter names.[/color]
                        > >
                        > >
                        > > I would have sworn to god that that's how it work, at least in the case
                        > > of register_global s = 1 and extract(). Yet that's not the behavior when
                        > > I tried. Dashes remain dashes in $_GET and the variable isn't set.
                        > > Weird...[/color]
                        >
                        > Good, I'm not completely loosing it them... ;)
                        >
                        > --
                        > Justin Koivisto, ZCE - justin@koivi.co m
                        > http://koivi.com[/color]

                        Aha, now I remember! It's the period/fullstop that gets automatically
                        replaced by an underscore, not the dash.

                        Comment

                        • ross

                          #13
                          Re: Cannot use &quot;-&quot; in variable names?

                          I "was" parsing a xml file and would like to create variables on the fly
                          with the same name as the XML tags. some of the xml tags are in the
                          form "xx-yy" etc.

                          I now know the xml structure and I am reverting back to xx_yy.

                          Comment

                          Working...