Jump to content
Larry Ullman's Book Forums

Using | To Separate Two Arguments


Recommended Posts

Chapter 11, pg. 301, contains the following text:

 

 

If you are running PHP 5.1 or greater, you can add the LOCK_EX constant as the third argument to

file_put_contents():

 

file_put_contents($file, $data, LOCK_EX);

 

To use both the LOCK_EX and FILE_APPEND constants, separate them with the binary OR operator (|):

 

file_put_contents($file, $data, FILE_APPEND | LOCK_EX);

 

It doesn’t matter in which order you list the two constants.

 

This is a really interesting solution, and I'm surprised my mind didn't registered it when I was reading that book for the first time.

 

Could someone please explain to my why the use of | operator is possible in this situation?

 

Is this a common use of such operator, allowing to place two arguments where only one is expected? Are there any other situations where | could be used in similar manner?

 

Thank you in advance for your wisdom!

Link to comment
Share on other sites

The vertical bar is a bitwise operator in PHP meaning, as Larry states, logical OR. Please see the following for details:

http://php.net/manual/en/language.operators.bitwise.php

 

The meaning of the vertical bar as logical OR can be extended for use with function arguments as defined by the PHP language. There are other functions that allow for this usage of the vertical bar with its arguments. For example, the socket_recv function:

http://jp2.php.net/manual/en/function.socket-recv.php

Link to comment
Share on other sites

 Share

×
×
  • Create New...