简体   繁体   English

结束没有die()或exit()的php标签

[英]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是否有一个允许我在进入“?>”标记之前结束进程的函数,例如:

<?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. 我知道有些人认为这是无用的东西,但我想在iframe上使用验证脚本而不是使用dieexit函数,因为它会杀死整个脚本,我只想结束它的一部分。

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! 通常我使用if - else代替,但我想避免它们因为进程很大而且我想要更具可读性的东西,就像我使用的方式if - die在我的ajax脚本中if - die而且我想在我的iframe中使用这样的东西, 谢谢!


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). 好吧,我只是想知道PHP是否已经有一个适当的功能(似乎没有),所以我想我会留下if - elses,因为不值得使用更多的进程让它“更具可读性“(例如:尝试 - 捕获使用太多资源,我也不会去。) 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) 我的疑问仅仅是为了那个,我只会在我的ajax文件中使用die函数使用die函数(我不知道是否推荐它,但我认为没有问题,因为PHP因某些原因应该有它)

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(); 你把你的代码放在try块中,然后你结束抛出一个异常throw new Exception(); , and it exits only the rest of the code inside the try block. ,它只退出try块内的其余代码。

Your code would then be: 您的代码将是:

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

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: 我要把它扔到那里然后躲开,但如果你真的需要这样做,那么goto实际上并不是一个糟糕的选择:

<?php

    echo 'first';

    goto endofblock;

    echo 'second';

endofblock:

?>

Or you could avoid the "evil" of goto with a faux-loop. 或者你可以用一个虚循环来避免goto的“邪恶”。 To the compiler they basically look the same, but other programmers won't club you to death for using goto 对于编译器,它们看起来基本相同,但是其他程序员不会因为使用goto而致死

<?php
do {
    echo 'first';

    break;

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

Why do you not use switches and cases? 你为什么不使用开关和箱子?

If I were you, I would omit the end-php tag. 如果我是你,我会省略end-php标签。 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. 但除此之外,请继续使用if / else或switch / case控制结构。 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. 从内联代码中获取验证过程的大部分内容,如果您使用的是OOP,则将它们打包在函数或类方法中。

See also this note about GOTO ;-) http://xkcd.com/292/ 另见关于GOTO的这个说明;-) http://xkcd.com/292/

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. PHP解释器将文件作为一个单元使用, <?php标记只是指定要忽略的文本部分(就像标记一样),就像注释一样。 The tag does not border any portion of code. 标签不会与代码的任何部分相邻。 So in your case, I think the goto statement might be the right option. 所以在你的情况下,我认为goto语句可能是正确的选择。 Or, if you could put the code into a function , you could use return . 或者,如果您可以将代码放入function ,则可以使用return

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM