NULL expected, won't show up ...

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

    NULL expected, won't show up ...

    Hi group.

    I have tho following code.

    $_GET['artile'] looks like this: /2006/03/lorem_ipsum

    $locationinfo = explode('/', trim( $_GET['article'] , '/') );

    list( $article_year, $article_month, $article_url ) = $locationinfo;

    echo '<br>year: '.(($article_ye ar === NULL)? 'null': $article_year);
    echo '<br>month: '.(($article_mo nth === NULL)? 'null': $article_month) ;
    echo '<br>article: '.(($article_ur l === NULL)? 'null': $article_url);

    The weird thing is, if $_GET['article'] is empty, $article_year isn't
    NULL, but the rest is. This doesn't make any sense to me ...

    Frizzle.

  • ColdShine

    #2
    Re: NULL expected, won't show up ...

    frizzle in news:1142640199 .152903.92170@p 10g2000cwp.goog legroups.com wrote:
    [color=blue]
    > $_GET['artile'] looks like this: /2006/03/lorem_ipsum
    >
    > $locationinfo = explode('/', trim( $_GET['article'] , '/') );
    >
    > list( $article_year, $article_month, $article_url ) = $locationinfo;
    >
    > echo '<br>year: '.(($article_ye ar === NULL)? 'null': $article_year);
    > echo '<br>month: '.(($article_mo nth === NULL)? 'null': $article_month) ;
    > echo '<br>article: '.(($article_ur l === NULL)? 'null': $article_url);
    >
    > The weird thing is, if $_GET['article'] is empty, $article_year isn't
    > NULL, but the rest is. This doesn't make any sense to me ...[/color]

    When there's no match for the specified delimiter, explode() returns an
    array with 1 item (the entire source string). So, assigning this to
    list(...) results in the first list() argument being assigned the value of
    the 1st (and only) item in the array, and the remaining list() arguments are
    set to null (or simply still unset? I don't know).

    So, as in your case, you get trim($_GET['article'], '/') == ''; explode('/',
    '') returns array(''), and list($a_y, $a_m, $a_u) translates into $a_y = '';
    $a_m = null, $a_u = null.

    --
    ColdShine

    "Experience is a hard teacher: she gives the test first, the lesson
    afterwards." - Vernon Sanders law


    Comment

    • frizzle

      #3
      Re: NULL expected, won't show up ...


      ColdShine wrote:[color=blue]
      > frizzle in news:1142640199 .152903.92170@p 10g2000cwp.goog legroups.com wrote:
      >[color=green]
      > > $_GET['artile'] looks like this: /2006/03/lorem_ipsum
      > >
      > > $locationinfo = explode('/', trim( $_GET['article'] , '/') );
      > >
      > > list( $article_year, $article_month, $article_url ) = $locationinfo;
      > >
      > > echo '<br>year: '.(($article_ye ar === NULL)? 'null': $article_year);
      > > echo '<br>month: '.(($article_mo nth === NULL)? 'null': $article_month) ;
      > > echo '<br>article: '.(($article_ur l === NULL)? 'null': $article_url);
      > >
      > > The weird thing is, if $_GET['article'] is empty, $article_year isn't
      > > NULL, but the rest is. This doesn't make any sense to me ...[/color]
      >
      > When there's no match for the specified delimiter, explode() returns an
      > array with 1 item (the entire source string). So, assigning this to
      > list(...) results in the first list() argument being assigned the value of
      > the 1st (and only) item in the array, and the remaining list() arguments are
      > set to null (or simply still unset? I don't know).
      >
      > So, as in your case, you get trim($_GET['article'], '/') == ''; explode('/',
      > '') returns array(''), and list($a_y, $a_m, $a_u) translates into $a_y = '';
      > $a_m = null, $a_u = null.
      >
      > --
      > ColdShine
      >
      > "Experience is a hard teacher: she gives the test first, the lesson
      > afterwards." - Vernon Sanders law[/color]

      So validating it as follows would be just as safe?

      echo '<br>year: '.(($article_ye ar === '' )? 'null': $article_year);
      echo '<br>month: '.(($article_mo nth === NULL)? 'null': $article_month) ;
      echo '<br>article: '.(($article_ur l === NULL)? 'null': $article_url);

      Frizzle.

      Comment

      • ColdShine

        #4
        Re: NULL expected, won't show up ...

        frizzle in news:1142644593 .183520.156040@ g10g2000cwb.goo glegroups.com wrote:
        [color=blue]
        > ColdShine wrote:
        >[color=green]
        >> frizzle in news:1142640199 .152903.92170@p 10g2000cwp.goog legroups.com
        >> wrote:
        >>[color=darkred]
        >>> $_GET['artile'] looks like this: /2006/03/lorem_ipsum
        >>>
        >>> $locationinfo = explode('/', trim( $_GET['article'] , '/') );
        >>>
        >>> list( $article_year, $article_month, $article_url ) = $locationinfo;
        >>>
        >>> echo '<br>year: '.(($article_ye ar === NULL)? 'null': $article_year);
        >>> echo '<br>month: '.(($article_mo nth === NULL)? 'null': $article_month) ;
        >>> echo '<br>article: '.(($article_ur l === NULL)? 'null': $article_url);
        >>>
        >>> The weird thing is, if $_GET['article'] is empty, $article_year isn't
        >>> NULL, but the rest is. This doesn't make any sense to me ...[/color]
        >>
        >> When there's no match for the specified delimiter, explode() returns an
        >> array with 1 item (the entire source string). So, assigning this to
        >> list(...) results in the first list() argument being assigned the value
        >> of the 1st (and only) item in the array, and the remaining list()
        >> arguments are set to null (or simply still unset? I don't know).
        >>
        >> So, as in your case, you get trim($_GET['article'], '/') == '';
        >> explode('/', '') returns array(''), and list($a_y, $a_m, $a_u)
        >> translates into $a_y = ''; $a_m = null, $a_u = null.[/color]
        >
        > So validating it as follows would be just as safe?
        >
        > echo '<br>year: '.(($article_ye ar === '' )? 'null': $article_year);
        > echo '<br>month: '.(($article_mo nth === NULL)? 'null': $article_month) ;
        > echo '<br>article: '.(($article_ur l === NULL)? 'null': $article_url);[/color]

        You may choose between several options... here I list two of them:

        if (count($locatio ninfo) == 3)
        {
        list($...) = $locationinfo;
        echo $...; // Assume valid values.
        }
        else
        {
        echo ...; // Assume null values.
        }

        Or:

        echo '<br>year: '.($article_yea r? $article_year: 'null');
        echo '<br>month: '.($article_mon th? $article_month: 'null');
        echo '<br>article: '.($article_url ? $article_url: 'null');

        Since 0 is not a valid value for an year nor it is for a month and a '' URL
        is as useless as 'null', and (bool)'' equals false as (bool)null does, this
        code is conceptually the same as you originally posted.

        --
        ColdShine

        "Experience is a hard teacher: she gives the test first, the lesson
        afterwards." - Vernon Sanders law


        Comment

        • Richard Levasseur

          #5
          Re: NULL expected, won't show up ...

          You can also just do == 0

          "" == 0
          NULL == 0
          array() == 0
          0 == 0

          Coldshine's method is better though. If you require all the data, make
          sure all of the parts have data in them.

          Comment

          • frizzle

            #6
            Re: NULL expected, won't show up ...


            Richard Levasseur wrote:[color=blue]
            > You can also just do == 0
            >
            > "" == 0
            > NULL == 0
            > array() == 0
            > 0 == 0
            >
            > Coldshine's method is better though. If you require all the data, make
            > sure all of the parts have data in them.[/color]

            Thank you both! So in fact it was ok to do my second option.
            What i don't understand is the following:

            echo '<br>year: '.($article_yea r? $article_year: 'null');
            echo '<br>month: '.($article_mon th? $article_month: 'null');
            echo '<br>article: '.($article_url ? $article_url: 'null');

            I know the statements, but what does it look for in this case?
            Does it detect wether or not e.g. $article_year is set at all?

            Frizzle.

            Comment

            • ColdShine

              #7
              Re: NULL expected, won't show up ...

              frizzle in news:1142679430 .087309.144470@ g10g2000cwb.goo glegroups.com wrote:
              [color=blue]
              > Richard Levasseur wrote:[color=green]
              >> You can also just do == 0
              >>
              >> "" == 0
              >> NULL == 0
              >> array() == 0
              >> 0 == 0
              >>
              >> Coldshine's method is better though. If you require all the data, make
              >> sure all of the parts have data in them.[/color]
              >
              > Thank you both! So in fact it was ok to do my second option.
              > What i don't understand is the following:
              >
              > echo '<br>year: '.($article_yea r? $article_year: 'null');
              > echo '<br>month: '.($article_mon th? $article_month: 'null');
              > echo '<br>article: '.($article_url ? $article_url: 'null');
              >
              > I know the statements, but what does it look for in this case?
              > Does it detect wether or not e.g. $article_year is set at all?
              >
              > Frizzle.[/color]


              Comment

              • ColdShine

                #8
                Re: NULL expected, won't show up ...

                frizzle in news:1142679430 .087309.144470@ g10g2000cwb.goo glegroups.com wrote:
                [color=blue]
                > Richard Levasseur wrote:[color=green]
                >> You can also just do == 0
                >>
                >> "" == 0
                >> NULL == 0
                >> array() == 0
                >> 0 == 0
                >>
                >> Coldshine's method is better though. If you require all the data, make
                >> sure all of the parts have data in them.[/color]
                >
                > Thank you both! So in fact it was ok to do my second option.
                > What i don't understand is the following:
                >
                > echo '<br>year: '.($article_yea r? $article_year: 'null');
                > echo '<br>month: '.($article_mon th? $article_month: 'null');
                > echo '<br>article: '.($article_url ? $article_url: 'null');
                >
                > I know the statements, but what does it look for in this case?
                > Does it detect wether or not e.g. $article_year is set at all?[/color]

                Sorry for my empty reply...
                Anyways: if a variable is not set, casting it to bool (as done by testing
                statements) will result in an E_NOTICE and will return false. Casting a 0 or
                a null also return false.

                --
                ColdShine

                "Experience is a hard teacher: she gives the test first, the lesson
                afterwards." - Vernon Sanders law


                Comment

                • frizzle

                  #9
                  Re: NULL expected, won't show up ...


                  ColdShine wrote:[color=blue]
                  > frizzle in news:1142679430 .087309.144470@ g10g2000cwb.goo glegroups.com wrote:
                  >[color=green]
                  > > Richard Levasseur wrote:[color=darkred]
                  > >> You can also just do == 0
                  > >>
                  > >> "" == 0
                  > >> NULL == 0
                  > >> array() == 0
                  > >> 0 == 0
                  > >>
                  > >> Coldshine's method is better though. If you require all the data, make
                  > >> sure all of the parts have data in them.[/color]
                  > >
                  > > Thank you both! So in fact it was ok to do my second option.
                  > > What i don't understand is the following:
                  > >
                  > > echo '<br>year: '.($article_yea r? $article_year: 'null');
                  > > echo '<br>month: '.($article_mon th? $article_month: 'null');
                  > > echo '<br>article: '.($article_url ? $article_url: 'null');
                  > >
                  > > I know the statements, but what does it look for in this case?
                  > > Does it detect wether or not e.g. $article_year is set at all?[/color]
                  >
                  > Sorry for my empty reply...
                  > Anyways: if a variable is not set, casting it to bool (as done by testing
                  > statements) will result in an E_NOTICE and will return false. Casting a 0 or
                  > a null also return false.
                  >
                  > --
                  > ColdShine
                  >
                  > "Experience is a hard teacher: she gives the test first, the lesson
                  > afterwards." - Vernon Sanders law[/color]

                  ok, thank you!
                  I have it up and running now, and tested a lot, and it works great.
                  Thanks.

                  Comment

                  Working...