简体   繁体   English

PHP转到替代方案

[英]PHP goto alternative

I've just recently started using PHP 5.4 and have noticed as of 5.3 you can use goto to jump to sections of code which I am using to jump from a loop section. 我刚刚开始使用PHP 5.4并且注意到从5.3开始你可以使用goto跳转到我用来从循环部分跳转的代码部分。 My question is after having read this post... Is GOTO in PHP evil? 我的问题是在阅读了这篇文章后... GOTO在PHP中是邪恶的吗? is this bad practice in this case or is it a viable solution? 在这种情况下这是不好的做法还是可行的解决方案?

<?php
while ($thisResult = mysql_fetch_array($result)) {      
    if($article && $i > 0) {
        goto comments;
    }

?>
    <h2>
        <a href="/plugins/<?=$thisResult['post_name']?>"><?=$thisResult['post_title']?></a>
        <span><?=$thisResult['post_modified_gmt']?></span>
    </h2>
    <p class="content">
        <?=nl2br($thisResult['post_content']);?>
    </p>
    <br />  
 <?php
    comments: 
        if ($article) {
?>
        <p class="comment"><?=$thisResult['comment_content']?>
<?php
        }

    $i++;
}

 ?>

This is called spaghetti programming and is a bad practice. 这被称为意大利面条编程,是一种不好的做法。 http://en.wikipedia.org/wiki/Spaghetti_code http://en.wikipedia.org/wiki/Spaghetti_code

here is what you can do instead for your code 这是你可以为你的代码做的事情

    <?php
    while ($thisResult = mysql_fetch_array($result)) {      
        if($article && $i > 0) {

        }
    else {
    ?>
        <h2>
            <a href="/plugins/<?php $thisResult['post_name']?>"><?php $thisResult['post_title']?></a>
            <span><?php $thisResult['post_modified_gmt']?></span>
        </h2>
        <p class="content">
            <?php nl2br($thisResult['post_content']);?>
        </p>
        <br />  
     <?php
    }
            if ($article) {
    ?>
            <p class="comment"><?php $thisResult['comment_content']?>
    <?php
            }

        $i++;
    }

     ?>

A simple else solves it. 一个简单的解决方案。 you can probably find more elegant solutions using switch cases or flags. 您可以使用开关案例或标志找到更优雅的解决方案。

The problem is that your code will be hard to edit and add stuff to it. 问题是您的代码很难编辑并添加内容。

This is rewrite of the same code without using goto , to show you that you can always make it without it 这是在不使用goto情况下重写相同的代码,向您展示您可以在没有它的情况下完成它

<?php
    while ($thisResult = mysql_fetch_array($result)):
        if(!$article || $i <= 0): ?>
            <h2>
                <a href="/plugins/<?php echo $thisResult['post_name']; ?>"><?php echo $thisResult['post_title']; ?></a>
                <span><?php echo $thisResult['post_modified_gmt']; ?></span>
            </h2>
            <p class="content">
                <?php echo nl2br($thisResult['post_content']); ?>
            </p>
            <br />
        <?php endif; ?>
        <?php if ($article): ?>
            <p class="comment"><?php echo $thisResult['comment_content']; ?></p>
        <?php endif;
        $i++;
    endwhile;

I used alternative syntax for control structures, and replaced <?= with <?php echo as it's much more readable to me. 我使用了替代语法来控制结构,并将<?=替换为<?php echo因为它对我来说更具可读性。 Also, other commenters gave you some good suggestions regarding separation of markup and db functions etc, so please give it a thought 另外,其他评论者给了你一些关于标记和数据库功能分离的好建议,所以请你仔细考虑一下

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

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