Why is break so lame, and no goto

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fshsoup@gmail.com

    Why is break so lame, and no goto

    If i write this

    $page = 'start';
    for (;;) {
    include "$page.inc" ;
    }

    and inside start.inc I put a "break;" it should work, right? no it
    doesn't. And why isn't there any "goto" statement in PHP? is it
    impossible for code in the start.inc to get out of my loop?

    john
  • Jerry Stuckle

    #2
    Re: Why is break so lame, and no goto

    fshsoup@gmail.c om wrote:
    If i write this
    >
    $page = 'start';
    for (;;) {
    include "$page.inc" ;
    }
    >
    and inside start.inc I put a "break;" it should work, right? no it
    doesn't. And why isn't there any "goto" statement in PHP? is it
    impossible for code in the start.inc to get out of my loop?
    >
    john
    >
    There is no goto in PHP because that doesn't fit structured programming
    techniques - and can cause other problems.

    You should be able to break out of the loop - but every time you go
    through the loop you are going ton include the page again
    (include_once() will fix this problem).

    I don't know what's in the page, but it would be better to structure
    your code so that it sets a flag to exit the loop, put the loop in the
    included file or similar.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Tim Streater

      #3
      Re: Why is break so lame, and no goto

      In article
      <a7a70e43-6df2-4360-9b8f-bd433a9c5a22@64 g2000hsm.google groups.com>,
      fshsoup@gmail.c om wrote:
      If i write this
      >
      $page = 'start';
      for (;;) {
      include "$page.inc" ;
      }
      >
      and inside start.inc I put a "break;" it should work, right? no it
      doesn't. And why isn't there any "goto" statement in PHP? is it
      impossible for code in the start.inc to get out of my loop?
      goto is lame. I haven't needed to use one since I stopped writing
      FORTRAN in 1978.

      Comment

      • fshsoup@gmail.com

        #4
        Re: Why is break so lame, and no goto

        On Oct 16, 1:41 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
        fshs...@gmail.c om wrote:
        If i write this
        >
        $page = 'start';
        for (;;) {
            include "$page.inc" ;
        }
        >
        and inside start.inc I put a "break;" it should work, right?  no it
        doesn't.  And why isn't there any "goto" statement in PHP?  is it
        impossible for code in the start.inc to get out of my loop?
        >
        john
        >
        There is no goto in PHP because that doesn't fit structured programming
        techniques - and can cause other problems.
        >
        You should be able to break out of the loop - but every time you go
        through the loop you are going ton include the page again
        (include_once() will fix this problem).
        >
        I don't know what's in the page, but it would be better to structure
        your code so that it sets a flag to exit the loop, put the loop in the
        included file or similar.
        >
        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstuck...@attgl obal.net
        =============== ===
        Thanks Jerry, but you don't understand the paradigm. I have lots of
        inc files, start.inc, readname.inc, sumadd.inc, revoke.inc, etc... and
        each one sets the $page variable to the one that should be included
        next. The main does not know how many times to go around the loop,
        nor when to leave the loop. I was hoping I could just put a "break;"
        in the last one. That would be nice a clean. But I'll just have to
        set a flag like

        $page = 'start';
        $stop = false;
        while (!$stop) {
        include "$page.inc" ;
        }

        Comment

        • FutureShock

          #5
          Re: Why is break so lame, and no goto

          fshsoup@gmail.c om wrote:
          On Oct 16, 1:41 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
          >fshs...@gmail. com wrote:
          >>If i write this
          >>$page = 'start';
          >>for (;;) {
          >> include "$page.inc" ;
          >>}
          >>and inside start.inc I put a "break;" it should work, right? no it
          >>doesn't. And why isn't there any "goto" statement in PHP? is it
          >>impossible for code in the start.inc to get out of my loop?
          >>john
          >There is no goto in PHP because that doesn't fit structured programming
          >techniques - and can cause other problems.
          >>
          >You should be able to break out of the loop - but every time you go
          >through the loop you are going ton include the page again
          >(include_once( ) will fix this problem).
          >>
          >I don't know what's in the page, but it would be better to structure
          >your code so that it sets a flag to exit the loop, put the loop in the
          >included file or similar.
          >>
          >--
          >============== ====
          >Remove the "x" from my email address
          >Jerry Stuckle
          >JDS Computer Training Corp.
          >jstuck...@attg lobal.net
          >============== ====
          >
          Thanks Jerry, but you don't understand the paradigm. I have lots of
          inc files, start.inc, readname.inc, sumadd.inc, revoke.inc, etc... and
          each one sets the $page variable to the one that should be included
          next. The main does not know how many times to go around the loop,
          nor when to leave the loop. I was hoping I could just put a "break;"
          in the last one. That would be nice a clean. But I'll just have to
          set a flag like
          >
          $page = 'start';
          $stop = false;
          while (!$stop) {
          include "$page.inc" ;
          }
          >
          If you give a little more details on what you are trying to achieve,
          there maybe a better way to go about it. There is a great deal of
          knowledge in here and I am sure if you have thought it up, it has been
          done before and maybe even more effective. No use in reinventing the wheel.

          Also on a side note, I notice you spewed out a bunch of include files
          with the inc extension. If you are using those extensions, make sure
          they are above your root directory or they will be visible to the web,
          unless more security methods are employeed. Or maybe rename them to
          lets say..... .php

          starte_inc.php etc.. IMHO

          Scotty

          Comment

          • Tim Roberts

            #6
            Re: Why is break so lame, and no goto

            fshsoup@gmail.c om wrote:
            >
            >If i write this
            >
            >$page = 'start';
            >for (;;) {
            include "$page.inc" ;
            >}
            >
            >and inside start.inc I put a "break;" it should work, right? no it
            >doesn't.
            "break" breaks out of a scope. The "include" statement does more than just
            insert text -- it apparently opens up a new scope.

            You could have the include set a variable, and use that variable to
            terminate the loop.
            --
            Tim Roberts, timr@probo.com
            Providenza & Boekelheide, Inc.

            Comment

            • Thomas Mlynarczyk

              #7
              Re: Why is break so lame, and no goto

              Tim Roberts schrieb:
              "break" breaks out of a scope. The "include" statement does more than just
              insert text -- it apparently opens up a new scope.
              Then, theoretically, "break 2" should do the trick?

              Greetings,
              Thomas

              --
              Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
              (Coluche)

              Comment

              • C. (http://symcbean.blogspot.com/)

                #8
                Re: Why is break so lame, and no goto

                On 17 Oct, 12:28, Thomas Mlynarczyk <tho...@mlynarc zyk-webdesign.de>
                wrote:
                Tim Roberts schrieb:
                >
                "break" breaks out of a scope. The "include" statement does more than just
                insert text -- it apparently opens up a new scope.
                >
                Then, theoretically, "break 2" should do the trick?
                >
                Greetings,
                Thomas
                >
                --
                Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
                (Coluche)
                Returning a value from the include and parsing it in the loop scope
                would be a slightly better way of doing this. But really the right way
                to do it would be to not use 'include' like this - put it outside the
                loop and call named functions/methods defined in the include file
                instead.

                C.

                Comment

                • Thomas Mlynarczyk

                  #9
                  Re: Why is break so lame, and no goto

                  C. (http://symcbean.blogspot.com/) schrieb:
                  >>"break" breaks out of a scope. The "include" statement does more than just
                  >>insert text -- it apparently opens up a new scope.
                  >Then, theoretically, "break 2" should do the trick?
                  Returning a value from the include and parsing it in the loop scope
                  would be a slightly better way of doing this. But really the right way
                  to do it would be to not use 'include' like this - put it outside the
                  loop and call named functions/methods defined in the include file
                  instead.
                  Yes, of course that is right. I was merely suggesting a way to make the
                  original code "work".

                  Greetings,
                  Thomas

                  --
                  Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
                  (Coluche)

                  Comment

                  • Norman Peelman

                    #10
                    Re: Why is break so lame, and no goto

                    Thomas Mlynarczyk wrote:
                    Tim Roberts schrieb:
                    >
                    >"break" breaks out of a scope. The "include" statement does more than
                    >just
                    >insert text -- it apparently opens up a new scope.
                    >
                    Then, theoretically, "break 2" should do the trick?
                    >
                    Greetings,
                    Thomas
                    >
                    Nope, won't work. The include is a different scope... no amount of break
                    works. Wouldn't it be easier to just include a file that just includes
                    all the includes? Using the OP's way how do you keep track which include
                    is last? I don't mean programmaticall y, I mean physically/mentally. What
                    about:

                    $handle = opendir('/path/to/includes');
                    while (false !== ($file = readdir($handle )))
                    {
                    if (strrchr($fileN ame, '.') == '.inc')
                    {
                    include($file);
                    }
                    }
                    closedir($handl e);


                    --
                    Norman
                    Registered Linux user #461062
                    -Have you been to www.php.net yet?-

                    Comment

                    • fshsoup@gmail.com

                      #11
                      Re: Why is break so lame, and no goto

                      Returning a value from the include and parsing it in the loop scope
                      would be a slightly better way of doing this. But really the right way
                      to do it would be to not use 'include' like this - put it outside the
                      loop and call named functions/methods defined in the include file
                      instead.
                      >
                      Yes, of course that is right. I was merely suggesting a way to make the
                      original code "work".
                      I'm not putting any functions in my .inc files. the .inc files just
                      contain code. In effect I am using the "include" statement as if it
                      were a function call. or more like GOSUB in basic. The main.php will
                      invoke the start.inc, which will "chain" to other inc files. One inc
                      file might create another inc file and then execute it (by setting the
                      $page).

                      See how cool this is. I don't have to write any functions. All the
                      code in the inc files shares the global variable space. new inc files
                      can be added dynamically, even at runtime without any modification to
                      the main.php. It's like an LISP AI system that can morph and learn
                      and self-modify. (Yes i know there are major security holes)

                      thanks for all the advice

                      Comment

                      • Michael Fesser

                        #12
                        Re: Why is break so lame, and no goto

                        ..oO(fshsoup@gm ail.com)
                        >I'm not putting any functions in my .inc files. the .inc files just
                        >contain code. In effect I am using the "include" statement as if it
                        >were a function call.
                        Bad design.
                        >or more like GOSUB in basic.
                        Bad language. PHP is not BASIC!
                        >The main.php will
                        >invoke the start.inc, which will "chain" to other inc files. One inc
                        >file might create another inc file and then execute it (by setting the
                        >$page).
                        >
                        >See how cool this is. I don't have to write any functions.
                        This is _not_ cool, it's just damn ugly.
                        >All the
                        >code in the inc files shares the global variable space. new inc files
                        >can be added dynamically, even at runtime without any modification to
                        >the main.php. It's like an LISP AI system that can morph and learn
                        >and self-modify. (Yes i know there are major security holes)
                        >
                        >thanks for all the advice
                        Maybe learn some basics of structured programming? Or use another
                        language. It seems that PHP is not the right tool for your way of
                        programming.

                        Micha

                        Comment

                        • Gordon

                          #13
                          Re: Why is break so lame, and no goto

                          On Oct 16, 7:41 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                          fshs...@gmail.c om wrote:
                          If i write this
                          >
                          $page = 'start';
                          for (;;) {
                              include "$page.inc" ;
                          }
                          >
                          and inside start.inc I put a "break;" it should work, right?  no it
                          doesn't.  And why isn't there any "goto" statement in PHP?  is it
                          impossible for code in the start.inc to get out of my loop?
                          >
                          john
                          >
                          There is no goto in PHP because that doesn't fit structured programming
                          techniques - and can cause other problems.
                          >
                          You should be able to break out of the loop - but every time you go
                          through the loop you are going ton include the page again
                          (include_once() will fix this problem).
                          >
                          I don't know what's in the page, but it would be better to structure
                          your code so that it sets a flag to exit the loop, put the loop in the
                          included file or similar.
                          >
                          --
                          =============== ===
                          Remove the "x" from my email address
                          Jerry Stuckle
                          JDS Computer Training Corp.
                          jstuck...@attgl obal.net
                          =============== ===
                          Do Not Feed The Troll.

                          Comment

                          • =?UTF-8?B?SXbDoW4gU8OhbmNoZXogT3J0ZWdh?=

                            #14
                            Re: Why is break so lame, and no goto

                            fshsoup@gmail.c om wrote:
                            One inc file might create another inc file and then execute it (by setting
                            the $page).
                            eval(), man.
                            All the code in the inc files shares the global variable space.
                            And how having only one scope is a good thing? Geez.

                            --
                            ----------------------------------
                            Iván Sánchez Ortega -ivan-algarroba-sanchezortega-punto-es-

                            Un ordenador no es un televisor ni un microondas, es una herramienta
                            compleja.

                            Comment

                            • Thomas Mlynarczyk

                              #15
                              Re: Why is break so lame, and no goto

                              Norman Peelman schrieb:
                              >Then, theoretically, "break 2" should do the trick?
                              Nope, won't work.
                              I just tested it (PHP 5.2.4):

                              Main script:

                              for (;;) { include 'inctest.inc.ph p'; }
                              echo ' Done';

                              inctest.inc.php :

                              echo ' Included';
                              break;

                              Result (after echoing " Included"): "Fatal error: Cannot break/continue
                              1 level...". When I read the OPs first posting, I assumed that "doesn't
                              [work]" meant the break was ignored and the loop would run forever. Now
                              I see *how* it does not work. This is confusing, though. With respect to
                              variables, includes behave exactly as if their code was replacing the
                              include statement. For break and return, however, includes behave like
                              functions.

                              Greetings,
                              Thomas

                              --
                              Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
                              (Coluche)

                              Comment

                              Working...