if / else if语法

时间:2011-06-13 21:10:18

标签: php

我在使用以下代码时遇到了一些问题,很可能是n00b错误,如果答案很明显,请提前道歉。 if / elseif / if在没有内容但没有测试语句的情况下工作。我缺少一些语法的细微之处。

<?php 

if (stripos($_SERVER['REQUEST_URI'],'/workshops/') !== false) {echo ''

}; elseif ($ticketlink = get_post_meta($post->ID, 'Ticket-Link-1', true))

{ echo '<div class="artist-buy-tickets-box"><a class="artist-buy-button" href="'.$ticketlink.'" title="Buy tickets" target="_blank">Buy Tickets</a>';}

else { echo '<h6>TICKETS ON SALE<br/>JUNE 15 2011</h6>' ;}

?>

5 个答案:

答案 0 :(得分:4)

您的;之后有一个if,并且在第一个回音之后错过;

<?php 
if (stripos($_SERVER['REQUEST_URI'],'/workshops/') !== false) {
    echo '';
}
elseif ($ticketlink = get_post_meta($post->ID, 'Ticket-Link-1', true)) {
    echo '<div class="artist-buy-tickets-box"><a class="artist-buy-button" href="'.$ticketlink.'" title="Buy tickets" target="_blank">Buy Tickets</a>';
}
else { 
    echo '<h6>TICKETS ON SALE<br/>JUNE 15 2011</h6>';
}
?>

答案 1 :(得分:2)

你只有一个小的语法错误。这段代码格式如何可读?请改用这样的东西:

<?php 
if (stripos($_SERVER['REQUEST_URI'],'/workshops/') !== false) {
    echo '';
} elseif ($ticketlink = get_post_meta($post->ID, 'Ticket-Link-1', true)) {
    echo '<div class="artist-buy-tickets-box"><a class="artist-buy-button" href="' . $ticketlink . '" title="Buy tickets" target="_blank">Buy Tickets</a>';
} else {
    echo '<h6>TICKETS ON SALE<br/>JUNE 15 2011</h6>';
}
?>

错误是:echo '' };,应为echo ''; }

答案 2 :(得分:1)

在“elseif”之前丢掉分号;

答案 3 :(得分:1)

<?php 
if(stripos($_SERVER['REQUEST_URI'],'/workshops/') !== false) 
{
   echo '';
} 
elseif ($ticketlink = get_post_meta($post->ID, 'Ticket-Link-1', true)) 
{ 
   echo '<div class="artist-buy-tickets-box"><a class="artist-buy-button" href="'.$ticketlink.'" title="Buy tickets" target="_blank">Buy Tickets</a>';
} 
else 
{ 
   echo '<h6>TICKETS ON SALE<br/>JUNE 15 2011</h6>';
}
?>

答案 4 :(得分:1)

你的代码非常混乱。尝试清理一下,大多数语法错误都会立即显现出来。

这是您的代码具有更好的格式和删除分号导致您的问题:

<?php 

if (stripos($_SERVER['REQUEST_URI'],'/workshops/') !== false)
{
    echo ''
} elseif ($ticketlink = get_post_meta($post->ID, 'Ticket-Link-1', true)) {
    echo '<div class="artist-buy-tickets-box"><a class="artist-buy-button" href="'.$ticketlink.'" title="Buy tickets" target="_blank">Buy Tickets</a>';
} else {
    echo '<h6>TICKETS ON SALE<br/>JUNE 15 2011</h6>';
}

?>