如何在PHP和HTML混合中添加if语句?
如果如何在PHP和HTML混合中添加if语句?,php,html,Php,Html,如果$\u GET['text'],我只想将数据prefilltext参数添加到按钮中“未定义”。如何做到这一点 HTML: id=“共享按钮” class=“g-interactivepost” data prefilltext=“如果您希望$\u GET['text']与字符串连接,则使用“未定义” 为了保持HTML的整洁,我可能会在实际的HTML按钮元素之外定义一个字符串,如下所示: <?php $prefilltext = isset($_GET['text']) ? 'data
$\u GET['text'],我只想将数据prefilltext
参数添加到按钮中“未定义”
。如何做到这一点
HTML:
id=“共享按钮”
class=“g-interactivepost”
data prefilltext=“如果您希望$\u GET['text']
与字符串连接,则使用“未定义”
为了保持HTML的整洁,我可能会在实际的HTML按钮元素之外定义一个字符串,如下所示:
<?php $prefilltext = isset($_GET['text']) ? 'data-prefilltext="' . $_GET['text'] . '"' : ''; ?>
<button id="sharebutton" class="g-interactivepost" <?php echo $prefilltext;?>>
Send
</button>
您可以执行以下操作:
<button
id="sharebutton"
class="g-interactivepost"
<?php echo ($_GET['text'] != 'undefined')? "data-prefilltext=\"".$_GET['text']."\" ": "";?>
Send
</button>
<button
id="sharebutton"
class="g-interactivepost"
<?php if (isset($_GET['text']) && !empty($_GET['text']) && $_GET['text'] !== 'undefined') { ?>
data-prefilltext="<?php echo $_GET['text'];?>"
<?php } ?>
>
Send
</button>
<?php $prefilltext = isset($_GET['text']) ? 'data-prefilltext="' . $_GET['text'] . '"' : ''; ?>
<button id="sharebutton" class="g-interactivepost" <?php echo $prefilltext;?>>
Send
</button>
<button
id="sharebutton"
class="g-interactivepost"
<?php echo ($_GET['text'] != 'undefined')? "data-prefilltext=\"".$_GET['text']."\" ": "";?>
Send
</button>
<button
id="sharebutton"
class="g-interactivepost"
data-prefilltext="<?php if(isset($_GET['text']) && !empty($_GET['text']) { if($_GET['text'] == "undefined") { echo $_GET['text']; } else { // if text is not equal to undefined }?>"
Send
</button>