Should we always return something on a function? A pratical example

Hello all,

I’m almost figuring out what I’m doing here, but I have a couple of questions:

After establishing a socket connection using fsockopen, I get a filepoint of stream type. Ok.

We are now able to use fwrite and other functions like if it was a file.


/**
* @desc sends a XML frame with 4 bytes at the beginning
*
*/
public function enviarFrame($xml)
{
        $frame = fwrite($this->_filePointer, pack('N', (strlen($xml)+4)).$xml);
        return $frame;
}

Should we return $frame, or this is redundant, and doing only:


public function enviarFrame($xml)
{
    fwrite($this->_filePointer, pack('N', (strlen($xml)+4)).$xml);   
}

Will be enough ?

The pack with ‘N’ means, format the returned integer as Big Endian.
The server where this needs to be send, seems to request this “format?” my question is, what is the INT format of php by default? Or this does “something” to our returned strlen that I’m not aware about?

Any help please?
Márcio

[ot]Glasgow is much more cultural. :stuck_out_tongue: :wink:

[/ot]

Thanks. :slight_smile:

Off Topic:

I love it, every day. (:

The fwrite function gets executed, and its return value gets stored in $something.

:stuck_out_tongue: I’m you do. Ahhhrrhh!!! :slight_smile:

If we do something like this:


/**
* @desc sends a XML frame with 4 bytes at the beginning
*
*/
public function enviarFrame($xml)
{
        $frame = fwrite($this->_filePointer, pack('N', (strlen($xml)+4)).$xml);
        return $frame;
}

We will have returned the number of bytes written. If we wanted that, this would be the solution, but we just want to write to the socket, hence, the method without return will be enough.

Please say otherwise if I’m missing the point.

Thanks again,
Márcio

If you’ll never check the return value of the method, then don’t return anything. If you’ll have some want/need to check the return value (e.g. to confirm that some bytes were written) then return something useful. It really is that simple.

Of course, and then we use can use that returned value and throw some errors.

I have a question however, it may seem stupid, I know. Still… :slight_smile:

If we have $something = fwrite(‘…’);

This only saves the fwrite written bytes into the variable.
Or, this will do that AND also, write something on a given file or socket location?

Thanks,
Márcio

ps- absolutely off topic - I’ve loved Edinburgh landscape when I was there. Really amazing. :slight_smile: