2013-01-22 66 views
0

可能重複:
How to get useful error messages in PHP?使用「和」PHP語法

香港專業教育學院開始了我的新年計劃的一部分,並決定學習PHP的,因爲它的一部分我試着去解析一個xml提要,並且回顯包裹在<a>標籤中的事件的名稱,將它們鏈接回xml飼料網站上的事件頁面

我覺得我得到了一切在,但我不能似乎明白爲什麼這個心不是工作我只是得到一個空白頁,如果有一個人能在正確的方向,將不勝感激指向我,歡呼聲

<?php 
    // F1 W/H xml feed 
    $xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N'); 

    foreach ($xml->response->williamhill->class->type as $type) { 
    $type_attrib = $type->attributes(); 
    echo "<h2>".$type_attrib['name']."</h2>"; //Title - in this case f1 championship 
} ?> 


<ul> 
    <?php 
     foreach($type->market as $event) { 
      echo "<li>"; 
      echo "<a href="$event_attributes['url']">"; 
      echo $event_attributes['name']; 
      echo "</a>"; 
      echo "</li>"; 
     } 
    ?> 
</ul> 
+2

一個空白的PHP頁面通常意味着一個錯誤。檢查錯誤日誌或開啓['display_errors'](http://php.net/manual/en/errorfunc.configuration.php#ini.display-errors) –

+1

在php.ini/.htaccess中啓用'error_reporting'/.user.ini(在調用腳本中語法錯誤太晚)。 – mario

+0

你的'$ event_attributes'數組是如何分析的? – hjpotter92

回答

4
echo "<a href="$event_attributes['url']">"; 

嘗試改變該行

echo "<a href=\"".$event_attributes['url']."\">"; 

PHP解析器是非常有趣的關於這一點。通常你會選擇一個,只要堅持下去,或者隨便使用單引號和雙引號。只要記住帶有雙引號的字符串就會被解析爲變量。

$hello = "Hello"; 
echo "$hello master"; 

相同

$hello ="Hello"; 
echo $hello.' master'; 
+0

我想你甚至可以說'「」;' – styfle

+0

如果你有一個非挑剔的警告政策是應該可以的:-) –

0

變化

echo "<a href="$event_attributes['url']">"; 

對於

echo "<a href=".$event_attributes['url'].">"; 
0

你缺少你的第二echo期間,你有你$event_attributes['url']

<?php 
    foreach($type->market as $event) { 
     echo "<li>"; 
     echo "<a href=".$event_attributes['url'].">"; 
     echo $event_attributes['name']; 
     echo "</a>"; 
     echo "</li>"; 
    } 

?>

我會建議您啓用錯誤日誌,它將允許你知道在你的腳本的問題就行了。

1

當你測試你的PHP腳本,你會發現它有用的錯誤切換 - 那麼PHP實際上將告訴你爲什麼它沒有顯示你什麼:

error_reporting(E_ALL); 

通常你會錯過一個;或者打錯一個變量名。

你的情況

的錯誤是在這裏:

echo "<a href="$event_attributes['url']">"; 

你意外結束了與雙引號串,所以PHP認爲字符串結尾的位置:

echo "<a href=" 

這是使用單 - 引用可以非常方便,因爲您的雙引號不會關閉字符串。

echo '<a href="' . $event_attributes['url'] . '">'; 

PHP中單引號和雙引號的主要區別在於雙引號具有特殊的巧妙解析規則,而單引號沒有。例如:

$myVar = "BLAH"; 
echo "Example $myVar"; // Example BLAH 
echo 'Example $myVar'; // Example $myVar 
1

在你的無序列表,你應該用一個點來連接你的字符串,並逃避你的雙引號是這樣的:

echo "<a href=\"".$event_attributes['url']."\">"; 

而不是

echo "<a href="$event_attributes['url']">"; 

你示例拋出和錯誤,因爲你沒有使用正確的字符串連接。但是,即使使用正確的concat,它也會呈現爲<a href=http://someurl>,並且您需要根據html標準添加雙引號。因此你必須雙引號。如果你想不具有使用'"話,我建議使用PHP語法php alternative syntax

與給定的代碼之間切換的困擾它看起來像

1

<?php 
// F1 W/H xml feed 
$xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N'); 

foreach ($xml->response->williamhill->class->type as $type) { 
    $type_attrib = $type->attributes(); 
    echo "<h2>".$type_attrib['name']."</h2>"; //Title - in this case f1 championship 
} ?> 

<ul> 
    <?php foreach($type->market as $event):?> 
     <li> 
      <a href="<?php echo $event_attributes['url']; ?>"> 
       <?php echo $event_attributes['name']; ?> 
      </a> 
     </li> 
    <? endforeach;?> 
</ul> 

這將帶來的一個好處是,它會產生更乾淨的代碼,因爲你可以清楚地從html中清除php代碼,這是代表所有其他<?php ?>以及其他人的價格的表示部分會聲稱性能下降。選擇是你的