By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

Why is break so lame, and no goto

P: n/a
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
Oct 16 '08 #1
Share this Question
Share on Google+
18 Replies


P: n/a
fs*****@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.
js*******@attglobal.net
==================

Oct 16 '08 #2

P: n/a
In article
<a7**********************************@64g2000hsm.g ooglegroups.com>,
fs*****@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?
goto is lame. I haven't needed to use one since I stopped writing
FORTRAN in 1978.
Oct 16 '08 #3

P: n/a
On Oct 16, 1:41*pm, Jerry Stuckle <jstuck...@attglobal.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...@attglobal.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";
}

Oct 16 '08 #4

P: n/a
fs*****@gmail.com wrote:
On Oct 16, 1:41 pm, Jerry Stuckle <jstuck...@attglobal.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...@attglobal.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
Oct 17 '08 #5

P: n/a
fs*****@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.
"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, ti**@probo.com
Providenza & Boekelheide, Inc.
Oct 17 '08 #6

P: n/a
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)
Oct 17 '08 #7

P: n/a
On 17 Oct, 12:28, Thomas Mlynarczyk <tho...@mlynarczyk-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.
Oct 17 '08 #8

P: n/a
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)
Oct 17 '08 #9

P: n/a
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 programmatically, I mean physically/mentally. What
about:

$handle = opendir('/path/to/includes');
while (false !== ($file = readdir($handle)))
{
if (strrchr($fileName, '.') == '.inc')
{
include($file);
}
}
closedir($handle);
--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
Oct 18 '08 #10

P: n/a
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
Oct 18 '08 #11

P: n/a
..oO(fs*****@gmail.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
Oct 18 '08 #12

P: n/a
On Oct 16, 7:41*pm, Jerry Stuckle <jstuck...@attglobal.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...@attglobal.net
==================
Do Not Feed The Troll.
Oct 18 '08 #13

P: n/a
fs*****@gmail.com 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.
Oct 18 '08 #14

P: n/a
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.php'; }
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)
Oct 18 '08 #15

P: n/a
Norman Peelman <np******@cfl.rr.comwrote:
>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?

Nope, won't work. The include is a different scope... no amount of break
works.
May I ask if you actually found this written anywhere? When I answered
Thomas, I wrote "apparently" because, although it made sense and matched my
experiments, I couldn't find it in writing.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Oct 19 '08 #16

P: n/a
fs*****@gmail.com wrote:
>
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.
No offence, but this sounds like a nightmare.
Oct 19 '08 #17

P: n/a
Tim Roberts wrote:
Norman Peelman <np******@cfl.rr.comwrote:
>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?
Nope, won't work. The include is a different scope... no amount of break
works.

May I ask if you actually found this written anywhere? When I answered
Thomas, I wrote "apparently" because, although it made sense and matched my
experiments, I couldn't find it in writing.
I tested it... maybe the word 'scope' is incorrect, I don't know.
What I do know is that it doesn't work - break, break 2, just doesn't work.
--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
Oct 19 '08 #18

P: n/a
On Oct 16, 9:05*pm, 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
while ( $page is not empty )
{
include "$page.inc";
}

Now, just ensure that the $page is empty when no more pages are to be
processed!

Best wishes,
John
Oct 20 '08 #19

This discussion thread is closed

Replies have been disabled for this discussion.