Neo
Neo

Reputation: 395

End a php tag without die() or exit()

I was wondering if PHP has a function that allows me to end a process before it reaches the "?>" tag, example:

<?php

    echo 'first';

    endphptag();

    echo 'second';

?>

third

<?php

    echo 'fourth';

?>

Then the output should be:

first

third

fourth

I know that some people consider this as something useless, but I want to do it for a validation script on an iframe instead of use the die or exit function because it kills the whole script, I just want to end a part of it.

Normally I use if - else instead, but I want to avoid them because the processes are large and I want something more readable, by the way I use if - die in my ajax scripts and I want to use something like this in my iframes too, Thank's!


Well, I just wanted to know if PHP already had a proper function for it (it seems not), so I think I will just leave it with if - elses, because is not really worth to use more process for make it "more readable" (ex: try - catches uses too much resources, I'm not going to go-tos neither). My doubt was only for that, I will only use this procesdure in my ajax files using the die function (I don't know if it is recommended, but I think there's no problem because PHP should have it for some reason)

Upvotes: 6

Views: 1385

Answers (5)

Melebius
Melebius

Reputation: 6695

The PHP interpreter works with the file as a single unit, the <?php tag just specifies portions of text (outside the tag) to ignore, just like comments. The tag does not border any portion of code. So in your case, I think the goto statement might be the right option. Or, if you could put the code into a function, you could use return.

Upvotes: 0

Eric Petroelje
Eric Petroelje

Reputation: 60518

I'm going to throw this out there and duck, but if you really need to do this, then goto is actually not a bad option:

<?php

    echo 'first';

    goto endofblock;

    echo 'second';

endofblock:

?>

Or you could avoid the "evil" of goto with a faux-loop. To the compiler they basically look the same, but other programmers won't club you to death for using goto

<?php
do {
    echo 'first';

    break;

    echo 'second';
} while (false)        
?>

Upvotes: 4

Ray Paseur
Ray Paseur

Reputation: 2194

If I were you, I would omit the end-php tag. Most coding standards advise against it, for reasons that are too lengthy to explain here.

But that aside, go ahead and use the if/else or the switch/case control structures. Take the large parts of the validation process out of the inline code and package them in functions or class methods if you're using OOP.

See also this note about GOTO ;-) http://xkcd.com/292/

Upvotes: 0

Shoe
Shoe

Reputation: 76270

Exceptions is what you are looking for:

try {
    // code
} catch (Exception $e) {
    // handling
}

You put your code inside the try block and you end it throwing an exception with throw new Exception();, and it exits only the rest of the code inside the try block.

Your code would then be:

<?php
    try {
        echo 'first';
        throw new Exception();
        echo 'second';
    } catch (Exception $e) {}
?>
third
<?php
    echo 'fourth';
?>

Upvotes: 4

Blazer
Blazer

Reputation: 14277

Why do you not use switches and cases?

Upvotes: 1

Related Questions