1

I have this:

case true:

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

    default:
    echo '<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;
}
7
  • 2
    switch((check_friend_state($showU["id"], 'friend') == true))... wow. Commented Dec 14, 2010 at 18:29
  • noone asked for your comment, sorry im too newbie for your likings Commented Dec 14, 2010 at 18:36
  • 2
    @Karem If you're a newbie and you realize it then start with the basics. My intention was not to offend you, I was genuinely surprised by your use of a case construct. Also no one has to ask for any comments on SO. It's an open website. Commented Dec 14, 2010 at 18:39
  • @AlinPurcaru and you did need to put the surprise to words..? Commented Dec 14, 2010 at 18:42
  • @Karem I consider my actions justified. If you feel that they do not contribute to the value of this post then you are welcomed to flag the comments. Commented Dec 14, 2010 at 18:45

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Are you saying switch() is more efficient? How so?
@Webnet Switches are more efficient when checking against multiple cases because the checked expression is evaluated only once. But in this particular situation there wouldn't be any difference between using a switch or an if/else construct because either way only one evaluation is needed.
What @Alin Purcaru said... ^^^
@Webnet still the same results, it think its true still. Maybe i could do something to the function that returns err1, err2 etc? I cant return 0 on all the errors because then I cant handle which error it was (in my further coding)
@Karem if your function returns multiple value types (boolean, integer, string) you should focus on fixing the function to return only one type. Otherwise you'll be doing Pokemon-style coding everywhere you go.
2

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 ==.

Comments

0

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>';

2 Comments

check_friend_state($showU["id"], 'friend') == true Why oh why? I thought you advocated brevity?
Much better! Although, since the OP's function returns multiple value types, not just booleans, he's going to have the same problem in your example (because strings evaluate true). To be clear, the problem is not with your example (which is good), it's with the OP's function.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.