美化PHP和HTML语句的组合

问题描述 投票:-2回答:2

我想在一个外部php标签中写这一行。也许我们可以使用带有回显的echo语句?或者有人可以使用三元语句来美化它?

<?php if ( condition = true ) { ?>

  <p ><label for="user_limit" > User - Limit(&euro;)</label ><input type = "text" name = "user_limit" id = "user_limit" size = "40" value = "<?php echo inout($res['user_limit']);?>" /></p >

<?php } ?>
php ternary
2个回答
2
投票

就我个人而言,我不会缩短您的代码,因为这会使它的可读性降低,只有我要更改的是使用syntactic sugar,以便更容易理解if语句在哪里停止,如下所示:

<?php if ( condition = true ): ?>

  <p ><label for="user_limit" > User - Limit(&euro;)</label ><input type = "text" name = "user_limit" id = "user_limit" size = "40" value = "<?php echo inout($res['user_limit']);?>" /></p >

<?php endif; ?>

我认为结合使用html和php时,使用if / endif组合更易读。

但是,如果您想使其更紧凑,以易读性为代价,可以使用这种衬板:

<?= ($condition) ? '<p ><label for="user_limit" > User - Limit(&euro;)</label ><input type = "text" name = "user_limit" id = "user_limit" size = "40" value = "' . $someVal . '" /></p>' : ''; ?>

说明:

// <?= is called short echo tag and it equals <?php echo
// ?: ternary operator is shorthand for if/else statement

1
投票

是的,您可以使用回声

<?php 
if ( $condition == true ) { 

    echo "<p>
            <label for='user_limit'> User - Limit(&euro;)</label>
            <input type='text' name='user_limit' id='user_limit' size='40' value='" 
                . inout($res['user_limit']) . "' />
          </p>";
}

注意:还删除了HTML中的许多不必要的空格,以使其保持整洁

© www.soinside.com 2019 - 2024. All rights reserved.