我有一个函数,如果值等于A,C或D,我想显示一些东西。
问题是如果值设置为B或E,则该功能是A.C.D中的显示数据。除了B或E之外还设置。
我做错了什么?
$linkp = opensky_featured_image_position();
if ( $linkp == 'featuredimg-1' || 'featuredimg-3' || 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
if ( $linkp == 'featuredimg-2' ) {
echo '<h1>L - '. opensky_featured_image_position() .'</h1>';
}
if ( $linkp == 'featuredimg-5' ) {
echo '<h1>N - '. opensky_featured_image_position() .'</h1>';
}
答案 0 :(得分:8)
if ( $linkp == 'featuredimg-1' || 'featuredimg-3' || 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
应该是
if ( $linkp == 'featuredimg-1' || $linkp == 'featuredimg-3' || $linkp == 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
如果定义了$ linkkp的所有值,您可以执行类似这样的操作
$linkArr = array(
'featuredimg-1' => 'F',
'featuredimg-2' => 'L',
'featuredimg-3' => 'F',
'featuredimg-4' => 'F',
'featuredimg-5' => 'N',
);
$linkp = opensky_featured_image_position();
echo '<h1>'.$linkArr[$linkp].' - '. opensky_featured_image_position() .'</h1>';
答案 1 :(得分:2)
关于operator precedence的全部内容。由于==具有更高的优先级,因此您的代码将被重写如下:
if ( ($linkp == 'featuredimg-1') || 'featuredimg-3' || 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
转为
if ( false || true || true ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
因为当你将一个非空字符串转换为bool时,它将被评估为true。
你必须重写它:
if ( $linkp == 'featuredimg-1' || $linkp == 'featuredimg-3' || $linkp == 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
符合以下条件:
if ( ($linkp == 'featuredimg-1') || ($linkp == 'featuredimg-3') || ($linkp == 'featuredimg-4') ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
但是当你处理这种情况时,也许最好使用in_array
答案 2 :(得分:1)
对我来说,这似乎是因为你过多地使用if
来描述一些非常简单的东西:某些值映射到字符,本质上是静态数据。无需使用如此多的详细代码将该数据写入代码中。
考虑使用数组代替根据数字获取您实际寻找的角色,然后再回复它:
$linkp = opensky_featured_image_position();
sscanf($linkp, 'featuredimg-%d', $number);
$chars = 'FLN';
$map = array(1 => 0, 2 => 1, 3 => 0, 4 => 0, 5 => 2);
echo '<h1>', $chars[$map[$number]], ' - ', $linkp, '</h1>';
答案 3 :(得分:0)
应该是:
if ( $linkp == 'featuredimg-1' || $linkp == 'featuredimg-3' || $linkp == 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
..
答案 4 :(得分:0)
'featuredimg-3'将始终被评估为true,您必须在每个或子条件中尝试$ linkp =='whatever'。
所以:
if ( $linkp == 'featuredimg-1' || $linkp == 'featuredimg-3' || $linkp == 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
答案 5 :(得分:0)
您需要为==
子句中的每个条件指定or
运算符,否则,将自行评估字符串值,例如'featuredimg-3',并且任何非-empty string计算结果为true。
此外,当选项互斥时,最好使用elseif
,这样PHP可以在匹配1时跳过剩余的if语句。
我还添加了最终的其他内容,因此您可以看到在没有任何值匹配的情况下您可以做些什么。
$linkp = opensky_featured_image_position();
if ( $linkp == 'featuredimg-1' ||
$linkp == 'featuredimg-3' ||
$linkp == 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
} elseif ( $linkp == 'featuredimg-2' ) {
echo '<h1>L - '. opensky_featured_image_position() .'</h1>';
} elseif ( $linkp == 'featuredimg-5' ) {
echo '<h1>N - '. opensky_featured_image_position() .'</h1>';
} else {
echo '<h1>Invalid or missing image position</h1>';
}
您可能还想查看switch/case
结构。
像其他人所建议的那样使用数组确实可以简化并且可以说是可读性。如果你确实使用数组,你可能仍然需要检查以确保在数组中找到一个值并处理该情况,如果你没有。