Php 如果不是,多个
如果两个if语句都为false,如何显示一些默认代码 我有这个密码-Php 如果不是,多个,php,if-statement,Php,If Statement,如果两个if语句都为false,如何显示一些默认代码 我有这个密码- <?php if( get_sub_field('cta_phone')): ?> <a class="phone" href="tel:<?php the_sub_field('cta_phone');?>"><?php the_sub_field('cta_phone');?></a></span> <?php else: ?>&
<?php if( get_sub_field('cta_phone')): ?>
<a class="phone" href="tel:<?php the_sub_field('cta_phone');?>"><?php the_sub_field('cta_phone');?></a></span>
<?php else: ?><?php endif; ?>
<?php if( get_sub_field('cta_mobile')): ?>
<a class="phone" href="tel:<?php the_sub_field('cta_mobile');?>"><?php the_sub_field('cta_mobile');?></a></span>
<?php else: ?><?php endif; ?>
<?php else: ?>
<img src="http://my-domain/image.jpg">
<?php endif; ?>
我现在没有在末尾使用else
位,因为我只想显示两个'if'是否都为false
希望它是有意义的您可以使用1个php标记来提高语法的可读性和可用性 避免 多个
标记
您还可以使用echo
像这样使用php
脚本来打印html
标记
<?php
if( get_sub_field('cta_phone')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_phone').'">'.the_sub_field('cta_phone').'</a></span>';
} else if (get_sub_field('cta_mobile')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_mobile').'">'.the_sub_field('cta_mobile').'</a></span>';
} else {
//do what you want to do here, if they are false.
}
好的,谢谢,如果两个if都不是真的,“else”可以工作,但是如果我有一个是真的或两个都是真的,它只显示第一个条目,但显示两次
<?php
if( get_sub_field('cta_phone')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_phone').'">'.the_sub_field('cta_phone').'</a>';
} else if (get_sub_field('cta_mobile')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_mobile').'">'.the_sub_field('cta_mobile').'</a>';
} else {
echo '<img src="http://placehold.it/350x150">';
}
?>
如果两条语句都为true(if和else if),则它将按优先级执行(意味着它将执行第一个true“if”),“if”一次只能执行其中一个if。它不能在一个if-elseif-else树下执行多个if。谢谢,太好了:)