打印while循环(作用域)php上面的变量

打印while循环(作用域)php上面的变量,php,while-loop,scope,Php,While Loop,Scope,有没有办法在while循环下定义变量并在while循环上打印 像这样的 print $catName while ($b = mysqli_fetch_assoc($results)) { $catName = $b['cat']; } 编辑 $getBooks = $db->prepare('SELECT id, cat, instituteId, title, cover, authorName FROM books_library WHERE instituteId=? A

有没有办法在
while循环下定义变量并在
while循环上打印

像这样的

print $catName
while ($b = mysqli_fetch_assoc($results)) {
$catName = $b['cat'];
}
编辑

$getBooks = $db->prepare('SELECT
    id, cat, instituteId, title, cover, authorName FROM books_library WHERE instituteId=? AND cat=?');
$getList = array();
$getBooks->bind_param('ii', $inId, $cid);
if ($getBooks->execute()) {
    $libResults = $getBooks->get_result();
    $getList[] = $libResults;
    ?>
HTML代码到这里来

<h3 class="marginBottom8px margin-top_30px text-bold text-danger"><?php echo catName ?></h3>

现在在
while循环下
我得到了
$catName
我如何

中回送它上面的
我想根据编辑的问题,这就是你想要的

while ($book = mysqli_fetch_assoc($libResults)) {
    echo '<h3>' . $book['cat'] . '</h3>';
}
while($book=mysqli\u fetch\u assoc($libResults)){
回音“.$book['cat']”;
}

在while块之后,将
h3
html放入变量中并打印它

$myHtml = '<h3 class="marginBottom8px margin-top_30px text-bold text-danger">' 
    . $catName . '</h3>';
print $myHtml;
$myHtml=''
. $猫名;
打印$myHtml;

不要像那样混合html和php。

你不能重复一个尚未定义的变量,就像试图喂养一个尚未怀孕的婴儿。。。。。在这里解释你想要实现什么,而不是你想要如何实现。。。。。ie为什么在读取数据库查询结果之前需要回显数据库查询中的值。。。。为什么不能简单地在循环中显示它?为什么需要这样做?也许你想要实现的另一个选择是有序的。给我一点时间,我将编辑我的问题,要么将标记放在while循环中,要么放在它之后,而不是之前。。。。在定义变量之前,不能回显变量,但可以将回显移动到定义变量之后的某个点。答案不应向下。因为问题是@#@#@$@$,如果你的意思是将$myHtml放在循环之前。。。这行不通$创建字符串时,catName将为空。
$myHtml = '<h3 class="marginBottom8px margin-top_30px text-bold text-danger">' 
    . $catName . '</h3>';
print $myHtml;