News for hackers. Stuff that matters.
Re:Upcoming changes in PHP 7.1 by Mithaldu on Monday May 23, 2016 @07:44AM (#11753204)
I don't like PHP either, but that doesn't mean kneejerk reactions are excusable, and at the least on the first point you are wrong. Using | instead of || is not an inconsistency, but actually quite consistent.

|| is a short-cutting or comparison operator which returns the first true thing it encounters.

| is a bitwise or combinator, which takes two sets of flags, and combines them into one set of flags that has all flags activated which were also active in the input sets.

Semantically, | is absolutely the correct choice here.

A better question would be why there's a $2 there, though i hope that's just a typo and means $e.

Re:Upcoming changes in PHP 7.1 by oneeyedpigeon on Monday May 23, 2016 @09:09AM (#11753585)
> || is a short-cutting or comparison operator which returns the first true thing it encounters.

I wish. The fact that it actually doesn't is one of the things that really bugs me about PHP:

    <?php
    var_dump(0 || 42);
The above example will output:

    bool(true)
Re:Upcoming changes in PHP 7.1 by TazeTSchnitzel on Monday May 23, 2016 @09:16AM (#11753610)
PHP does have the shorthand ternary operator `?:`, though, which does what you want here:

  $ php -r 'var_dump(0 || 42);'
  bool(true)
  $ php -r 'var_dump(0 ?: 42);'
  int(42)
Re:Upcoming changes in PHP 7.1 by oneeyedpigeon on Monday May 23, 2016 @09:21AM (#11753639)
Wow. OK, I am a big user of the ternary operator, but I hadn't come across omission of the middle part until now. To be honest, it's not the most accessible syntax (especially if you're coming from another language), but at least I have the option now - thanks!

edit: Just to clarify for anyone else unfamiliar with it:

    $ php -r 'var_dump(42 ?: 0);'
    int(42)
Re:Upcoming changes in PHP 7.1 by rbritton on Monday May 23, 2016 @12:17PM (#11754803)
Depending on how far back you have to support (e.g., WordPress is still supporting 5.2), omission of the middle part wasn't supported until PHP 5.3.
Re:Upcoming changes in PHP 7.1 by TazeTSchnitzel on Monday May 23, 2016 @04:02PM (#11756466)
5.3 came out seven years ago and isn't even officially supported by the PHP project now, let alone 5.2.
Re:Upcoming changes in PHP 7.1 by rbritton on Wednesday May 25, 2016 @01:09PM (#11771338)
I know all too well, and you'd never catch me running that on any of my own systems. With a large enough install base, it's not uncommon to have a decent number of people on those older versions though.
Re:Upcoming changes in PHP 7.1 by claar on Monday May 23, 2016 @01:53PM (#11755543)
Actually || is short circuiting and behaves exactly as he described, it returns the first true thing it encounters.

  $ php -a
  Interactive shell
  
  php > echo 1 || die('die');
  1
This echo's 1, it doesn't die.
Re:Upcoming changes in PHP 7.1 by Mithaldu on Monday May 23, 2016 @09:13AM (#11753598)
... ok, i gotta admit i had my Perl hat on for that reply.
Re:Upcoming changes in PHP 7.1 by claar on Monday May 23, 2016 @01:55PM (#11755553)
You were actually correct -- see my above reply.
Re:Upcoming changes in PHP 7.1 by arenaninja on Monday May 23, 2016 @08:16AM (#11753333)
This is correct. PHP makes extensive use of bitwise operators for function options. For examples, see json_encode, htmlentities, filter_input, etc.
Re:Upcoming changes in PHP 7.1 by spdionis on Monday May 23, 2016 @09:02AM (#11753558)
I didn't understand why the remark was there at all. Honestly | reminds me of sum types and it's the perfect choice here.
Re:Upcoming changes in PHP 7.1 by amochohan on Monday May 23, 2016 @09:26AM (#11753666)
The reason I added this was because most developers are used to using a double pipe when writing code that has 'or' conditions.

I understand the use of pipes for bitwise, but in this case if you read the code in the example, we're basically saying "if the exception is of type A or of type B", personally I'd be inclined to use || if I didn't already know the syntax.

Re:Upcoming changes in PHP 7.1 by amochohan on Monday May 23, 2016 @09:23AM (#11753650)
Indeed it was a typo, i've now fixed that in the article. Thanks.
Re:Upcoming changes in PHP 7.1 by bpicolo on Monday May 23, 2016 @07:51AM (#11753237)
Better yet, don't reinvent the world and just use commas?
Re:Upcoming changes in PHP 7.1 by fredleblanc on Monday May 23, 2016 @07:59AM (#11753271)
Commas are used in PHP as a sequence of things, not multiple options. This single-pipe notation is already established as the further-up comments states.
Re:Upcoming changes in PHP 7.1 by jack9 on Monday May 23, 2016 @10:08AM (#11753928)
> Commas are used in PHP as a sequence of things

Choosing a pipe because it's easier for an obscure language feature, is not least astonishment. Commas are used for lists. The pipe is a bad choice among a nice feature.

Re:Upcoming changes in PHP 7.1 by Mithaldu on Monday May 23, 2016 @07:58AM (#11753267)
Commas would require changing the syntax and implementation of catch. This way exception names just become first class subjects that can be combined with an overloaded operator.