开发者

Debugging - Improperly nested PHP if statement

开发者 https://www.devze.com 2023-02-09 22:02 出处:网络
I have a moderately complex PHP if statement with a few levels.Took me a while to get to this point but I just discovered that if (ICL_LANGUAGE_CODE == \'fr\') {} echoes true outside of the following

I have a moderately complex PHP if statement with a few levels. Took me a while to get to this point but I just discovered that if (ICL_LANGUAGE_CODE == 'fr') {} echoes true outside of the following code so I know I have a syntax error. Here is the code

In the following code, I know that my problem is on the commented lines:

        <?php

        $post_id = get_the_ID();

        if (in_category( array('blog','blog-fr'))) {
                echo '<h4 class="post_date"><span class="smaller">Posted On </span> ';
                the_time('F j, Y');
                echo '</h4>';
        } elseif (in_category( array('events','evenements','featured','en-vedette','performances','performances-evenements','productions','productions-evenements','workshops','ateliers','world-citizen-dance','world-citizen-dance-evenements'))) {
                echo '<h开发者_如何学运维4 class="event_top_date"><span class="month">';
                    //either it's THIS line
                    if (ICL_LANGUAGE_CODE == 'fr') { event_s_fr_month(); }
                    //or THIS one
                    else { if ( function_exists (event_date('start','M')));}
                echo '</span><span class="day">';
                if( function_exists (event_date('start','d')));         
                echo '</h4>';
        } else {
                echo '<h4 class="post_date"><span class="smaller">Posted On </span> ';
                the_time('F j, Y');
                echo '</h4>';
        } ?>

EDIT: Code below has been updated from the comments, but still not working

        } elseif (in_category( array('events','evenements','featured','en-vedette','performances','performances-evenements','productions','productions-evenements','workshops','ateliers','world-citizen-dance','world-citizen-dance-evenements'))) {
                echo '<h4 class="event_top_date"><span class="month">';
                    if (ICL_LANGUAGE_CODE == 'fr') { event_s_fr_month();}
                    else { event_date('start','M');}
                echo '</span><span class="day">';
                if( function_exists (event_date('start','d')));         
                echo '</h4>';


http://php.net/manual/en/function.function-exists.php

function_exists() takes a string parameter to determine whether a function of that name exists or not.

lets say event_date('start', 'd') returns a string "fri" or something

with this: if( function_exists (event_date('start','d')));

you are saying: if there is a function named 'fri()', do nothing

It looks like you're using Wordpress, and the event_date function I'm guessing is not being echoed. Also, you should be able to assume the function exists.

try something like this:

$post_id = get_the_ID();
$categories = array('events','evenements','featured','en-vedette','performances','performances-evenements','productions','productions-evenements','workshops','ateliers','world-citizen-dance','world-citizen-dance-evenements');
if( in_category( array('blog','blog-fr') ) )
{
    echo '<h4 class="post_date"><span class="smaller">Posted On </span> ';
    the_time('F j, Y');
    echo '</h4>';
}
elseif (in_category( $categories ) )
{
    echo '<h4 class="event_top_date"><span class="month">';
    if (ICL_LANGUAGE_CODE == 'fr')
    {
        echo event_s_fr_month();
    }
    else
    {
        echo event_date( 'start','M' );
    }
    echo '</span><span class="day">';
    event_date('start','d');
    echo '</h4>';
}
else
{
    echo '<h4 class="post_date"><span class="smaller">Posted On </span> ';
    the_time('F j, Y');
    echo '</h4>';
}

If by some chance you do need those functions available though, wrap them like this:

if( function_exists( 'event_date' ) )
{
    echo event_date( 'start','M' );
}


Remove this semicolon

 else { if ( function_exists (event_date('start','M')));  <-- This semicolon }

And where is the true part of this if statement put some code which will be executed if there is the passed function exists.

 else { if ( function_exists (event_date('start','M'))) {  //True part code here }  }

Probably you need if..elseif

if (ICL_LANGUAGE_CODE == 'fr')
{ 
    event_s_fr_month(); 
}
else if ( function_exists (event_date('start','M')))
{
  //code here
}


I'm quite sure that else { if ( function_exists (event_date('start','M')));} is incorrect. You probably want something like elseif ( function_exists (event_date('start','M'))){ // code here }. Where // code here is whatever action you'd like to take if that statement is true.


What does netbeans say?

Using a php editor that highlights syntax errors make these kinds of errors easy to debug.

Or use "Source -> Format" to reformat to see how the code is nested.

PS.

 if( function_exists (event_date('start','d'))

Executes the event_date() function and checks if the returnvalue is a existing function.

I haven't seen this construction before, did you mean:

 if ( function_exists('event_date') ) {
   event_date('start','d')
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号