开发者

PHP: switch() default for all?

I have this:

case true:

    echo '<textarea rows="2" cols="35" name="message_friend" id="message_friend"></textarea>';
    break;

    default:
    echo '开发者_Go百科<textarea rows="2" cols="35" name="message_friend" id="message_friend" readonly="readonly"></textarea>';
    break;

I am trying to ONLY if it's true, then show normal else do READONLY.

The switch is checking from a function

switch( ( check_friend_state($showU["id"], 'friend') ) )

And I tried to echo the function, and it returned err2 and not true, so why does it run true?

I also tried if/else

if(check_friend_state($showU["id"], 'friend') == true){

echo '<textarea rows="2" cols="35" name="message_friend" id="message_friend"></textarea>';
}else{
echo '<textarea rows="2" cols="35" name="message_friend" id="message_friend" readonly="readonly"></textarea>';
}

But as said previously it returns "err2" and still it runs true?

My function at the return:

if($USER == $uID){ // not yourself
return "err1";
}elseif( $checkIsFriend->rowCount() == 1 ){  // already friends
return "err2";
}elseif( $checkAlready->rowCount() == 1 ){ // already send a request
return "err3";
}elseif( $checkBlock->rowCount() == 1 ){ // blocked
return "err4";
}else{
return true;
}


This gets a little sloppy, but $return ends up being the value that was returned by check_friend_state()

echo '<textarea rows="2" cols="35" name="message_friend" id="message_friend"';
    if (!($return = check_friend_state($showU["id"], 'friend')) == true) {
         echo ' READONLY';
    }
echo '></textarea>';

if check_friend_state($showU["id"], 'friend') returns anything but false or 0 it will be true

Why be set on a switch()? It seems like switch() is more intensive on PHP because it requires more syntax, thus if used excessively it can actually have a negative effect. Though this is a guess and based on no facts.


This is an artifact of PHP converting types in comparisons for you. Numbers != 0 and non-empty strings are considered to be true values when converted to booleans.

You can prevent this by using === instead of ==.


IMHO ternary if statements work better in these cases for brevity of code... with the benefit of having to fiddle with the rendered HTML only once. But that may be a more personal preference..

   $readOnly=check_friend_state($showU["id"], 'friend')?'readonly="readonly"':'';
   echo '<textarea rows="2" cols="35" name="message_friend" id="message_friend" '.$readOnly.'></textarea>';


This is a modified version of @Webnet's answer.

echo '<textarea rows="2" cols="35" name="message_friend" id="message_friend"';
if(check_friend_state($showU["id"], 'friend') !== true) {
    echo 'readonly="readonly"';
}
echo '></textarea>';

It will work for you because of the !== operator.

Side Note

Your function, check_friend_state is the root of the problem. You need to restrict its return values to a single type. If it only returned a boolean, you wouldn't have your current problem.

Rather than developing a single function to handle and return all possible values, you should break it up into multiple functions, each handling a single aspect of the logic. For example:

echo '<textarea rows="2" cols="35" name="message_friend" id="message_friend"';
if(!is_friend($showU["id"])) {
    echo 'readonly="readonly"';
}
echo '></textarea>';

In that fictional example, the is_friend function only returns a boolean (true/false). You would have other functions that handle anything else that check_friend_state used to do.

Side note #2

Looking at your function, it seems as though you've halfway grokked the concept of throwing exceptions. Try reading up here: http://php.net/manual/en/language.exceptions.php

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜