php - !$variable = $variable inside if

650

I was gnawing on the basics of PHP socket servers and client here.

Then I stumbled upon these lines (excerpt from the above links first example, happens insidewhile):

if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
    echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
    break 2;
}

if (!$buf = trim($buf)) {
    continue;
}

I'm okay with the reading part, and closing connection in case there was an error reading.

But that nextif is driving me nuts.
Firstly, I'm confused about how one may and would need to assign a value to boolean.
Secondly, I have trouble understanding the whole expression altogether.

Could someone please explain what happens inside that if, and how it applies to the server context?

P.S. Please excuse me if the question isn't properly asked. I'm just too confused about what happens there and have no idea what specifically to ask for.

90

Answer

Solution:

With this statement, there's no assignment to a boolean. We're comparing the type offalse and the value offalse (true false, not merely 0). You can read about that here

if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ)))

Then

if (!$buf = trim($buf)) {
    continue;
}

This is the same as

$buf = trim($buf);
if ($buf == false) {
  continue;
}

So, to explain the original:

// The assignment happens first and then we're checking the boolean value
if (!$buf = trim($buf))
32

Answer

Solution:

Here's the explanation of the code.

false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))

First,socket_read is called, and the result is stored in$buf. Then$buf is compared withfalse to see whether or not it is abool and its value isfalse. (=== means same type and equality at the same time)

if (!$buf = trim($buf)) {
    continue;
}

In this part of the code, first$buf is trimmed and the result is stored in$buf. Then it's checked whether or not the$buf isfalse. If it is false,continue takes action.

863

Answer

Solution:

The expression is splitable.

  • $buf = trim($foo)
  • $check = !$buf
  • if ($check) { something(); }

first you get the variable, then check wether it evaluates to false (might befalse, might benull might be0), and lastly oyu do the switch based on that information.

793

Answer

Solution:

if you are seeking basics have a journey!

try this code

<?php
    $buf="0 ";// (note the spaces)
    //next try changing $buf="1 "; (note the spaces for trim)

    echo $buf ."<----Non trimmed buf<br>" ;
    $buf= trim($buf);
    echo $buf ."<----trimmed buf<br>" ;
    echo !$buf ."<----!buf<br>" ;

    if (!$buf = trim($buf))
       echo "Trimmed buf is false";
    else
       echo "Trimmed buf is True";
?>

output

0 <----Non trimmed buf
0<----trimmed buf
1<----!buf
Trimmed buf is false

People are also looking for solutions to the problem: php - Cookies not visible after ajax/load request

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.