Array

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

    #1

    Array

    I have made a script that redirect a user to the page related to the month.
    I bind the month numbers to the url as uitrukken.php?m d=05

    Now....i would print the month name on my page. I have made the following
    array.

    <?PHP
    $maanden = array(
    01 => "Januari",
    02 => "Februari",
    03 => "Maart",
    04 => "April",
    05 => "Mei",
    06 => "Juni",
    07 => "Juli",
    08 => "Augustus",
    09 => "September" ,
    10 => "Oktober",
    11 => "November",
    12 => "December"
    );

    echo $maanden['$md'];
    ?>

    Why doen't show dis the month. In which format can i use a array with a
    variabele as echo $maanden['$md'];

    FiremanSAM


  • Jan Pieter Kunst

    #2
    Re: Array

    In article <tZfbb.5640$P51 .8734@amstwist0 0>,
    "FiremanSAM " <firemansam@co. uk> wrote:
    [color=blue]
    > Why doen't show dis the month. In which format can i use a array with a
    > variabele as echo $maanden['$md'];[/color]

    use double quotes, otherwise variables will not be interpolated:

    echo $maanden["$md"];

    If register_global s is off in your setup, you'll need to do this:

    echo $maanden[$_GET['md']];

    JP

    --
    Sorry, <devnull@cauce. org> is een "spam trap".
    E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

    Comment

    • Ronald Beilsma

      #3
      Re: Array

      "FiremanSAM " <firemansam@co. uk> schreef in bericht
      news:tZfbb.5640 $P51.8734@amstw ist00...[color=blue]
      > I have made a script that redirect a user to the page related to the[/color]
      month.[color=blue]
      > I bind the month numbers to the url as uitrukken.php?m d=05
      >
      > Now....i would print the month name on my page. I have made the following
      > array.
      >
      > <?PHP
      > $maanden = array(
      > 01 => "Januari",
      > 02 => "Februari",
      > 03 => "Maart",
      > 04 => "April",
      > 05 => "Mei",
      > 06 => "Juni",
      > 07 => "Juli",
      > 08 => "Augustus",
      > 09 => "September" ,
      > 10 => "Oktober",
      > 11 => "November",
      > 12 => "December"
      > );
      >
      > echo $maanden['$md'];
      > ?>
      >
      > Why doen't show dis the month. In which format can i use a array with a
      > variabele as echo $maanden['$md'];[/color]

      Drop the single quotes:

      echo $maanden[$md];


      Comment

      • Bernhard Hörlberger

        #4
        Re: Array

        > > $maanden = array([color=blue][color=green]
        > > 01 => "Januari",
        > > 02 => "Februari",
        > > 03 => "Maart",
        > > 04 => "April",
        > > 05 => "Mei",
        > > 06 => "Juni",
        > > 07 => "Juli",
        > > 08 => "Augustus",
        > > 09 => "September" ,
        > > 10 => "Oktober",
        > > 11 => "November",
        > > 12 => "December"
        > > );[/color][/color]

        you could of course use strftime("%B") (http://at2.php.net/strftime) and
        setlocale() (http://at2.php.net/manual/en/function.setlocale.php) to have
        the month names in your language.....


        Comment

        • BKDotCom

          #5
          Re: Array

          Jan Pieter Kunst <devnull@cauce. org> wrote in message >[color=blue]
          > use double quotes, otherwise variables will not be interpolated:
          >
          > echo $maanden["$md"];[/color]

          sorry, you've just hit my pet-peeve. Surrounding a variable in
          double-quotes is a waste, PHP doesn't know what's in the quotes and
          must go in a parse the string to find any variables to expand,
          escaped, chars, or whatever. Variables don't need to be quoted.. just
          use $maanden[$md]. Can someone back-up my belief that they
          "shouldn't" be quoted?

          Comment

          • Matthias Esken

            #6
            Re: Array

            BradKent@swbell .net (BKDotCom) schrieb:
            [color=blue]
            > Jan Pieter Kunst <devnull@cauce. org> wrote in message >[color=green]
            >> use double quotes, otherwise variables will not be interpolated:
            >>
            >> echo $maanden["$md"];[/color]
            >
            > sorry, you've just hit my pet-peeve. Surrounding a variable in
            > double-quotes is a waste, PHP doesn't know what's in the quotes and
            > must go in a parse the string to find any variables to expand,
            > escaped, chars, or whatever. Variables don't need to be quoted.. just
            > use $maanden[$md]. Can someone back-up my belief that they
            > "shouldn't" be quoted?[/color]

            It takes just some few extra microseconds of time if you use double
            quotes, but that adds up with each time you run those scripts.

            I prefer to use no quotes if there would be just one variable inside the
            string and I use single quotes if the string contains no variable that
            has to be interpolated.

            Regards,
            Matthias

            Comment

            • Jan Pieter Kunst

              #7
              Re: Array

              In article <e8269f23.03092 11242.6eabf88d@ posting.google. com>,
              BradKent@swbell .net (BKDotCom) wrote:
              [color=blue][color=green]
              > > use double quotes, otherwise variables will not be interpolated:
              > >
              > > echo $maanden["$md"];[/color]
              >
              > sorry, you've just hit my pet-peeve. Surrounding a variable in
              > double-quotes is a waste, PHP doesn't know what's in the quotes and
              > must go in a parse the string to find any variables to expand,
              > escaped, chars, or whatever. Variables don't need to be quoted.. just
              > use $maanden[$md]. Can someone back-up my belief that they
              > "shouldn't" be quoted?[/color]

              You are right. I probably had the rule 'You should always use quotes
              around a string literal array index'
              (see <http://nl2.php.net/types.array>) in mind, but that is about string
              literals, not about variables. There is no need to use double quotes
              around variable array keys. (I doubt that there will be a noticeable
              speed difference, though.)

              JP

              --
              Sorry, <devnull@cauce. org> is een "spam trap".
              E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

              Comment

              • Randell D.

                #8
                Re: Array


                "BKDotCom" <BradKent@swbel l.net> wrote in message
                news:e8269f23.0 309211242.6eabf 88d@posting.goo gle.com...[color=blue]
                > Jan Pieter Kunst <devnull@cauce. org> wrote in message >[color=green]
                > > use double quotes, otherwise variables will not be interpolated:
                > >
                > > echo $maanden["$md"];[/color]
                >
                > sorry, you've just hit my pet-peeve. Surrounding a variable in
                > double-quotes is a waste, PHP doesn't know what's in the quotes and
                > must go in a parse the string to find any variables to expand,
                > escaped, chars, or whatever. Variables don't need to be quoted.. just
                > use $maanden[$md]. Can someone back-up my belief that they
                > "shouldn't" be quoted?[/color]

                I *would* have agreed until I tested both... and you'll be surprised of the
                results...

                The performance difference would be small on a quiet server with a small
                workload - However, given a busier environment, the performance enhancement
                differs almost 50%.

                In my test, I had a loop which checked the value of an array element - The
                element was also a variable - First test had the element variable inside
                quotes (thus $testArray["$element"]) while the second test excluded the
                double quotes (thus $testArray[$element]).

                I ran this on my server whereby I was the only user - the box was almost
                asleep so I cannot blame it on other environment changes - I also ran the
                test twice. Test 1 took 86seconds while test 2 took 152 seconds...

                Surprised? So was I... It looks like its better to include double quotes as
                opposed to excluding them - I've not worked out why just yet... I'll pitch a
                seperate post asking folk to comment on this... My test script follows
                below:

                <?
                // Perform a test $maxLoop number of times, count the number of seconds
                // it takes to complete the loop - Test 1 has the array element named
                // inside double quotes, Test 2 excludes the double quotes
                set_time_limit( 250);
                $testArray=$_EN V;
                $maxLoop=100000 ;
                /////////////////////// Test 1 ////////////////////
                $start=time();
                for($loopMany=0 ; $loopMany<$maxL oop; ++$loopMany)
                { foreach($testAr ray as $elementName=>$ elementValue)
                { if($testArray["$elementNa me"]=="HOST")
                { $thisHost="$ele mentValue"; }
                }
                }
                $end=time();
                $difference=$en d-$start;
                print("<br>Dura tion 1: $difference<hr> ");

                set_time_limit( 250);
                $testArray=$_EN V;
                $maxLoop=100000 ;
                /////////////////////// Test 2 ////////////////////
                for($loopMany=0 ; $loopMany<$maxL oop; ++$loopMany)
                { foreach($testAr ray as $elementName=>$ elementValue)
                { if($testArray[$elementName]=="HOST")
                { $thisHost="$ele mentValue"; }
                }
                }
                $end=time();
                $difference=$en d-$start;
                print("<br>Dura tion 2: $difference<hr> ");
                ?>


                Comment

                • BKDotCom

                  #9
                  Re: Array

                  *cough cough*
                  Your test 2 took 66 seconds.
                  you didn't reinitialize $start after test1 compleated... therefore
                  152 seconds is the total time your script ran.

                  so:
                  86 sec for test 1 (quotes)
                  66 sec for test 2 (no quotes)
                  152 sec: total

                  but, bonus points to you for actually taking the time to research this.


                  "Randell D." <you.can.email. me.at.randelld@ yahoo.com> wrote in message news:<Eevbb.204 019$la.4059525@ news1.calgary.s haw.ca>...[color=blue]
                  > "BKDotCom" <BradKent@swbel l.net> wrote in message
                  > news:e8269f23.0 309211242.6eabf 88d@posting.goo gle.com...[color=green]
                  > > Jan Pieter Kunst <devnull@cauce. org> wrote in message >[color=darkred]
                  > > > use double quotes, otherwise variables will not be interpolated:
                  > > >
                  > > > echo $maanden["$md"];[/color]
                  > >
                  > > sorry, you've just hit my pet-peeve. Surrounding a variable in
                  > > double-quotes is a waste, PHP doesn't know what's in the quotes and
                  > > must go in a parse the string to find any variables to expand,
                  > > escaped, chars, or whatever. Variables don't need to be quoted.. just
                  > > use $maanden[$md]. Can someone back-up my belief that they
                  > > "shouldn't" be quoted?[/color]
                  >
                  > I *would* have agreed until I tested both... and you'll be surprised of the
                  > results...
                  >
                  > The performance difference would be small on a quiet server with a small
                  > workload - However, given a busier environment, the performance enhancement
                  > differs almost 50%.
                  >
                  > In my test, I had a loop which checked the value of an array element - The
                  > element was also a variable - First test had the element variable inside
                  > quotes (thus $testArray["$element"]) while the second test excluded the
                  > double quotes (thus $testArray[$element]).
                  >
                  > I ran this on my server whereby I was the only user - the box was almost
                  > asleep so I cannot blame it on other environment changes - I also ran the
                  > test twice. Test 1 took 86seconds while test 2 took 152 seconds...
                  >
                  > Surprised? So was I... It looks like its better to include double quotes as
                  > opposed to excluding them - I've not worked out why just yet... I'll pitch a
                  > seperate post asking folk to comment on this... My test script follows
                  > below:
                  >
                  > <?
                  > // Perform a test $maxLoop number of times, count the number of seconds
                  > // it takes to complete the loop - Test 1 has the array element named
                  > // inside double quotes, Test 2 excludes the double quotes
                  > set_time_limit( 250);
                  > $testArray=$_EN V;
                  > $maxLoop=100000 ;
                  > /////////////////////// Test 1 ////////////////////
                  > $start=time();
                  > for($loopMany=0 ; $loopMany<$maxL oop; ++$loopMany)
                  > { foreach($testAr ray as $elementName=>$ elementValue)
                  > { if($testArray["$elementNa me"]=="HOST")
                  > { $thisHost="$ele mentValue"; }
                  > }
                  > }
                  > $end=time();
                  > $difference=$en d-$start;
                  > print("<br>Dura tion 1: $difference<hr> ");
                  >
                  > set_time_limit( 250);
                  > $testArray=$_EN V;
                  > $maxLoop=100000 ;
                  > /////////////////////// Test 2 ////////////////////
                  > for($loopMany=0 ; $loopMany<$maxL oop; ++$loopMany)
                  > { foreach($testAr ray as $elementName=>$ elementValue)
                  > { if($testArray[$elementName]=="HOST")
                  > { $thisHost="$ele mentValue"; }
                  > }
                  > }
                  > $end=time();
                  > $difference=$en d-$start;
                  > print("<br>Dura tion 2: $difference<hr> ");
                  > ?>[/color]

                  Comment

                  • Randell D.

                    #10
                    Re: Array


                    "BKDotCom" <BradKent@swbel l.net> wrote in message
                    news:e8269f23.0 309220613.36be2 a1e@posting.goo gle.com...[color=blue]
                    > *cough cough*
                    > Your test 2 took 66 seconds.
                    > you didn't reinitialize $start after test1 compleated... therefore
                    > 152 seconds is the total time your script ran.
                    >
                    > so:
                    > 86 sec for test 1 (quotes)
                    > 66 sec for test 2 (no quotes)
                    > 152 sec: total
                    >
                    > but, bonus points to you for actually taking the time to research this.
                    >
                    >
                    > "Randell D." <you.can.email. me.at.randelld@ yahoo.com> wrote in message[/color]
                    news:<Eevbb.204 019$la.4059525@ news1.calgary.s haw.ca>...[color=blue][color=green]
                    > > "BKDotCom" <BradKent@swbel l.net> wrote in message
                    > > news:e8269f23.0 309211242.6eabf 88d@posting.goo gle.com...[color=darkred]
                    > > > Jan Pieter Kunst <devnull@cauce. org> wrote in message >
                    > > > > use double quotes, otherwise variables will not be interpolated:
                    > > > >
                    > > > > echo $maanden["$md"];
                    > > >
                    > > > sorry, you've just hit my pet-peeve. Surrounding a variable in
                    > > > double-quotes is a waste, PHP doesn't know what's in the quotes and
                    > > > must go in a parse the string to find any variables to expand,
                    > > > escaped, chars, or whatever. Variables don't need to be quoted.. just
                    > > > use $maanden[$md]. Can someone back-up my belief that they
                    > > > "shouldn't" be quoted?[/color]
                    > >
                    > > I *would* have agreed until I tested both... and you'll be surprised of[/color][/color]
                    the[color=blue][color=green]
                    > > results...
                    > >
                    > > The performance difference would be small on a quiet server with a small
                    > > workload - However, given a busier environment, the performance[/color][/color]
                    enhancement[color=blue][color=green]
                    > > differs almost 50%.
                    > >
                    > > In my test, I had a loop which checked the value of an array element -[/color][/color]
                    The[color=blue][color=green]
                    > > element was also a variable - First test had the element variable inside
                    > > quotes (thus $testArray["$element"]) while the second test excluded the
                    > > double quotes (thus $testArray[$element]).
                    > >
                    > > I ran this on my server whereby I was the only user - the box was almost
                    > > asleep so I cannot blame it on other environment changes - I also ran[/color][/color]
                    the[color=blue][color=green]
                    > > test twice. Test 1 took 86seconds while test 2 took 152 seconds...
                    > >
                    > > Surprised? So was I... It looks like its better to include double[/color][/color]
                    quotes as[color=blue][color=green]
                    > > opposed to excluding them - I've not worked out why just yet... I'll[/color][/color]
                    pitch a[color=blue][color=green]
                    > > seperate post asking folk to comment on this... My test script follows
                    > > below:
                    > >
                    > > <?
                    > > // Perform a test $maxLoop number of times, count the number of seconds
                    > > // it takes to complete the loop - Test 1 has the array element named
                    > > // inside double quotes, Test 2 excludes the double quotes
                    > > set_time_limit( 250);
                    > > $testArray=$_EN V;
                    > > $maxLoop=100000 ;
                    > > /////////////////////// Test 1 ////////////////////
                    > > $start=time();
                    > > for($loopMany=0 ; $loopMany<$maxL oop; ++$loopMany)
                    > > { foreach($testAr ray as $elementName=>$ elementValue)
                    > > { if($testArray["$elementNa me"]=="HOST")
                    > > { $thisHost="$ele mentValue"; }
                    > > }
                    > > }
                    > > $end=time();
                    > > $difference=$en d-$start;
                    > > print("<br>Dura tion 1: $difference<hr> ");
                    > >
                    > > set_time_limit( 250);
                    > > $testArray=$_EN V;
                    > > $maxLoop=100000 ;
                    > > /////////////////////// Test 2 ////////////////////
                    > > for($loopMany=0 ; $loopMany<$maxL oop; ++$loopMany)
                    > > { foreach($testAr ray as $elementName=>$ elementValue)
                    > > { if($testArray[$elementName]=="HOST")
                    > > { $thisHost="$ele mentValue"; }
                    > > }
                    > > }
                    > > $end=time();
                    > > $difference=$en d-$start;
                    > > print("<br>Dura tion 2: $difference<hr> ");
                    > > ?>[/color][/color]


                    That is a bit embarrassing isn't it.... thanks for pointing that one out -
                    It could have steered me (and others) in the wrong direction...


                    Comment

                    Working...