globals ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yang Li Ke

    globals ?

    Hi anyone know why a user running PHP Version 4.3.4
    on a windows system cant run a script having a simple
    code like this:

    if($myvar){
    header("Locatio n: $redirecturl");
    }

    it gives some undefined var error

    I need help!

    Thank you
    --
    Yang


  • Jon Kraft

    #2
    Re: globals ?

    "Yang Li Ke" <yanglike@sympa tico.ca> wrote:
    [color=blue]
    > Hi anyone know why a user running PHP Version 4.3.4
    > on a windows system cant run a script having a simple
    > code like this:
    >
    > if($myvar){
    > header("Locatio n: $redirecturl");
    > }
    >
    > it gives some undefined var error[/color]


    It doesn't return an error but a notice. Because $myvar is obviously not
    defined.

    if(isset($myvar )){

    is the better solution, although if the original code always returned the
    notice, I presume "your friend" makes the common mistake of not keeping up
    to date with PHP development and still expects form fields becoming
    $variables as if by magic.




    HTH;
    JOn

    Comment

    • Michael Meckelein

      #3
      Re: globals ?

      "Yang Li Ke" wrote[color=blue]
      > Hi anyone know why a user running PHP Version 4.3.4
      > on a windows system cant run a script having a simple
      > code like this:
      >
      > if($myvar){
      > header("Locatio n: $redirecturl");
      > }
      >
      > it gives some undefined var error[/color]

      Maybe you are using register_global s Off? In which way get you the $myvar,
      $redirecturl?

      If from a form via post, use this:
      $_POST['myvar'], ...

      or via get request, use this:
      $_GET['myvar'], ...

      Since PHP 4.2.0 register_global s is by default off. This is nice, don't
      change this!

      For more information see the manual at www.php.net

      Michael

      Comment

      • Dan Tripp

        #4
        Re: globals ?

        Michael Meckelein wrote:
        [color=blue]
        > "Yang Li Ke" wrote
        >[color=green]
        >>Hi anyone know why a user running PHP Version 4.3.4
        >>on a windows system cant run a script having a simple
        >>code like this:
        >>
        >>if($myvar){
        >> header("Locatio n: $redirecturl");
        >>}
        >>
        >>it gives some undefined var error[/color]
        >
        >
        > Maybe you are using register_global s Off? In which way get you the $myvar,
        > $redirecturl?
        >
        > If from a form via post, use this:
        > $_POST['myvar'], ...
        >
        > or via get request, use this:
        > $_GET['myvar'], ...
        >
        > Since PHP 4.2.0 register_global s is by default off. This is nice, don't
        > change this!
        >
        > For more information see the manual at www.php.net
        >
        > Michael
        >[/color]

        I generally use something like this:

        if(ISSET($_POST['var'])){
        $var = $_POST['var'];
        } else {
        $var = "";
        }

        This way, if I haven't sent the variable $var to the form, I don't get
        an error.

        It can also be smooshed to one line...

        if(ISSET($_POST['var'])){ $var = $_POST['var']; } else { var = ""; }

        Regards,

        - Dan

        Comment

        • Pedro Graca

          #5
          Re: globals ?

          Dan Tripp wrote:[color=blue]
          > It can also be smooshed to one line...
          >
          > if(ISSET($_POST['var'])){ $var = $_POST['var']; } else { var = ""; }[/color]


          or, if you prefer a little more cryptic code:

          $var = @$_POST['var']?$_POST['var']:'';
          --
          --= my mail box only accepts =--
          --= Content-Type: text/plain =--
          --= Size below 10001 bytes =--

          Comment

          • Dan Tripp

            #6
            Re: globals ?

            Pedro Graca wrote:[color=blue]
            > Dan Tripp wrote:
            >[color=green]
            >>It can also be smooshed to one line...
            >>
            >>if(ISSET($_PO ST['var'])){ $var = $_POST['var']; } else { var = ""; }[/color]
            >
            >
            >
            > or, if you prefer a little more cryptic code:
            >
            > $var = @$_POST['var']?$_POST['var']:'';[/color]

            ROCK!

            Thank you Pedro, that will save me *A LOT* of typing! Woot!

            - Dan

            Comment

            • Dan Tripp

              #7
              Re: globals ?

              Pedro Graca wrote:
              [color=blue]
              > Dan Tripp wrote:
              >[color=green]
              >>It can also be smooshed to one line...
              >>
              >>if(ISSET($_PO ST['var'])){ $var = $_POST['var']; } else { var = ""; }[/color]
              >
              >
              >
              > or, if you prefer a little more cryptic code:
              >
              > $var = @$_POST['var']?$_POST['var']:'';[/color]


              "PHP offers an alternative syntax for some of its control structures;
              namely, if, while, for, foreach, and switch. In each case, the basic
              form of the alternate syntax is to change the opening brace to a colon
              (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or
              endswitch;, respectively."



              .... and here's the specific part that I think is cool:

              "The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1
              evaluates to TRUE, and expr3 if expr1 evaluates to FALSE."


              Thanks again! =)

              - Dan

              Comment

              • Eric Bohlman

                #8
                Re: globals ?

                Dan Tripp <thisIsNot@MyEM ailAddress.com> wrote in
                news:f0%Rb.1908 5$Rz.13648@news svr25.news.prod igy.com:
                [color=blue][color=green]
                >> $var = @$_POST['var']?$_POST['var']:'';[/color]
                >
                > ROCK!
                >
                > Thank you Pedro, that will save me *A LOT* of typing! Woot![/color]

                Even less typing: $var = $_POST['var'] or '';

                Comment

                • Pedro Graca

                  #9
                  Re: globals ?

                  Dan Tripp wrote:[color=blue]
                  > Pedro Graca wrote:[color=green]
                  >> $var = @$_POST['var']?$_POST['var']:'';[/color][/color]
                  [color=blue]
                  > ... and here's the specific part that I think is cool:
                  >
                  > "The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1
                  > evaluates to TRUE, and expr3 if expr1 evaluates to FALSE."
                  > http://us2.php.net/manual/en/languag...comparison.php[/color]

                  Yep! The ternary expression is what I used, with the '@' error
                  supressing:

                  $_POST['var'] evaluates to true if it has something except '0'.
                  If it is empty it evaluates to false.

                  If it does not exist and error_reporting is set to display Notices,
                  without the '@' for error suppressing, php would show the notice about
                  "undefined index". The '@' suppresses the error and treats $_POST['var']
                  as if it was the null string.
                  --
                  --= my mail box only accepts =--
                  --= Content-Type: text/plain =--
                  --= Size below 10001 bytes =--

                  Comment

                  • Martin Lucas-Smith

                    #10
                    Re: globals ?



                    [color=blue][color=green][color=darkred]
                    > >> $var = @$_POST['var']?$_POST['var']:'';[/color][/color]
                    >
                    > Yep! The ternary expression is what I used, with the '@' error
                    > supressing:
                    >
                    > $_POST['var'] evaluates to true if it has something except '0'.
                    > If it is empty it evaluates to false.
                    >
                    > If it does not exist and error_reporting is set to display Notices,
                    > without the '@' for error suppressing, php would show the notice about
                    > "undefined index". The '@' suppresses the error and treats $_POST['var']
                    > as if it was the null string.[/color]

                    But it's still an error. Surely the most correct way to handle this, if
                    using the ternary operator, is using isSet to check whether it exists:

                    $var = (isSet ($_POST['var']) ? $_POST['var'] : '');



                    Martin

                    Comment

                    • Pedro Graca

                      #11
                      Re: globals ?

                      Martin Lucas-Smith wrote:[color=blue][color=green][color=darkred]
                      >> >> $var = @$_POST['var'] ? $_POST['var'] : '' ;[/color][/color]
                      > $var = (isSet ($_POST['var']) ? $_POST['var'] : '');[/color]

                      I agree the second form is better.
                      .... and (unexpectedely) it is faster! *much* faster!!

                      #v+
                      <?php
                      $start1 = microtime();
                      $n = 1000000; while (--$n) {
                      $var1 = @$_POST['var'] ? $_POST['var'] : '' ;
                      }
                      $end1 = microtime();

                      $start2 = microtime();
                      $n = 1000000; while (--$n) {
                      $var2 = (isSet ($_POST['var']) ? $_POST['var'] : '');
                      }
                      $end2 = microtime();

                      $delta1 = array_sum(explo de(' ', $end1)) - array_sum(explo de(' ', $start1));
                      $delta2 = array_sum(explo de(' ', $end2)) - array_sum(explo de(' ', $start2));

                      echo "delta1 = $delta1\ndelta2 = $delta2\n\n";
                      ?>
                      #v-

                      Result on my computer:
                      delta1 = 20.6845738888
                      delta2 = 3.95724010468
                      --
                      --= my mail box only accepts =--
                      --= Content-Type: text/plain =--
                      --= Size below 10001 bytes =--

                      Comment

                      • Andy Hassall

                        #12
                        Re: globals ?

                        On Thu, 29 Jan 2004 12:00:31 +0000, Martin Lucas-Smith <mvl22@cam.ac.u k> wrote:
                        [color=blue][color=green][color=darkred]
                        >>>> $var = @$_POST['var']?$_POST['var']:'';[/color]
                        >>
                        >> Yep! The ternary expression is what I used, with the '@' error
                        >> supressing:
                        >>
                        >> $_POST['var'] evaluates to true if it has something except '0'.
                        >> If it is empty it evaluates to false.
                        >>
                        >> If it does not exist and error_reporting is set to display Notices,
                        >> without the '@' for error suppressing, php would show the notice about
                        >> "undefined index". The '@' suppresses the error and treats $_POST['var']
                        >> as if it was the null string.[/color]
                        >
                        >But it's still an error. Surely the most correct way to handle this, if
                        >using the ternary operator, is using isSet to check whether it exists:
                        >
                        >$var = (isSet ($_POST['var']) ? $_POST['var'] : '');[/color]

                        That's definitely the formally correct way of doing it, but I admit I've been
                        tending sometimes to use just:

                        $var = @$_POST['var'];

                        ... on the grounds that the only thing that can go wrong there is that it's an
                        invalid index, and you get $var set to NULL. This also gives you a distinction
                        between 'var' not having been sent at all, or having been sent as an empty
                        string. But if you're just going to later do an 'if ($var) { something; }' then
                        it doesn't much matter that it's empty or NULL; they both convert to false.

                        But for the situations where you always want it as a string, the
                        isset-with-ternary statement above is definitely the way to go.

                        --
                        Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
                        <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

                        Comment

                        • Andy Hassall

                          #13
                          Re: globals ?

                          On 29 Jan 2004 08:43:08 GMT, Eric Bohlman <ebohlman@earth link.net> wrote:
                          [color=blue]
                          >Dan Tripp <thisIsNot@MyEM ailAddress.com> wrote in
                          >news:f0%Rb.190 85$Rz.13648@new ssvr25.news.pro digy.com:
                          >[color=green][color=darkred]
                          >>> $var = @$_POST['var']?$_POST['var']:'';[/color]
                          >>
                          >> ROCK!
                          >>
                          >> Thank you Pedro, that will save me *A LOT* of typing! Woot![/color]
                          >
                          >Even less typing: $var = $_POST['var'] or '';[/color]

                          Doesn't avoid the warning, though, since you're using an undefined index.

                          --
                          Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
                          <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

                          Comment

                          Working...