2013-03-04 94 views
0

我想將隨機swf變量回顯爲html,但我似乎無法得到正確的結果。如何在html中回顯變量(新手)

通過與螢火檢查我可以看到,它輸出我的PHP代碼的PHP回聲「$ randomImage」

我有什麼至今:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd"> 
<html> 
<head> 

<style media="screen" type="text/css"> 

.content { 
    position: absolute; 
    height: 200px; 
    width: 400px; 
    margin: -100px 0 0 -200px; 
    top: 25%; 
    left: 50%; 
} 

</style> 

</head> 

<body> 
<?php 

$imagesDir = '/'; 

$images = glob($imagesDir . '*.{swf}', GLOB_BRACE); 

$randomImage = $images[array_rand($images)]; 

?> 


<div class="content"> 
    <center><object width="675" height="517"><param value="<?php echo '$randomImage' ?>" name="movie"><embed width="475" height="317" type="application/x-shockwave-flash" src="<?php echo '$randomImage' ?>"></object></center><center></center> 
    </div> 
    </body> 
    </html> 
+0

' '$ randomImage''應該只是$ randomImage或者用's – Class 2013-03-04 01:08:53

+0

將你的單引號改爲雙引號 – 2013-03-04 01:09:21

+0

如果你ar e看到整個語句'<?php echo'$ randomImage'; ?>在HTML輸出中,這意味着你的PHP代碼沒有被解析。這是從Web服務器運行嗎? (如果你只看到''$ randomImage',那麼它就是已經提到的引用。 – 2013-03-04 01:09:54

回答

2

刪除引號。

<?php echo $randomImage; ?> 

PHP將其解釋爲字符串而不是變量$randomImage。你可以在引號中使用變量,但它們必須像雙引號一樣。

echo "$randomImage"; 

但是沒有理由這樣做,因爲您只需回顯變量即可。當您想要在不使用連接操作的情況下使用多個變量格式化字符串時,這非常有用。

echo "Hello, my name is $myName!, I am a $species."; 

這比替代方案更簡單和更易讀。

echo "Hello, my name is ", $myName ,"!, I am a ", $species ,"."; 
0

您:

  1. 需要用雙引號
  2. 可以擺脫完全

的qutes的通過將你的變量在單引號你告訴PHP你美元符號是從字面上理解的,而不是用來表示存在變量。

所以使用

<?php echo $randomImage ?> 

或者

<?php echo "$randomImage" ?> 
0

要正確迴應,你應該刪除引號'$randomImage' - >$randomImage或者你可以在 「」(雙引號)包起來"$randomImage"