Jump to content
Larry Ullman's Book Forums

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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...