2016-07-29 43 views
1

案例:PHP:返回相同,而裏面的值子雖然

嗨,社區#1。

其實我有looping(while)while while。這就是我所說的分而已。我想從while返回一些值,但是它看起來像是,值不能在一個子集中讀取。

實驗:

  1. 獲取代碼正常工作。一切都好,除了這$ x;
  2. 有沒有發現其他錯誤,但全球$ X,不能內部子被打印而

我嘗試使用全局函數聲明,而函數值。儘管如此,全局函數仍然無法在sub中讀取。該代碼是幾乎沒有這樣的:

while (fetch){ 
$x = value; // multi value, like A, B, C, D, E for example 
global $x; 
echo $x; // echo print still ok 

while (fetch){ 
echo "<select>". // i use select attribute in this case, it's plus a (.) dot 
// so code inside this sub while actually working but $x can't be printed 
echo $x; // not print anything 
echo "<option" . (($x==$throw[0]) ? 'selected="selected"': '') ."value=".$throw[0].">".$throw[0]."</option>'"; 
// i use this option attribute, and want $x to be inside the option attribute basically, because it needs to be compared 
"</select>"; 

} // end sub while 

} // end while 

所需的輸出:

我想在$ X返回值是它,就像在while循環。因爲$ x中的多值將與具有正確顯示的值的$ throw [0]進行比較,而$ x值甚至沒有顯示。

僅供參考,$ X包含例如幾個值,如:A,B,C,d,E

你有任何想法,這是爲什麼發生?我該怎麼做或修改?

謝謝你Stackoverflow社區。

+0

不應該有後選擇()點。回聲「」。$ x; //已更改。其實它打印$ x,但內部回聲

+0

你能告訴我你想達到的輸出嗎? – Arif

+0

[link](https://s31.postimg.org/3xmgalcu3/edit.png)就像那樣,如果$ x工作,它應該在select屬性上設置默認值。但事實證明,該類別沒有選中,這意味着$ x可能無法在那裏讀取。我使用這段代碼作爲參考[鏈接](http://stackoverflow.com/questions/38651772/html-php-set-selected-value-based-on-database-fetched-value) –