php
  • if-statement
  • 2010-06-26 56 views 2 likes 
    2

    我正在通過國家特定的代碼過濾我的網站上的一些內容,我試圖添加其他語句,因此它不必將每件作爲單獨的代碼運行,除非我嘗試給出錯誤:PHP意外elseif

    <?php if (function_exists('isCountryInFilter')) { ?> 
    <?php if(isCountryInFilter(array("us", "ca"))) { ?> 
    <a rel="nofollow" class='preloading gallery_image' href="#" target="_blank"> 
    <?php } ?> 
    
    <?php elseif(isCountryInFilter(array("au"))) { ?> 
    <a rel="nofollow" class='preloading gallery_image' href="#" target="_blank"> 
    <?php } ?> 
    
    <?php else(isCountryInFilter(array("nz"))) { ?> 
    <a rel="nofollow" class='preloading gallery_image' href="#" target="_blank"> 
    <?php }} ?> 
    

    上面提供了以下錯誤:Parse error: syntax error, unexpected T_ELSEIF in,指的是第一ELSEIF而不是格式的

    +0

    你應該真的學會如何從你的視圖/輸出中分離邏輯。反正,檢查我的回答 – RobertPitt 2010-06-26 16:57:55

    回答

    2

    甚至更​​好用的switch-case了許多條件。 例如:

    switch($x) { 
        case 1: 
        ?> some html <?php 
        break; 
        case 2: 
        ?> more html <?php 
        break; 
    } 
    

    ,而不是關閉的標籤,你可以回聲/打印的HTML。

    這將工作:

    <?php if (function_exists('isCountryInFilter')) { 
    if(isCountryInFilter(array("us", "ca"))) { 
    ?> 
    <a rel="nofollow" class='preloading gallery_image' href="#" target="_blank"> 
    <?php } elseif(isCountryInFilter(array("au"))) { ?> 
    <a rel="nofollow" class='preloading gallery_image' href="#" target="_blank"> 
    <?php } elseif(isCountryInFilter(array("nz"))) { ?> 
    <a rel="nofollow" class='preloading gallery_image' href="#" target="_blank"> 
    <?php } 
    } 
    ?> 
    
    +0

    欣賞所有答案我發現這個建議以及DamienL的答案解決了我的錯誤。 – illtemper 2010-06-26 17:15:03

    2

    使用

    <?php if($foo == 1): ?> 
        html code 
    <?php else if($foo == 2): ?> 
        other html 
    <?php else: ?> 
        more html code 
    <?php end; ?> 
    
    +0

    爲什麼這種格式? – Sarfraz 2010-06-26 16:33:56

    +0

    @sAc:這種風格被許多人喜歡這種類型的編碼(我仍然認爲意大利麪條的味道),因爲它消除了花括號的需要。由於花括號是導致OP絆倒的原因,這是一個公平的建議。 – grossvogel 2010-06-26 16:41:07

    +0

    這就是你將php邏輯與html結合後得到的結果,這與他上面使用的方法沒有什麼不同。 – RobertPitt 2010-06-26 16:56:42

    3

    否則不能評價AC另外,如果所有其他條件都是假的,它將被執行,根據交換機的默認語句來考慮。

    此:

    <?php else(isCountryInFilter(array("nz"))) { ?> 
        <a rel="nofollow" class='preloading gallery_image' href="#" target="_blank"> 
        <?php }} ?> 
    

    需求是這樣的:

    <?php else if(isCountryInFilter(array("nz"))) { ?> 
    <a rel="nofollow" class='preloading gallery_image' href="#" target="_blank"> 
    <?php }} ?> 
    
    +0

    非常真實,但OPs描述使得它聽起來像解析器甚至沒有使它變得如此... – grossvogel 2010-06-26 16:30:00

    1

    FrEaKmAn的回答可能會工作,但我更喜歡花括號自己。嘗試改變

    <?php } ?> 
    <?php elseif(isCountryInFilter(array("au"))) { ?> 
    

    <?php } elseif(isCountryInFilter(array("au"))) { ?> 
    

    而且,在一般情況下,這是很好的減少你打破進出PHP塊的次數......

    (一旦這塊固定,您需要處理DamienL指出的問題。)

    +0

    這個答案有什麼問題? – grossvogel 2010-06-26 16:57:15

    3

    正如您將PHP與直接HTML/OUTPUT結合在一起。在您的代碼期間,您將在結尾括號和elseif關鍵字之間打印空白。

    PHP的解釋器直接在} elseif關鍵字後面查找,但它找到的是一個輸出數據塊,因此會引發錯誤。

    <?php if (function_exists('isCountryInFilter')) { ?> 
    <?php if(isCountryInFilter(array("us", "ca"))) { ?> 
    <a rel="nofollow" class='preloading gallery_image' href="#" target="_blank"> 
    <?php } ?> 
    <!--PHP Finds space here which it does not expect.--> 
    <?php elseif(isCountryInFilter(array("au"))) { ?> 
    <a rel="nofollow" class='preloading gallery_image' href="#" target="_blank"> 
    <?php } ?> 
    <!--PHP Finds space here which it does not expect.--> 
    <?php elseif(isCountryInFilter(array("nz"))) { ?> 
    <a rel="nofollow" class='preloading gallery_image' href="#" target="_blank"> 
    <?php }} ?> 
    

    你需要做的是刪除像這樣的空白。

    <?php if (function_exists('isCountryInFilter')) { ?> 
        <?php if(isCountryInFilter(array("us", "ca"))) { ?> 
        <a rel="nofollow" class='preloading gallery_image' href="#" target="_blank"> 
        <?php }elseif(isCountryInFilter(array("au"))) { ?> 
        <a rel="nofollow" class='preloading gallery_image' href="#" target="_blank"> 
        <?php }else(isCountryInFilter(array("nz"))) { ?> 
        <a rel="nofollow" class='preloading gallery_image' href="#" target="_blank"> 
    <?php}}?> 
    

    您會注意到PHP塊之外的空間不足。

    這應該可以解決您的問題。

    +0

    +1,因爲這是問題!空白輸出! – alexanderpas 2010-06-26 17:06:10

    +0

    不僅。 else(isCountryInFilter(array(「nz」)))錯誤。否則將運行,如果所有的陳述是假的,它不能包含任何條件。它會給你一個錯誤。 結局如何:它也會給你一個錯誤,因爲<?php和}之間沒有空格。 – 2010-06-26 17:08:40

    +0

    這是真的,但我集中我的手頭上的錯誤的評論,一旦他用我的幫助修復錯誤,他會自己處理第二個問題。但是,你是對的,他需要把括號中的else執行包起來。 – RobertPitt 2010-06-27 10:14:37

    相關問題