Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
继续沿着';elseif';在PHP中,即使满足了过去的条件?_Php_Wordpress_If Statement - Fatal编程技术网

继续沿着';elseif';在PHP中,即使满足了过去的条件?

继续沿着';elseif';在PHP中,即使满足了过去的条件?,php,wordpress,if-statement,Php,Wordpress,If Statement,我正在尝试创建一个“满足条件时添加到字符串”函数,下面是我的实际代码: if ( ! function_exists( '_s_posted_on' ) ) : function _s_posted_on( $params = array() ) { $output = 's'; /** Holds the string of the code output by the arguments. */ if ( in_array('time', $pa

我正在尝试创建一个“满足条件时添加到字符串”函数,下面是我的实际代码:

if ( ! function_exists( '_s_posted_on' ) ) :

    function _s_posted_on( $params = array() ) {

        $output = 's'; /** Holds the string of the code output by the arguments. */

        if ( in_array('time', $params) ) : /** Begin main loop */
            /**
            Show the time of the post
            */

            $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>';

            $time_string = sprintf( $time_string,
                esc_attr( get_the_date( 'c' ) ),
                esc_html( get_the_date() ),
                esc_attr( get_the_modified_date( 'c' ) ),
                esc_html( get_the_modified_date() )
            );

            $posted_on = sprintf(
                /* translators: %s: post date. */
                esc_html_x( 'Posted on %s', 'post date', '_s' ),
                '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
            );

            $output .= '<span class="posted-on">' . $posted_on . '</span>';

        elseif ( in_array('author', $params) ) :
            /**
            Show the post's author
            */

            $author = sprintf(
            /* translators: %s: post author. */
                esc_html_x( 'by %s', 'post author', '_s' ),
                '<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
            );

            $output .= '<span class="author"> ' . $author . '</span>';

        elseif ( in_array('category', $params) ) :
            /**
            Show the post's category.
            */

            $categories = (array) wp_get_post_terms( get_the_ID(), 'category' );

            $category_string = sprintf (
                esc_html_x( 'in %s', 'post_category', '_s' ),
                '<a class="category">' . $categories[0] -> name . '</a>'
            );

            $output .= '<span class="category">' . $category_string . '</span>';

        else :
            $output = 'No info about the post!';

        endif; /** End main loop */
        echo '<div class="post-meta-info"' . $output . '</div>';
    }
endif;
问题是循环每次遇到一个条件时都会中断,所以假设我用
$string1,$string2
调用我的函数
\u s\u posted\u on
,我的
输出应该是
$string1
$string2
的结果,但不幸的是,它在
$string1
处停止,不进一步检查
$string2


简言之,我试图根据一些条件向字符串中添加,其中可以存在两个条件,但循环在满足第一个条件时停止并中断。

elseif
意味着,如果前一个条件未通过,而此权限确实通过,则执行该操作。听起来您只想让多个
if
语句一个接一个地出现,这是完全正常的

所以你可以

if($a == true) {
  // Execute this if $a is true.
}

if($b == true) {
  // Execute this if $b is true, no matter if the first if 
  // statement was executed or not.
}

您可以尝试多个
ifs
,并带有
else

$flag = false;
if (string_exists_in_params($string, $params)):
    add_to_my_string --> $string2;
    $flag = true;

if (string_exists_in_params($string1, $params)):
    add_to_string --> $string1;
    $flag = true;

if (string_exists_in_params($string2, $params)):
    add_to_string --> $string2;
    $flag = true;

if ($flag == false):
    $output = 'No info about the post!';
建议代码:

if(!function_exists('_s_posted_on')){
    function _s_posted_on($params=array()){

        $params=array_intersect(array('time','author','category'),$params);  // filter and sort

        if(empty($params)){  // this provides a shortcut whereby NO additional conditions or function calls are required
            $output = 'No info about the post!';
        }else{
            $output=''; // I don't understand why your string should start with `s` so I am omitting it
            foreach($params as $param){  // iterate upto 3 times total
                if($param=='time'){  // function-less conditional check
                    $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>';

                    $time_string = sprintf( $time_string,
                        esc_attr(get_the_date('c')),
                        esc_html(get_the_date()),
                        esc_attr(get_the_modified_date('c')),
                        esc_html(get_the_modified_date())
                    );

                    $posted_on = sprintf(
                        /* translators: %s: post date. */
                        esc_html_x('Posted on %s','post date','_s'),
                        '<a href="'.esc_url(get_permalink()).'" rel="bookmark">'.$time_string.'</a>'
                    );

                    $output.='<span class="posted-on">'.$posted_on.'</span>';
                }elseif($param=='author'){  // function-less conditional check
                    $author = sprintf(
                        /* translators: %s: post author. */
                        esc_html_x( 'by %s', 'post author', '_s' ),
                        '<span class="author vcard"><a class="url fn n" href="'.esc_url(get_author_posts_url(get_the_author_meta('ID'))).'">'.esc_html(get_the_author()).'</a></span>'
                    );

                    $output.='<span class="author"> '.$author.'</span>';
                }else{ // can only be `category` due to filtration step and prior conditions
                    $categories=(array) wp_get_post_terms(get_the_ID(),'category');

                    $category_string=sprintf(
                        esc_html_x('in %s','post_category','_s'),
                        '<a class="category">'.$categories[0]->name.'</a>'
                    );

                    $output.='<span class="category">'.$category_string.'</span>';
                }  // end `param` conditional
            }  // end foreach
        }  // end `empty` conditional

        echo '<div class="post-meta-info">'.$output.'</div>';  // (generally, return is used in functions instead of echoing)
        // I added this greater than sign^  <-- it seems rather important!
    }
}
如果(!函数存在(“'s'u posted'u on')){
函数_s_posted_on($params=array()){
$params=array_intersect(array('time','author','category'),$params);//筛选和排序
if(empty($params)){//这提供了一个快捷方式,不需要额外的条件或函数调用
$output='没有关于帖子的信息!';
}否则{
$output='';//我不明白为什么字符串应该以's'开头,所以我省略了它
foreach($params作为$param){//最多迭代3次
if($param=='time'){//无函数条件检查
$time_string='%2$s';
$time\u string=sprintf$time\u string,
esc_attr(获取日期('c')),
esc_html(获取日期()),
esc_attr(获取修改日期('c')),
esc\u html(获取修改日期()
);
$posted_on=sprintf(
/*翻译人员:%s:发布日期*/
esc_html_x('post on%s'、'post date'、'u s'),
''
);
$output.=''.$posted_on';
}elseif($param=='author'){//无函数条件检查
$author=sprintf(
/*翻译人员:%s:文章作者*/
esc_html_x('by%s','post author','u s'),
''
);
$output.=''.$author';
}由于过滤步骤和先决条件,else{//只能是“category”
$categories=(数组)wp_get_post_terms(get_the_ID(),'category');
$category\u string=sprintf(
esc_html_x('in%s'、'post_category'、'u s'),
"
  • 始终尝试在条件块(以及循环)中编写早期退出条件。如果没有可操作的元素,则直接跳到末尾——不要检查
    时间
    ,然后检查
    作者
    ,然后再检查
    类别
    ——这是一个高效/最佳实践的问题
  • 我不知道为什么您的
    $output
    字符串以
    s
    开头,所以我删除了它
  • 我能够完全消除
    in_array()
    调用。
    foreach()
    中的
    if elseif else
    块现在不进行函数调用,事实上,甚至不需要检查
    类别,因为它在逻辑上得到了保证

  • 我在
    中添加了缺少的
    ,你有没有尝试过多个
    如果而没有
    其他如果呢?@Erwin没有!那有意义吗?我正在寻找一个
    如果也有
    ,但是多个
    如果没有
    ,就破坏了脚本。编辑:它有效。谢谢。你的方法对我来说似乎不太精细。我想更好地理解存储在
    $params
    中的潜在数据。我假设您可以控制元素及其顺序--这个数组是如何生成的?使用多个完整数组扫描不是最有效的方法。请提供我请求的详细信息,以便我可以扩展一些最佳做法建议。谢谢。@mickmackusa感谢您的关注!因此,以下就是生成
    $params
    的代码:
    同样,在我的
    \u show\u post\u info
    中,我只是看看项目是否在数组中,数组中是否有
    。这不是我的意思/我想问的问题。
    数组('author','time')如何
    generated?您是手动编写的吗?它是从其他地方的代码自动生成的吗?请澄清,因为我需要隔离if
    array('time','author'))
    是可能的。有效!非常感谢。如果有帮助,请将我的答案标记为正确答案。我很高兴它帮助了您。@JonathanGuerin您可以通过按下投票按钮下方答案旁边的空复选标记来接受答案。Erwin,有什么原因需要标记吗?如果不满足前3个条件,标记将保持为假。这是什么原因将返回最后一个条件true,并返回
    $output='No information about the post!';
    ,该条件等于您的
    else
    statement@JonathanGuerin我已经为您发布了一个干净/完善的功能,并提供了全面的解释。如果您有问题,请提问。如果您认为这是最佳答案,您可以移动绿色勾号,但是你不需要这样做。写得很棒。肯定更有意义。不过我想知道,这在速度方面如何比较?还有,我可以使用任何其他资源来支持我的PHP代码吗?我回答中的最佳实践确保这将比其他方法运行得更快。如果你在当
    time
    aut
    
    if(!function_exists('_s_posted_on')){
        function _s_posted_on($params=array()){
    
            $params=array_intersect(array('time','author','category'),$params);  // filter and sort
    
            if(empty($params)){  // this provides a shortcut whereby NO additional conditions or function calls are required
                $output = 'No info about the post!';
            }else{
                $output=''; // I don't understand why your string should start with `s` so I am omitting it
                foreach($params as $param){  // iterate upto 3 times total
                    if($param=='time'){  // function-less conditional check
                        $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>';
    
                        $time_string = sprintf( $time_string,
                            esc_attr(get_the_date('c')),
                            esc_html(get_the_date()),
                            esc_attr(get_the_modified_date('c')),
                            esc_html(get_the_modified_date())
                        );
    
                        $posted_on = sprintf(
                            /* translators: %s: post date. */
                            esc_html_x('Posted on %s','post date','_s'),
                            '<a href="'.esc_url(get_permalink()).'" rel="bookmark">'.$time_string.'</a>'
                        );
    
                        $output.='<span class="posted-on">'.$posted_on.'</span>';
                    }elseif($param=='author'){  // function-less conditional check
                        $author = sprintf(
                            /* translators: %s: post author. */
                            esc_html_x( 'by %s', 'post author', '_s' ),
                            '<span class="author vcard"><a class="url fn n" href="'.esc_url(get_author_posts_url(get_the_author_meta('ID'))).'">'.esc_html(get_the_author()).'</a></span>'
                        );
    
                        $output.='<span class="author"> '.$author.'</span>';
                    }else{ // can only be `category` due to filtration step and prior conditions
                        $categories=(array) wp_get_post_terms(get_the_ID(),'category');
    
                        $category_string=sprintf(
                            esc_html_x('in %s','post_category','_s'),
                            '<a class="category">'.$categories[0]->name.'</a>'
                        );
    
                        $output.='<span class="category">'.$category_string.'</span>';
                    }  // end `param` conditional
                }  // end foreach
            }  // end `empty` conditional
    
            echo '<div class="post-meta-info">'.$output.'</div>';  // (generally, return is used in functions instead of echoing)
            // I added this greater than sign^  <-- it seems rather important!
        }
    }