Is there any way to do this? (skipping code)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wonderoid
Forum Newbie
Posts: 6
Joined: Tue Oct 13, 2009 2:36 pm

Is there any way to do this? (skipping code)

Post by wonderoid »

Hello, a php newbie here.

Anyway; here is my question: I want to skip over some html + php code. Is it possible? (elegantly or hack)

Here is an example:

Code: Select all

<?php skip() ?>
<div> some html </div>
<div> <?php echo $var ?></div>
<?php resume() ?>
Here I want to skip between the skip() function and resume(). However using output buffering and discarding the contents is not a solution, $var might be unset and there may be an error. I want that part completely discarded (or not interpreted as php so I can just buffer and discard it).

Thanks!
wonderoid
Forum Newbie
Posts: 6
Joined: Tue Oct 13, 2009 2:36 pm

Re: Is there any way to do this? (skipping code)

Post by wonderoid »

Thanks for the reply but goto doesn't seem to jump out of blocks. So it is not possible to jump from inside the skip() function. I want to achieve it by using those 2 functions (if possible ofc, if not i'll try something else).
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Is there any way to do this? (skipping code)

Post by Mirge »

You could just put the actual condition for skipping/resuming within your page... or return true/false from the skip() function to say whether to skip or not and then eliminate the resume() function call.

For example:

Code: Select all

 
<html>
<head><title>Foo</title></head>
<body>
One
Two
Three
<?php if(!skip()) { ?>
Four
Five
<?php } /* end if statement */ ?>
Six
Seven
Eight
</body>
</html>
 
EDIT: Please do not ever use goto.
User avatar
desperado
Forum Commoner
Posts: 46
Joined: Wed Dec 10, 2008 8:49 am

Re: Is there any way to do this? (skipping code)

Post by desperado »

well I can't actually use goto myself, I'm running 5.2.8, but it takes me back to my good old basic days...

...does anyone remember basic?

...anyone?

ok, what he said.... :mrgreen:
wonderoid
Forum Newbie
Posts: 6
Joined: Tue Oct 13, 2009 2:36 pm

Re: Is there any way to do this? (skipping code)

Post by wonderoid »

Mirge wrote:You could just put the actual condition for skipping/resuming within your page... or return true/false from the skip() function to say whether to skip or not and then eliminate the resume() function call.

For example:

Code: Select all

 
<html>
<head><title>Foo</title></head>
<body>
One
Two
Three
<?php if(!skip()) { ?>
Four
Five
<?php } /* end if statement */ ?>
Six
Seven
Eight
</body>
</html>
 
EDIT: Please do not ever use goto.
Thanks for the reply Mirge, I think I was not clear on the first message. Of course there are many ways to jump over that part of the code. What I want to do is marking a point as the start and just skip anything until the end marker is found. Think of it like a markup or template syntax. Like ob_start() - end() pair (with the difference that the content will not be executed.)
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Is there any way to do this? (skipping code)

Post by JNettles »

Is there some reason you can't just block off sections with an IF statement?
User avatar
desperado
Forum Commoner
Posts: 46
Joined: Wed Dec 10, 2008 8:49 am

Re: Is there any way to do this? (skipping code)

Post by desperado »

I don't know if this helps, but i've done something like below recently:

Code: Select all

<?php ob_start(); ?>
BLUE
<?php 
$printoutput1 = ob_get_contents();
ob_end_clean();
ob_start();
?>
SKY
<?php 
$printoutput2 = ob_get_contents();
ob_end_clean(); 
$printview = $printoutput1 . $printoutput2;
echo $printoutput1;
?>
 GARBAGE
 <?php echo $printoutput2; ?>
$printview only contains what I need.
wonderoid
Forum Newbie
Posts: 6
Joined: Tue Oct 13, 2009 2:36 pm

Re: Is there any way to do this? (skipping code)

Post by wonderoid »

desperado wrote:I don't know if this helps, but i've done something like below recently:

Code: Select all

<?php ob_start(); ?>
BLUE
<?php 
$printoutput1 = ob_get_contents();
ob_end_clean();
ob_start();
?>
SKY
<?php 
$printoutput2 = ob_get_contents();
ob_end_clean(); 
$printview = $printoutput1 . $printoutput2;
echo $printoutput1;
?>
 GARBAGE
 <?php echo $printoutput2; ?>
$printview only contains what I need.
Output buffering doesn't work for me though, as it executes the code(renders to the buffer). What I was looking for was skipping, which probably doesn't exist unless I use goto in a suitable scope.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Is there any way to do this? (skipping code)

Post by Mirge »

wonderoid wrote:
Mirge wrote:You could just put the actual condition for skipping/resuming within your page... or return true/false from the skip() function to say whether to skip or not and then eliminate the resume() function call.

For example:

Code: Select all

 
<html>
<head><title>Foo</title></head>
<body>
One
Two
Three
<?php if(!skip()) { ?>
Four
Five
<?php } /* end if statement */ ?>
Six
Seven
Eight
</body>
</html>
 
EDIT: Please do not ever use goto.
Thanks for the reply Mirge, I think I was not clear on the first message. Of course there are many ways to jump over that part of the code. What I want to do is marking a point as the start and just skip anything until the end marker is found. Think of it like a markup or template syntax. Like ob_start() - end() pair (with the difference that the content will not be executed.)
So... are you trying to use custom markers & parse the code yourself? I don't see why you can't just use an if() statement.

I repeat, do not use goto... it will only pollute your code and make it unreadable, and more difficult to maintain.. and debug.

Or, if you're just trying to skip it altogether, always...

Code: Select all

 
<html>
<head><title>Foo</title></head>
<body>
One
Two
Three
<?php /*
Four
Five
*/ ?>
Six
Seven
Eight
</body>
</html>
 
User avatar
desperado
Forum Commoner
Posts: 46
Joined: Wed Dec 10, 2008 8:49 am

Re: Is there any way to do this? (skipping code)

Post by desperado »

well, not exactly. the code I pasted skips the GARBAGE part.

the php will be rendered no matter what when it's called.

Perhaps you should explain what you are trying to achieve, as there is certainly a way to do it.
wonderoid
Forum Newbie
Posts: 6
Joined: Tue Oct 13, 2009 2:36 pm

Re: Is there any way to do this? (skipping code)

Post by wonderoid »

desperado wrote:well, not exactly. the code I pasted skips the GARBAGE part.

the php will be rendered no matter what when it's called.

Perhaps you should explain what you are trying to achieve, as there is certainly a way to do it.
Ok; I am used to developing in Django, and what I was trying to achieve was simple template inheritance. In Django's template inheritance; you can mark a block of template code-html and give it a name, which you can on another template override or extend (like methods). Now I know there are such php frameworks too but it is done by parsing the page(as a text file). I was curious if I could make one without, but either my skills aren't really up to that point or there isn'T a way. What I was trying to achieve was for example; making a block by calling startBlock('name') - endblock(). Now when you extend this template, any code within the block is invalid in the new scope, you might have a variable there that exists in the base page but not in the extended one etc.
Post Reply