如何将PHP if语句添加到HTML字符串中
如果某些用户元数据的值为true,我想将checked属性(checked=“checked”)添加到输入中。但我不知道在HTML字符串中工作时使用正确的语法。以下是我当前的语法:如何将PHP if语句添加到HTML字符串中,php,Php,如果某些用户元数据的值为true,我想将checked属性(checked=“checked”)添加到输入中。但我不知道在HTML字符串中工作时使用正确的语法。以下是我当前的语法: $html = '' . '<h3>User Committees</h3>' . '<table class="form-table">' . '<tr>' . '<td>' .
$html = '' .
'<h3>User Committees</h3>' .
'<table class="form-table">' .
'<tr>' .
'<td>' .
'<label>' .
'<input type="checkbox" name="conference_&_sponsorship" value="1" ' . if ( get_user_meta($user->ID, 'conference_&_sponsorship', true) == 'true') { 'checked' } . '>' .
'Conference & Sponsorship' .
'</label>' .
'</td>' .
'</tr>' .
'</table>';
echo $html;
$html=''。
“用户委员会”。
'' .
'' .
'' .
'' .
'' .
“会议与赞助”。
'' .
'' .
'' .
'';
echo$html;
三元运算符将帮助您:
$html = '' .
'<label>' .
'<input type="checkbox" name="conference_&_sponsorship" value="1" ' . ( get_user_meta($user->ID, 'conference_&_sponsorship', true) == 'true' ? 'checked' : '' ) . '>' .
'Conference & Sponsorship' .
'</label>';
echo $html;
$html=''。
'' .
'' .
“会议与赞助”。
'';
echo$html;
您可以像这样将其分开:
$html = '' .
'<h3>User Committees</h3>' .
'<table class="form-table">' .
'<tr>' .
'<td>' .
'<label>' .
'<input type="checkbox" name="conference_&_sponsorship" value="1" ';
if ( get_user_meta($user->ID, 'conference_&_sponsorship', true) == 'true') { $html .= 'checked'; }
$html .= '>' .
'Conference & Sponsorship' .
'</label>' .
'</td>' .
'</tr>' .
'</table>';
echo $html;
$html=''。
“用户委员会”。
'' .
'' .
'' .
'' .
'' .
“会议与赞助”。
'' .
'' .
'' .
'';
echo$html;
这是PHP(不是Sparta),所以只要使用多行字符串,我就会看到你的代码:)
$checked=get\u user\u meta($user->ID,'conference\u&\u consornance',true)?'勾选“:”;
$html='1
用户委员会
到目前为止你试过什么?你被困在哪里了?按照@u_mulder的建议使用三元运算符。或者考虑使用一些模板引擎(比如SMARTY或拿铁)。请展示你试过的……我不知道你们两个在看什么,但是我清楚地把我的代码放在显示我尝试过的问题上。
$checked = get_user_meta($user->ID, 'conference_&_sponsorship', true) ? 'checked' : '';
$html = '
<h3>User Committees</h3>
<table class="form-table">
<tr>
<td>
<label>
<input type="checkbox" name="conference_&_sponsorship" value="1" '.$checked.'">
Conference & Sponsorship
</label>
</td>
</tr>
</table>
';
echo $html;