Asked  7 Months ago    Answers:  5   Viewed   30 times

I'm pretty sure this is a simple fundamental flaw in my newb PHP knowledge, but I was surprised when the following happened:

enter image description here

$result is TRUE... so why is it considered equal to the string "email"? I'm guessing this is because, technically, it's a bool and it isn't false? So when it's compared against a string (e.g. "email") it returns true.

Should I change my method to return as the result as a string containing "true" (instead of return true; on success), or is there another way I should be doing this?

Thanks.

 Answers

54

Yes, true is equal (==) to a non-empty string. Not identical (===) though.

I suggest you peruse the type comparison table.

Wednesday, March 31, 2021
 
iftheshoefritz
answered 7 Months ago
80

Maybe the following is a solution:

// loop through array1
foreach($array1 as $line) {
    // check if the word is found
    $word_found = false;

    // explode on every word
    $words = explode(" ", $line);

    // loop through every word
    foreach($words as $word) {
        if(in_array($word, $array2)) {
            $word_found = true;
            break;
        }
    }

    // if the word is found do something
    if($word_found) {
        echo "There is a match found.";
    } else {
        echo "No match found."
    }
}

Should give you the result you want. I'm absolute sure there is a more efficient way to do this.. but thats for you 2 find out i quess.. good luck

Friday, May 28, 2021
 
daiscog
answered 5 Months ago
89

Here are a couple of guidelines:

  • Boolean operators are usually used on boolean values but bitwise operators are usually used on integer values.
  • Boolean operators are short-circuiting but bitwise operators are not short-circuiting.

The short-circuiting behaviour is useful in expressions like this:

if x is not None and x.foo == 42:
    # ...

This would not work correctly with the bitwise & operator because both sides would always be evaluated, giving AttributeError: 'NoneType' object has no attribute 'foo'. When you use the boolean andoperator the second expression is not evaluated when the first is False. Similarly or does not evaluate the second argument if the first is True.

Tuesday, June 1, 2021
 
Juicy
answered 5 Months ago
47

If you want true values to be greater than false, you must correct your compare() method:

@Override
public int compare(Patient p1, Patient p2) {

    if (p1.isPreviouslyInQueue() && !p2.isPreviouslyInQueue()) 
        return -1;
    if (!p1.isPreviouslyInQueue() && p2.isPreviouslyInQueue()) 
        return 1;
    return 0;
}

notice to second if. another implementation may be :

@Override
public int compare(Patient p1, Patient p2) {
    return ((Boolean)p2.isPreviouslyInQueue()).compareTo(p1.isPreviouslyInQueue());
}
Sunday, October 3, 2021
 
CAMason
answered 3 Weeks ago
83

You can just allow MATLAB to perform standard matrix multiplication and convert the result to logical:

b1 = [1,1,1;1,0,1;1,0,0]
b2 = [1,0,1;1,0,0;0,1,0]
bout = (b1*b2)>0 % or logical(b1*b2) as per natan's answer!

bout =

     1     1     1
     1     1     1
     1     0     1

However, if you want to faithfully perform the logical AND-OR operations of the Boolean matrix multiplication operator, you can do it with bsxfun and any as follows:

bout = any(bsxfun(@and,permute(b2,[3 2 1]),permute(b1,[1 3 2])),3);

That pretty well obfuscates the process, but it follows the formula.

Quick test data: b1 = randi(2,M,N)-1; b2 = randi(2,N,M)-1;.

Thursday, October 14, 2021
 
Fogmeister
answered 1 Week ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :