Vars not valid outside function

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

    Vars not valid outside function

    Hi,

    I'm trying to create a function which gives the total of time. Now the
    problem is that the function resets the int. $hour. How can I get them to be
    saved... I tried global $hour etc... but it didn't help and I couldn't find
    it.
    How can I get these vars to be valid through out the whole script..

    Thanks in advance, Maarten

    $hour = 0;
    $minute = 0;
    $second = 0;

    function total_time ($track_id)
    {
    $hour = substr ( $track_id, -6, -4 ) + $hour;
    $minute = substr ( $track_id, -4, -2 ) + $minute;
    $second = substr ( $track_id, -2 ) + $second;
    echo "$hour:$minute: $second <br />";
    }


  • Daniel Tryba

    #2
    Re: Vars not valid outside function

    MuffinMan <blah5ReMoVe*TH iS@custodis.cis tron.nl> wrote:[color=blue]
    > I'm trying to create a function which gives the total of time. Now the
    > problem is that the function resets the int.[/color]

    No it doesn't.
    [color=blue]
    > $hour. How can I get them to be saved... I tried global $hour etc...
    > but it didn't help and I couldn't find it.[/color]

    To bad you didn't post an example where you used global. My guess is you
    put the keyword global in the wrong place.
    [color=blue]
    > How can I get these vars to be valid through out the whole script..[/color]

    You should readup on scopes in PHP:


    Take a close loop al example 7.1 or 7.2.

    --

    Daniel Tryba

    Comment

    • Pedro Graca

      #3
      Re: Vars not valid outside function

      MuffinMan wrote:[color=blue]
      > I'm trying to create a function which gives the total of time. Now the
      > problem is that the function resets the int. $hour. How can I get them to be
      > saved... I tried global $hour etc... but it didn't help and I couldn't find
      > it.
      > How can I get these vars to be valid through out the whole script..
      >
      > Thanks in advance, Maarten
      >
      > $hour = 0;
      > $minute = 0;
      > $second = 0;
      >
      > function total_time ($track_id)
      > {
      > $hour = substr ( $track_id, -6, -4 ) + $hour;
      > $minute = substr ( $track_id, -4, -2 ) + $minute;
      > $second = substr ( $track_id, -2 ) + $second;
      > echo "$hour:$minute: $second <br />";
      > }[/color]


      I'd go about that somewhat differently :-)

      <?php // stopwatch.inc.p hp
      define('STOP_WA TCH_START', '0');
      define('STOP_WA TCH_STOP', '1');

      function stop_watch($mod e) {
      static $now = 0;

      switch ($mode) {
      case STOP_WATCH_STAR T:
      $now = time();
      return true;
      break;
      case STOP_WATCH_STOP :
      return time()-$now;
      break;
      default:
      return false;
      }
      }
      ?>


      and then in a script where you want it:

      <?php
      require_once 'stopwatch.inc. php';

      stop_watch(STOP _WATCH_START);

      /* do something */
      sleep(10);

      echo date('H:i:s', stop_watch(STOP _WATCH_STOP));
      ?>

      --
      USENET would be a better place if everybody read: : mail address :
      http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
      http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
      http://www.expita.com/nomime.html : to 10K bytes :

      Comment

      • MuffinMan

        #4
        Re: Vars not valid outside function


        | To bad you didn't post an example where you used global. My guess is you
        | put the keyword global in the wrong place.

        True, I had tried a few things except for the right one... oh well... works
        excellend now.... now the finishing touch..
        Thanks.. Maarten


        Comment

        • Pedro Graca

          #5
          Re: Vars not valid outside function

          Pedro Graca wrote:[color=blue]
          > <?php
          > require_once 'stopwatch.inc. php';
          >
          > stop_watch(STOP _WATCH_START);
          >
          > /* do something */
          > sleep(10);
          >
          > echo date('H:i:s', stop_watch(STOP _WATCH_STOP));[/color]

          /* BUG! */
          /* should be gmdate(): */
          echo gmdate('H:i:s', stop_watch(STOP _WATCH_STOP));
          [color=blue]
          > ?>[/color]

          Sorry folks :)

          --
          USENET would be a better place if everybody read: : mail address :
          http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
          http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
          http://www.expita.com/nomime.html : to 10K bytes :

          Comment

          • MuffinMan

            #6
            Re: Vars not valid outside function


            "Pedro Graca" <hexkid@hotpop. com> schreef in bericht
            news:2g2cjlF3rh hqU2@uni-berlin.de...
            | Pedro Graca wrote:
            | > <?php
            | > require_once 'stopwatch.inc. php';
            | >
            | > stop_watch(STOP _WATCH_START);
            | >
            | > /* do something */
            | > sleep(10);
            | >
            | > echo date('H:i:s', stop_watch(STOP _WATCH_STOP));
            |
            | /* BUG! */
            | /* should be gmdate(): */
            | echo gmdate('H:i:s', stop_watch(STOP _WATCH_STOP));
            |
            | > ?>
            |

            Thanks but I wanted to calculate the total time of all tracks of my cd
            collection and since I got all track time's in a database it shouldn't be
            hard to calculate....

            Maarten


            Comment

            • Pedro Graca

              #7
              Re: Vars not valid outside function

              MuffinMan wrote:[color=blue]
              >
              > "Pedro Graca" <hexkid@hotpop. com> schreef in bericht
              > news:2g2cjlF3rh hqU2@uni-berlin.de...[/color]

              (snip a bunch of code unrelated to the OP's intention)
              [color=blue]
              > Thanks but I wanted to calculate the total time of all tracks of my cd
              > collection and since I got all track time's in a database it shouldn't be
              > hard to calculate....[/color]

              Oops, sorry for the wrong assumption :-)

              Maybe you'd like to change the database and transform the column for the
              track time into an int representing the number of seconds; then you just

              select sum(track_time) from table

              to know how many seconds are in your total collection.


              Converting 617000 seconds to "7 days, 3 hours, 23 minutes and 20 seconds"
              is left as an exercise :-)

              --
              USENET would be a better place if everybody read: : mail address :
              http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
              http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
              http://www.expita.com/nomime.html : to 10K bytes :

              Comment

              • Geoff Berrow

                #8
                Re: Vars not valid outside function

                I noticed that Message-ID: <2g4kgvF4dr33U1 @uni-berlin.de> from Pedro
                Graca contained the following:
                [color=blue]
                >Converting 617000 seconds to "7 days, 3 hours, 23 minutes and 20 seconds"
                >is left as an exercise :-)[/color]

                function secstofull($sec onds){
                $days=floor($se conds/(24*60*60));
                $remainder=$sec onds%(24*60*60) ;
                $hours= floor($remainde r/3600);
                $remainder=$sec onds%(60*60);
                $mins=floor($re mainder/60);
                $secs=$seconds% 60;
                print "$days days, $hours hours, $mins minutes, $secs seconds";
                }

                But I bet there is an easier way...
                --
                Geoff Berrow (put thecat out to email)
                It's only Usenet, no one dies.
                My opinions, not the committee's, mine.
                Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                Comment

                • MuffinMan

                  #9
                  Re: Vars not valid outside function


                  | Maybe you'd like to change the database and transform the column for the
                  | track time into an int representing the number of seconds; then you just
                  |
                  | select sum(track_time) from table

                  I could yes, but as one entry can be used for several tunes.. like one is
                  the same artist and tune name... but from a diffent year and arrangement...
                  so it is impossible to give one entry a constant track time. Instead of that
                  I've created a field with a sort of cd id in which can excist more than one
                  id which are diveded by ';'. They point to the cd which they're from and
                  what length they are.. in seconds
                  next digit explains which cd it's from... cd 1 or cd 2 etc
                  next digit explains which track it is starting at 0
                  the rest is just the length... HH:MM:SS
                  example:
                  id starts with # to make searching a bit easier..
                  than the first 5 numbers are the cd id wich cd the tune points in this case
                  #1000010000635 --- ( in one field more of these numbers can excist and they
                  are divided by ; )
                  The above track ID point to the cd id below.... the first five digets below
                  are the same as the first five digits above starting from the '#'
                  10000, AC/DC, Live: Collectors Edition, Rock, 1992, CD ----- id, artist,
                  album, genre, year, media

                  Hope this make any sense... if not... I'll hear... and I'll try harder to
                  explain... bit late now...... bye bye

                  Maarten


                  Comment

                  Working...