开发者

Is there anything wrong with these if statements? [closed]

开发者 https://www.devze.com 2023-04-11 07:19 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.
<?php
if ( get_option('to_breadcrumbs') == 'Yes' );
if ( get_option('to_breadcrumbs') != 'No' ) {
     if (function_exists('dimox_breadcrumbs')) dimox_breadcrumbs();
} ?>

I'm pretty new to php. Is there anything about t开发者_如何学编程he code above that should be fixed?


I think the syntax is correct, but the logic is not right. get_option('to_breadcrumbs') != 'No' means the same as get_option('to_breadcrumbs') == 'Yes', assuming the value can be either yes or no.


It is syntactically correct.

if ( get_option('to_breadcrumbs') == 'Yes' );

That is not needed. There is no code being ran from outside of it.

if ( get_option('to_breadcrumbs') != 'No' ) {
    if (function_exists('dimox_breadcrumbs')) dimox_breadcrumbs();
}

This will run and do something. You can shorten it to a simple statement.

if (get_option('to_breadcrumbs') != 'No' and function_exists('dimox_breadcrumbs'))
    dimox_breadcrumbs();

It is your choice to code it however you wish but the line above is preferred by most PHP programmers.


The second line

if ( get_option('to_breadcrumbs') == 'Yes' );

doesn't make sense, it doesn't do anything except call get_option() - but the condition isn't acted upon.

The rest seems sane (not knowing what the functions actually do of course.)


I would have written it like this:

<?ph   
//I'm assuming here that only you only want to run it if the to_breadcrumbs option is equal to yes.
if(get_option('to_breadcrumbs') == 'Yes' AND function_exists('dimox_breadcrumbs')){
    dimox_breadcrumbs();
}
?>
0

精彩评论

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