第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

將文本與變量中的變量連接起來?

將文本與變量中的變量連接起來?

PHP
翻翻過去那場雪 2021-07-01 13:12:14
我正在嘗試使用循環(huán)創(chuàng)建一小段 HTML 代碼。在循環(huán)中,我想獲取$start_intro_headline_X下面簡化代碼中的每個(gè)文本值。我該如何解決這個(gè)問題?$start_intro_headline_0 = "This is the first headline";$start_intro_headline_1 = "This is the second headline";$intro_sections ="";for ($x = 0; $x <= 4; $x++) {$intro_sections .= "<h2>{$start_intro_headline_{$x}}</h2>"; <-- THIS LINE IS THE PROBLEM!} $pageContent = <<<EOD$intro_sectionsEOD;
查看完整描述

2 回答

?
幕布斯6054654

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超7個(gè)贊

您需要將變量的名稱作為字符串分配給另一個(gè)變量,然后將其用作變量變量。我知道這聽起來令人困惑,所以只需查看代碼:) 以下是代碼解釋:


$start_intro_headline_0 = "This is the first headline";

$start_intro_headline_1 = "This is the second headline";


$intro_sections ="";


for ($x = 0; $x <= 1; $x++) {

  $var = 'start_intro_headline_'.$x;

  $intro_sections .= "<h2>{$$var}</h2>"; // Note that it's $$var, not $var


echo $intro_sections;

該echo會(huì)產(chǎn)生<h2>This is the first headline</h2><h2>This is the second headline</h2>


查看完整回答
反對(duì) 回復(fù) 2021-07-09
?
肥皂起泡泡

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超6個(gè)贊

雖然我認(rèn)為另一個(gè)答案有效,但避免動(dòng)態(tài)變量會(huì)更安全。使用數(shù)組來保存您的值:


$start_intro_headline = [

  "This is the first headline",

  "This is the second headline"

];


$intro_sections ="";

$total = count($start_intro_headline);

for ($x = 0; $x < $total; $x++) {

  $intro_sections .= "<h2>{$start_intro_headline[$x]}</h2>";


echo $intro_sections;

這樣您以后就不必創(chuàng)建新變量,只需向數(shù)組添加新值即可。下面是使用動(dòng)態(tài)變量可能會(huì)出錯(cuò)的示例。


查看完整回答
反對(duì) 回復(fù) 2021-07-09
  • 2 回答
  • 0 關(guān)注
  • 196 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)