所以我有一个if else语句,我当前可以在'user1','user2'等之间打印语句'对话,但不知道如何在if / else语句之外打印。
我想在PHP的那个部分之外,在生成变量之前的部分中访问它
这是我的PHP
<?php print $conversation_members; ?>
<?php
if (count($rows)) {
$uids= array();
while ($row = mysql_fetch_array($result1)) {
$uids[] = $row['userid'];
}
$last = array_pop($uids);
$conversation_members = '<p>Conversation between ';
$conversation_members .= implode(', ', $uids) . ' and ' . $last;
$conversation_members .= '.</p>';
}
else {
blah blah irrelevant
}
?>
真诚感谢您的帮助!非常感谢 -
答案 0 :(得分:1)
将其存储在变量中:
$output = ''; // Give a default value
if (count($rows)) {
$uids= array();
while ($row = mysql_fetch_array($result1)) {
$uids[] = $row['userid'];
}
$last = array_pop($uids);
$output = '<p>Conversation between ';
$output .= implode(', ', $uids) . ' and ' . $last;
$ouptut .= '.</p>';
}
else {
blah blah irrelevant
}
echo $output;
答案 1 :(得分:0)
只需将您正在打印的文本设置为变量,并将.=
设置为正在打印的其余部分。然后,您可以在脚本中的任何其他位置将其用作变量。
$ thisVar =&#39;&#39;;
然后在你的if else:
$ thisVar。=&#34;会话等等等等#34 ;;
$ thisVar。=&#34;四个4&#34 ;;
echo $ thisVar; //将打印&#34;对话等等等等4&#34;
答案 2 :(得分:0)
$str = '';
if (count($rows)) {
$uids= array();
while ($row = mysql_fetch_array($result1)) {
$uids[] = $row['userid'];
}
$last = array_pop($uids);
$str .= '<p>Conversation between ';
$str .= implode(', ', $uids) . ' and ' . $last;
$str .= '.</p>';
echo $str;
}
else {
echo $str;
}
答案 3 :(得分:0)
创建一个返回所需内容的函数:
function getConversationMembers($result)
{
$uids = array();
while ($row = mysql_fetch_array($result)) {
$uids[] = $row['userid'];
}
if (count($uids) >= 2) {
$last = array_pop($uids);
return sprintf('<p>Conversation between %s and %s.</p>',
htmlspecialchars(join(',', $uids), ENT_QUOTES, 'UTF-8'),
htmlspecialchars($last, ENT_QUOTES, 'UTF-8')
);
} else {
return '';
}
}
然后:
<?php print getConversationMembers($result1); ?>