将此表达式放在这组引号中的最佳方法是什么?

时间:2012-08-22 04:31:22

标签: php quotes ternary-operator

放置此表达式的最佳方法是什么:

echo isset($GLOBALS['_url']) ? htmlspecialchars($GLOBALS['_url']) : ''

在此参数内:

<?php
    echo "
    <input type='text' value=' *INSERT EXPRESSION* ' />
    ";
?>

我不确定在报价中处理报价的最佳方法是什么,所以任何帮助都表示赞赏。我知道可以通过改变整体语法来避免这种情况,但是,考虑到这些限制,我怎样才能做到最好呢?谢谢你的帮助!

5 个答案:

答案 0 :(得分:2)

最简单的方法......

<?php
    $exp = isset($GLOBALS['_url']) ? htmlspecialchars($GLOBALS['_url']) : '';

    echo "<input type='text' value=' $exp ' />";
?>

答案 1 :(得分:0)

沿着这些方向可能有什么?

<?php
    echo "
    <input type='text' value='" . 
        (isset($GLOBALS['_url']) ? htmlspecialchars($GLOBALS['_url']) : '') . 
    "' />";
?>

答案 2 :(得分:0)

以下是一些方法:

方法1:

<?php
    $expression = isset($GLOBALS['_url']) ? htmlspecialchars($GLOBALS['_url']) : '';

    echo "
    <input type='text' value='$expression' />
    ";
?>

方法2:

<?php
    $expression = isset($GLOBALS['_url']) ? htmlspecialchars($GLOBALS['_url']) : '';

    echo "
    <input type='text' value='" . $expression . "' />
    ";
?>

方法3:

<?php
    echo "
    <input type='text' value='" . isset($GLOBALS['_url']) ? htmlspecialchars($GLOBALS['_url']) : '' . "' />
    ";
?>

<强>更新
方法4:我会使用方法4或5,因为PHP处理起来更快。这里的变化是我使用单引号而不是双引号。

<?php
    echo '
    <input type="text" value="' . isset($GLOBALS['_url']) ? htmlspecialchars($GLOBALS['_url']) : '' . '" />
    ';
?>

方法5:

<?php
    $expression = isset($GLOBALS['_url']) ? htmlspecialchars($GLOBALS['_url']) : '';

    echo '
    <input type="text" value="' . $expression . '" />
    ';
?>

答案 3 :(得分:0)

我总是使用printf()

<?php

    printf("\n<input type='text' value='%s' />\n", isset($GLOBALS['_url']) ? htmlspecialchars($GLOBALS['_url']) : '');

?>

答案 4 :(得分:0)

试试这个,

<?php
    define('URL',isset($GLOBALS['_url']) ? htmlspecialchars($GLOBALS['_url']) : '');
    echo "<input type='text' value=' ".URL." ' />";
?>