Live data from Hacker News

Upcoming changes in PHP 7.1

dotdev.co

61–70 of 137 posts

Re: Upcoming changes in PHP 7.1

#61

Earlier quoted context omitted.

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 f…

> || 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: The above example will output: bool(true)

... ok, i gotta admit i had my Perl hat on for that reply.

Re: Upcoming changes in PHP 7.1

#62

Earlier quoted context omitted.

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 f…

> || 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: The above example will output: bool(true)

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

#63

Earlier quoted context omitted.

> || 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: The above example will output: bool(true)

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)

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

#64

I'm more excited by some features not detailed in the article. Return type declarations, which were introduced in PHP 7.0, are being enhanced somewhat. First, it's now possible to declare nullable return types (previously all return types did not permit null): public function getFoo(): ?int; (You can also use ? on parameter and property types.) Second, we now have a void return type for functions where there is no us…

I touched on the shortened [] syntax and non-numeric string arithmetic in the article.

Thank you for getting these added to the language, I think it's a fantastic improvement.

Re: Upcoming changes in PHP 7.1

#65

... Note the syntax isn’t the usual double pipe || operator that we associate with or, rather a single pipe | character ... ... PHP 7.1 introduces visibility modifiers to constants ... ... With PHP 7.1 it is now possible to specify that a function has a void return type, i.e. it performs an action but does not return anything ... ... Example four contains numeric values, so everything else is stripped out, and the su…

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 f…

Indeed it was a typo, i've now fixed that in the article. Thanks.

Re: Upcoming changes in PHP 7.1

#66
post #57

Earlier quoted context omitted.

Ahead-of-time type checking can only really work if the entire program has type declarations everywhere, and if you know what constitutes the entire program. This isn't the case for the vast majority of existing PHP code. Furthermore, PHP's dynamic features mean that there are cases where ahead-of-time checks will always be impossible. So you have to have at least some runtime checks, at the boundaries between typed…

I've seen quite a few JavaScript bugs and potential bugs exposed after adding incomplete type annotations in TypeScript, so I really don't think it's all or nothing, at least not as a general rule. I've been away from PHP too long to speculate how much it would help here.

Sure, it's not all-or-nothing, type checks that aren't enforced at runtime have some benefits, I just disagree that they can provide all of them.

Re: Upcoming changes in PHP 7.1

#67

Earlier quoted context omitted.

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 f…

I didn't understand why the remark was there at all. Honestly | reminds me of sum types and it's the perfect choice here.

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

#68

Earlier quoted context omitted.

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 f…

> || 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: The above example will output: bool(true)

[deleted]

Re: Upcoming changes in PHP 7.1

#70

... Note the syntax isn’t the usual double pipe || operator that we associate with or, rather a single pipe | character ... ... PHP 7.1 introduces visibility modifiers to constants ... ... With PHP 7.1 it is now possible to specify that a function has a void return type, i.e. it performs an action but does not return anything ... ... Example four contains numeric values, so everything else is stripped out, and the su…

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 f…

[deleted]
Post reply on HN