Re: [PHP-DEV] Implicit isset/isempty check on short-ternary operator

2011-04-08 Thread Adam Richardson


  We need to be careful about changing the beahviour of existing
 operators.


Indeed.

The '?' character already is special, so using '??' seems like a safe,
practical approach. However, I'd prefer maintaining the form of the standard
ternary operator with the colon ($value = $var['bar'] ?? : 'Bar was not
set'; // value =Bar was not) so the '??' operator could be applied in any
situation that one would normally use the standard ternary operator.

// standard
$value = isset($a[$key]) ? $a[$key] : 'Not set';

// new ?? double ternary that performs isset check and omits second
expression
$value = $a[$key] ?? : 'Not set';

// new ?? double ternary that performs isset check and uses second
expression
$value = $a[$key] ?? strtoupper($a[$key]) : 'Not set';

Granted, the last example might be infrequent, but I think there's also a
value in keeping the form of the double ternary (if used at all) the same as
the standard ternary operator for consistency sake.

Adam

-- 
Nephtali:  A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com


Re: [PHP-DEV] Implicit isset/isempty check on short-ternary operator

2011-04-08 Thread dukeofgaming
Hi, just to drop an opinion on something I felt natural when reading this:
how about a word instead?:

$value = 'Not set' unless $a['key'];

I think it would be way more readable.

Regards,

David

On Fri, Apr 8, 2011 at 2:02 AM, Adam Richardson simples...@gmail.comwrote:

 
 
   We need to be careful about changing the beahviour of existing
  operators.
 

 Indeed.

 The '?' character already is special, so using '??' seems like a safe,
 practical approach. However, I'd prefer maintaining the form of the
 standard
 ternary operator with the colon ($value = $var['bar'] ?? : 'Bar was not
 set'; // value =Bar was not) so the '??' operator could be applied in any
 situation that one would normally use the standard ternary operator.

 // standard
 $value = isset($a[$key]) ? $a[$key] : 'Not set';

 // new ?? double ternary that performs isset check and omits second
 expression
 $value = $a[$key] ?? : 'Not set';

 // new ?? double ternary that performs isset check and uses second
 expression
 $value = $a[$key] ?? strtoupper($a[$key]) : 'Not set';

 Granted, the last example might be infrequent, but I think there's also a
 value in keeping the form of the double ternary (if used at all) the same
 as
 the standard ternary operator for consistency sake.

 Adam

 --
 Nephtali:  A simple, flexible, fast, and security-focused PHP framework
 http://nephtaliproject.com



RE: [PHP-DEV] Implicit isset/isempty check on short-ternary operator

2011-04-08 Thread Ford, Mike
 -Original Message-
 From: Adam Richardson [mailto:simples...@gmail.com]
 Sent: 08 April 2011 08:02


 
 Indeed.
 
 The '?' character already is special, so using '??' seems like a
 safe,
 practical approach. However, I'd prefer maintaining the form of the
 standard
 ternary operator with the colon ($value = $var['bar'] ?? : 'Bar was
 not
 set'; // value =Bar was not) so the '??' operator could be applied
 in any
 situation that one would normally use the standard ternary operator.
 
 // standard
 $value = isset($a[$key]) ? $a[$key] : 'Not set';
 
 // new ?? double ternary that performs isset check and omits second
 expression
 $value = $a[$key] ?? : 'Not set';
 
 // new ?? double ternary that performs isset check and uses second
 expression
 $value = $a[$key] ?? strtoupper($a[$key]) : 'Not set';
 

Like it! I would have proposed ??: for the isset() variant of ?:,
and I think decomposing it into a full ternary operator in this way,
with ??: as a degenerate variant, is brilliant.

(Only downside is, people would have to stop referring to *the*
ternary operator!)


Cheers!

Mike

 -- 
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,  
Leeds Metropolitan University, C507 City Campus, 
Woodhouse Lane, LEEDS,  LS1 3HE,  United Kingdom 
Email: m.f...@leedsmet.ac.uk 
Tel: +44 113 812 4730



To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] Implicit isset/isempty check on short-ternary operator

2011-04-08 Thread Hannes Landeholm
Hi. I like Adam's suggestion _a lot_ however I'd also find a third
case very useful.

In addition to:
*
// standard
$value = isset($a[$key]) ? $a[$key] : 'Not set';

// new ?? double ternary that performs isset check and omits second
expression
$value = $a[$key] ?? : 'Not set';

// new ?? double ternary that performs isset check and uses second
expression
$value = $a[$key] ?? strtoupper($a[$key]) : 'Not set';
*

I would also like a case where you could:

// new ?? double ternary that performs isset check and uses null
$value = $a[$key]??;

This would replace a lot of ugly code I have where: (the @ operator is ugly)

$value = @$a[$key];

When I want to treat all non existing indexes as containing the null value.

~Hannes


On 8 April 2011 09:02, Adam Richardson simples...@gmail.com wrote:

 
 
   We need to be careful about changing the beahviour of existing
  operators.
 

 Indeed.

 The '?' character already is special, so using '??' seems like a safe,
 practical approach. However, I'd prefer maintaining the form of the standard
 ternary operator with the colon ($value = $var['bar'] ?? : 'Bar was not
 set'; // value =Bar was not) so the '??' operator could be applied in any
 situation that one would normally use the standard ternary operator.

 // standard
 $value = isset($a[$key]) ? $a[$key] : 'Not set';

 // new ?? double ternary that performs isset check and omits second
 expression
 $value = $a[$key] ?? : 'Not set';

 // new ?? double ternary that performs isset check and uses second
 expression
 $value = $a[$key] ?? strtoupper($a[$key]) : 'Not set';

 Granted, the last example might be infrequent, but I think there's also a
 value in keeping the form of the double ternary (if used at all) the same as
 the standard ternary operator for consistency sake.

 Adam

 --
 Nephtali:  A simple, flexible, fast, and security-focused PHP framework
 http://nephtaliproject.com

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] Implicit isset/isempty check on short-ternary operator

2011-04-08 Thread Rune Kaagaard
Dear Internals

I'm very happy that this is getting some attention again. Please allow
me to give my 2 cents too. The text below can also be seen nicely
formatted at https://gist.github.com/909711.

## Intro ##

Isset and IsNotEmpty operators have for sure been a hot topic for several years
now and in my opinion rightly so. The non-DRY style of:

$my_array['my_long_boring_key'] = !empty($my_array['my_long_boring_key'])
  ? $my_array['my_long_boring_key'] : 'Default value';
$my_array['my_long_boring_key'] = isset($my_array['my_long_boring_key'])
  ? $my_array['my_long_boring_key'] : 'Default value';

is a true day-to-day hassle and addressing this annoyance would be a
big win for
the PHP community as a whole. As PHP has two keywords `isset` and `empty` that
can check for a non existing variable without throwing errors I think there
should exist two assignment/ternary operators who mirror those.

I have been thinking [1] about the same problem for my meta language Snow and
also ended up using `??` as an isset operator.

## Proposal ##
I propose that two new operators `??` (IssetOperator) and `?!`
(NotEmptyOperator) are added. `??` mirrors `isset` and `?!` mirrors `!empty`.
They are chainable ad nauseum but not with each other.

They would work like this:

### Example 1 : Ternary shortcut ###
Old syntax:
$a = isset($b) ? $b : 42;
$a = !empty($b) ? $b : 42;

New syntax:
$a = $b ?? 42;
$a = $b ?! 42;

### Example 2 : Direct assignment ###
Old syntax:
$arr['key'] = isset($arr['key']) ? $arr['key'] : 42;
$arr['key'] = !empty($arr['key']) ? $arr['key'] : 42;

New syntax:
$arr['key'] ??= 42;
$arr['key'] ?!= 42;

### Example 3 : Works with statements too ###
Old syntax:
// a)
$tmp = get_stuff('foo');
$a = isset($tmp) ? $tmp : 42;

// b)
$tmp = get_stuff('foo');
$a = !empty($tmp) ? $tmp : 42;

New syntax:
// a)
$a = get_stuff('foo') ?? 42;

// b)
$a = get_stuff('foo') ?! 42;

### Example 4 : Chaining ###
Old syntax [2]:
$a = false;
if (!empty($c) {
$a = $c;
} else {
$tmp = get_stuff();
$a = !empty($tmp) ? $tmp : false;
}
if ($a === false) {
$a = !empty($c) ? $c : 42;
}

New syntax:
$a = $c ?! get_stuff() ?! $b ?! 42;

### Example 5 : Illegal syntax ###
$a = $d ?? $c ?! $b ?? 42; // `??` and `?!` cannot be mixed.

## References ##
 * [1]: http://code.google.com/p/php-snow/wiki/EmptyIssetOperators
 * [2]: This could also be done by nesting ternary operators, but that gets
even more unreadable I think.

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] Implicit isset/isempty check on short-ternary operator

2011-04-08 Thread Jordi Boggiano
On 08.04.2011 15:19, Rune Kaagaard wrote:
 New syntax:
 // a)
 $a = get_stuff('foo') ?? 42;
 
 // b)
 $a = get_stuff('foo') ?! 42;

This is wrong. The new syntax is already available since 5.3.0 and is

$a = get_stuff('foo') ?: 42;

Now I agree with you, it sounds great and all, but we have to be clear:

$a ?: $b  =  $a ? $a : $b

And ?? should equal to !empty($a), or otherwise said isset($a)  $a
So:

$a ?? $b  =  (isset($a)  $a) ? $a : $b

Again, your example1 was unclear. !empty is not the equivalent of
isset(). They only are equal if the value is null, but not for false or
 or array().

Seeing how everyone says confusing stuff here, I'm starting to think
those shortcuts may actually be more harmful than anything.

I'd just favor switching ?: to be equivalent to !empty() instead of just
a cast to boolean, because it means we can drop the isset checks, and
the semantics wouldn't change - only BC break would be warnings going
away. The rest, adding new operators, is dangerous imo.

Cheers

-- 
Jordi Boggiano
@seldaek :: http://seld.be/

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] Implicit isset/isempty check on short-ternary operator

2011-04-08 Thread Hannes Landeholm
I think ?! wouldn't work as an operator as it would conflict with ternary
comparision + not operator. Also I don't see the point of adding an operator
for empty as the function/construct itself is pretty confusing and
non-useful as you have to memorize all the things that happen to be
considered empty by it and those things has too align up with the things
you want to check for by chance. (I never use it.)

Empty-ness is just one of a billion things you might want to write a
comparison for (and it's not even well defined). Having an operator for that
would be crazy. Dealing with possibly undefined indexes/properties is a very
common use case though which is why I think it deserves an operator. The
empty comparison thing is a _separate issue_ and it can simply be dealt
with by separating the assignment and comparison into two expressions...

~Hannes

On 8 April 2011 15:19, Rune Kaagaard rumi...@gmail.com wrote:

 Dear Internals

 I'm very happy that this is getting some attention again. Please allow
 me to give my 2 cents too. The text below can also be seen nicely
 formatted at https://gist.github.com/909711.

 ## Intro ##

 Isset and IsNotEmpty operators have for sure been a hot topic for several
 years
 now and in my opinion rightly so. The non-DRY style of:

$my_array['my_long_boring_key'] =
 !empty($my_array['my_long_boring_key'])
  ? $my_array['my_long_boring_key'] : 'Default value';
$my_array['my_long_boring_key'] = isset($my_array['my_long_boring_key'])
  ? $my_array['my_long_boring_key'] : 'Default value';

 is a true day-to-day hassle and addressing this annoyance would be a
 big win for
 the PHP community as a whole. As PHP has two keywords `isset` and `empty`
 that
 can check for a non existing variable without throwing errors I think there
 should exist two assignment/ternary operators who mirror those.

 I have been thinking [1] about the same problem for my meta language Snow
 and
 also ended up using `??` as an isset operator.

 ## Proposal ##
 I propose that two new operators `??` (IssetOperator) and `?!`
 (NotEmptyOperator) are added. `??` mirrors `isset` and `?!` mirrors
 `!empty`.
 They are chainable ad nauseum but not with each other.

 They would work like this:

 ### Example 1 : Ternary shortcut ###
 Old syntax:
$a = isset($b) ? $b : 42;
$a = !empty($b) ? $b : 42;

 New syntax:
$a = $b ?? 42;
$a = $b ?! 42;

 ### Example 2 : Direct assignment ###
 Old syntax:
$arr['key'] = isset($arr['key']) ? $arr['key'] : 42;
$arr['key'] = !empty($arr['key']) ? $arr['key'] : 42;

 New syntax:
$arr['key'] ??= 42;
$arr['key'] ?!= 42;

 ### Example 3 : Works with statements too ###
 Old syntax:
// a)
$tmp = get_stuff('foo');
$a = isset($tmp) ? $tmp : 42;

// b)
$tmp = get_stuff('foo');
$a = !empty($tmp) ? $tmp : 42;

 New syntax:
// a)
$a = get_stuff('foo') ?? 42;

// b)
$a = get_stuff('foo') ?! 42;

 ### Example 4 : Chaining ###
 Old syntax [2]:
$a = false;
if (!empty($c) {
$a = $c;
} else {
$tmp = get_stuff();
$a = !empty($tmp) ? $tmp : false;
}
if ($a === false) {
$a = !empty($c) ? $c : 42;
}

 New syntax:
$a = $c ?! get_stuff() ?! $b ?! 42;

 ### Example 5 : Illegal syntax ###
$a = $d ?? $c ?! $b ?? 42; // `??` and `?!` cannot be mixed.

 ## References ##
 * [1]: http://code.google.com/p/php-snow/wiki/EmptyIssetOperators
 * [2]: This could also be done by nesting ternary operators, but that
 gets
even more unreadable I think.



Re: [PHP-DEV] Implicit isset/isempty check on short-ternary operator

2011-04-08 Thread Martin Scotta
I just feels that !empty($arr['key']) or isset($arr['key']) do not express
the real meaning, instead I would choose to write array_key_exists('key',
$arr). It may be slower but it clearly express what I meant.

Regarding the operators, I believe they will do more harm than good.
To check if a variable was already defined is a bad programing habit, these
operators will encourage that kind of things

In the other hand, the isset check inside the condition ?: would silently
improve not-so-well written code, and could be a good feature for the lang.

 Martin Scotta


On Fri, Apr 8, 2011 at 11:27 AM, Hannes Landeholm landeh...@gmail.comwrote:

 I think ?! wouldn't work as an operator as it would conflict with ternary
 comparision + not operator. Also I don't see the point of adding an
 operator
 for empty as the function/construct itself is pretty confusing and
 non-useful as you have to memorize all the things that happen to be
 considered empty by it and those things has too align up with the things
 you want to check for by chance. (I never use it.)

 Empty-ness is just one of a billion things you might want to write a
 comparison for (and it's not even well defined). Having an operator for
 that
 would be crazy. Dealing with possibly undefined indexes/properties is a
 very
 common use case though which is why I think it deserves an operator. The
 empty comparison thing is a _separate issue_ and it can simply be dealt
 with by separating the assignment and comparison into two expressions...

 ~Hannes

 On 8 April 2011 15:19, Rune Kaagaard rumi...@gmail.com wrote:

  Dear Internals
 
  I'm very happy that this is getting some attention again. Please allow
  me to give my 2 cents too. The text below can also be seen nicely
  formatted at https://gist.github.com/909711.
 
  ## Intro ##
 
  Isset and IsNotEmpty operators have for sure been a hot topic for several
  years
  now and in my opinion rightly so. The non-DRY style of:
 
 $my_array['my_long_boring_key'] =
  !empty($my_array['my_long_boring_key'])
   ? $my_array['my_long_boring_key'] : 'Default value';
 $my_array['my_long_boring_key'] =
 isset($my_array['my_long_boring_key'])
   ? $my_array['my_long_boring_key'] : 'Default value';
 
  is a true day-to-day hassle and addressing this annoyance would be a
  big win for
  the PHP community as a whole. As PHP has two keywords `isset` and `empty`
  that
  can check for a non existing variable without throwing errors I think
 there
  should exist two assignment/ternary operators who mirror those.
 
  I have been thinking [1] about the same problem for my meta language Snow
  and
  also ended up using `??` as an isset operator.
 
  ## Proposal ##
  I propose that two new operators `??` (IssetOperator) and `?!`
  (NotEmptyOperator) are added. `??` mirrors `isset` and `?!` mirrors
  `!empty`.
  They are chainable ad nauseum but not with each other.
 
  They would work like this:
 
  ### Example 1 : Ternary shortcut ###
  Old syntax:
 $a = isset($b) ? $b : 42;
 $a = !empty($b) ? $b : 42;
 
  New syntax:
 $a = $b ?? 42;
 $a = $b ?! 42;
 
  ### Example 2 : Direct assignment ###
  Old syntax:
 $arr['key'] = isset($arr['key']) ? $arr['key'] : 42;
 $arr['key'] = !empty($arr['key']) ? $arr['key'] : 42;
 
  New syntax:
 $arr['key'] ??= 42;
 $arr['key'] ?!= 42;
 
  ### Example 3 : Works with statements too ###
  Old syntax:
 // a)
 $tmp = get_stuff('foo');
 $a = isset($tmp) ? $tmp : 42;
 
 // b)
 $tmp = get_stuff('foo');
 $a = !empty($tmp) ? $tmp : 42;
 
  New syntax:
 // a)
 $a = get_stuff('foo') ?? 42;
 
 // b)
 $a = get_stuff('foo') ?! 42;
 
  ### Example 4 : Chaining ###
  Old syntax [2]:
 $a = false;
 if (!empty($c) {
 $a = $c;
 } else {
 $tmp = get_stuff();
 $a = !empty($tmp) ? $tmp : false;
 }
 if ($a === false) {
 $a = !empty($c) ? $c : 42;
 }
 
  New syntax:
 $a = $c ?! get_stuff() ?! $b ?! 42;
 
  ### Example 5 : Illegal syntax ###
 $a = $d ?? $c ?! $b ?? 42; // `??` and `?!` cannot be mixed.
 
  ## References ##
  * [1]: http://code.google.com/p/php-snow/wiki/EmptyIssetOperators
  * [2]: This could also be done by nesting ternary operators, but that
  gets
 even more unreadable I think.
 



Re: [PHP-DEV] Implicit isset/isempty check on short-ternary operator

2011-04-08 Thread Hannes Landeholm
Operators should have very specific purpose and function... a ternary if
statement should just be another way to express a normal if statement.
Making the ? operator suppress the not defined error would be a poor
compromise, making PHP inconsistent. Then I'd rather have access of
non-defined array indexes return null instead (simply removing the
not-defined notice for arrays - where array_key_exists and isset would still
work like normal if you want to check for undefined).

~Hannes

On 8 April 2011 16:45, Martin Scotta martinsco...@gmail.com wrote:

 I just feels that !empty($arr['key']) or isset($arr['key']) do not express
 the real meaning, instead I would choose to write array_key_exists('key',
 $arr). It may be slower but it clearly express what I meant.

 Regarding the operators, I believe they will do more harm than good.
 To check if a variable was already defined is a bad programing habit, these
 operators will encourage that kind of things

 In the other hand, the isset check inside the condition ?: would silently
 improve not-so-well written code, and could be a good feature for the lang.

  Martin Scotta



 On Fri, Apr 8, 2011 at 11:27 AM, Hannes Landeholm landeh...@gmail.comwrote:

 I think ?! wouldn't work as an operator as it would conflict with
 ternary
 comparision + not operator. Also I don't see the point of adding an
 operator
 for empty as the function/construct itself is pretty confusing and
 non-useful as you have to memorize all the things that happen to be
 considered empty by it and those things has too align up with the things
 you want to check for by chance. (I never use it.)

 Empty-ness is just one of a billion things you might want to write a
 comparison for (and it's not even well defined). Having an operator for
 that
 would be crazy. Dealing with possibly undefined indexes/properties is a
 very
 common use case though which is why I think it deserves an operator. The
 empty comparison thing is a _separate issue_ and it can simply be dealt
 with by separating the assignment and comparison into two expressions...

 ~Hannes

 On 8 April 2011 15:19, Rune Kaagaard rumi...@gmail.com wrote:

  Dear Internals
 
  I'm very happy that this is getting some attention again. Please allow
  me to give my 2 cents too. The text below can also be seen nicely
  formatted at https://gist.github.com/909711.
 
  ## Intro ##
 
  Isset and IsNotEmpty operators have for sure been a hot topic for
 several
  years
  now and in my opinion rightly so. The non-DRY style of:
 
 $my_array['my_long_boring_key'] =
  !empty($my_array['my_long_boring_key'])
   ? $my_array['my_long_boring_key'] : 'Default value';
 $my_array['my_long_boring_key'] =
 isset($my_array['my_long_boring_key'])
   ? $my_array['my_long_boring_key'] : 'Default value';
 
  is a true day-to-day hassle and addressing this annoyance would be a
  big win for
  the PHP community as a whole. As PHP has two keywords `isset` and
 `empty`
  that
  can check for a non existing variable without throwing errors I think
 there
  should exist two assignment/ternary operators who mirror those.
 
  I have been thinking [1] about the same problem for my meta language
 Snow
  and
  also ended up using `??` as an isset operator.
 
  ## Proposal ##
  I propose that two new operators `??` (IssetOperator) and `?!`
  (NotEmptyOperator) are added. `??` mirrors `isset` and `?!` mirrors
  `!empty`.
  They are chainable ad nauseum but not with each other.
 
  They would work like this:
 
  ### Example 1 : Ternary shortcut ###
  Old syntax:
 $a = isset($b) ? $b : 42;
 $a = !empty($b) ? $b : 42;
 
  New syntax:
 $a = $b ?? 42;
 $a = $b ?! 42;
 
  ### Example 2 : Direct assignment ###
  Old syntax:
 $arr['key'] = isset($arr['key']) ? $arr['key'] : 42;
 $arr['key'] = !empty($arr['key']) ? $arr['key'] : 42;
 
  New syntax:
 $arr['key'] ??= 42;
 $arr['key'] ?!= 42;
 
  ### Example 3 : Works with statements too ###
  Old syntax:
 // a)
 $tmp = get_stuff('foo');
 $a = isset($tmp) ? $tmp : 42;
 
 // b)
 $tmp = get_stuff('foo');
 $a = !empty($tmp) ? $tmp : 42;
 
  New syntax:
 // a)
 $a = get_stuff('foo') ?? 42;
 
 // b)
 $a = get_stuff('foo') ?! 42;
 
  ### Example 4 : Chaining ###
  Old syntax [2]:
 $a = false;
 if (!empty($c) {
 $a = $c;
 } else {
 $tmp = get_stuff();
 $a = !empty($tmp) ? $tmp : false;
 }
 if ($a === false) {
 $a = !empty($c) ? $c : 42;
 }
 
  New syntax:
 $a = $c ?! get_stuff() ?! $b ?! 42;
 
  ### Example 5 : Illegal syntax ###
 $a = $d ?? $c ?! $b ?? 42; // `??` and `?!` cannot be mixed.
 
  ## References ##
  * [1]: http://code.google.com/p/php-snow/wiki/EmptyIssetOperators
  * [2]: This could also be done by nesting ternary operators, but
 that
  gets
 even more unreadable I think.
 





Re: [PHP-DEV] Implicit isset/isempty check on short-ternary operator

2011-04-08 Thread Matt Pelmear
Hello all,
First post here; been watching for a while though.

IMHO:
1) Implicit isset() checks in ?: would be bad. This would not
silently improve not-so-well written code; In fact it would make
not-so-well written code more difficult to debug. I can't count the
number of times I've run across components that are acting oddly
because somebody wrote them to ignore the fact that they're not
receiving some required parameter. At least with the current
functionality of ?: we get a warning. If the collective decision were
to implement this, could it at least have a setting to force it to
function how it does currently?

2) A shorthand assignment operator would be handy. I like the suggestion of ??=
where:
$var ??= $default;
is shorthand for
if( !isset($var) ) $var = $default;
Or, maybe similar to C#
$var = $var ?? $default;
(Correct me if I'm using this syntax incorrectly)

This would provide a clean way to set defaults for unset vars but
would make it easy to find blocks of code that are using unset vars
with the ternary operator.


-Matt

On Fri, Apr 8, 2011 at 10:45 AM, Martin Scotta martinsco...@gmail.com wrote:
 I just feels that !empty($arr['key']) or isset($arr['key']) do not express
 the real meaning, instead I would choose to write array_key_exists('key',
 $arr). It may be slower but it clearly express what I meant.

 Regarding the operators, I believe they will do more harm than good.
 To check if a variable was already defined is a bad programing habit, these
 operators will encourage that kind of things

 In the other hand, the isset check inside the condition ?: would silently
 improve not-so-well written code, and could be a good feature for the lang.

  Martin Scotta

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] Implicit isset/isempty check on short-ternary operator

2011-04-08 Thread Ben Schmidt

On 9/04/11 12:45 AM, Martin Scotta wrote:

I just feels that !empty($arr['key']) or isset($arr['key']) do not express
the real meaning, instead I would choose to write array_key_exists('key',
$arr). It may be slower but it clearly express what I meant.


I don't like this. array_key_exists will return true for an array key
that exists but is set to null, which is rarely what is wanted (though
it sometimes is).

I agree that the variants with empty() aren't very good.

Just a single isset() variant seems like a good idea. I favour the ??:
syntax.

I liked the well-thought-out examples with assignment and chaining and
so on. For chaining, does associativity matter? (Too tired to figure it
out for myself right now!)

Ben.




--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] Optional $limit argument for debug_backtrace()

2011-04-08 Thread Sebastian Bergmann
Am 04.04.2011 17:22, schrieb Sebastian Bergmann:
 Any thoughts?

 Are there any objections to applying the latest version of the patch [1]
 to trunk? I still think that debug_backtrace and debug_print_backtrace
 are in need of refactoring but that should be kept separate, I think.

 --
 [1] https://gist.github.com/901579

-- 
Sebastian BergmannCo-Founder and Principal Consultant
http://sebastian-bergmann.de/   http://thePHP.cc/

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] Implicit isset/isempty check on short-ternary operator

2011-04-08 Thread Sanford Whiteman
 First post here; been watching for a while though.

Same here.

Here's my take:

[1]  I  don't like ?? / ? because it is disjunctive with === / ==.. The
extra  equals  sign  strengthens  equality comparison, while the extra
question  mark  essentially  _weakens_ the ternary operator (making it
more forgiving). (Sure, a case could be made that an extra duplicate
operator has no inherent meaning overall, but these two operators seem
to be in a similar domain.)

[2]  I  would prefer @?. This makes the error-suppression obvious. But
this leads to...

[3]  ...  are we saying that [a] we are still suppressing a deep error
or  [b]  or that an error state does not exist anywhere when you use a
special  operator?  In  other  words,  is  it  now  going  to be fully
supported  to  use  undefined  variables,  as  long  as  we always use
operators  that  take  them  into  account?  This seems to be inviting
criticism  from other language fanboys (if we care) since they already
hold the non-fatal use of undefined variables in contempt.

[4]  Strange  idea:  what about treating the variable as defined after
you  test it with the special operator, even if you don't specifically
assign it a value?

So:

$varor2  =  $var1  @?:  2  //  $varor2==2,  no warning because special
ternary is used

$varor3  =  $var1  ?: 3 // $varor3=3, but no warning because $var1 has
been implicitly defined in special expression

[5]  Sort  of  the opposite of [4] and continuing the question of [3],
what  about  only letting a variable be special-op-checked once before
throwing  a  warning. So you can suppress (or not throw) the undefined
warning  once,  but it is assumed that you will define the variable in
that  statement.  So  this  would discourage the continuous use of the
same  undef var throughout code (which seems like a code smell), while
allowing  for the real case that you're using the special-op to filter
$_REQUEST or whatever -- one time.

I  haven't  really  thought about the edges, but thought I'd put those
out there.

Cheers,

Sandy


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] Optional $limit argument for debug_backtrace()

2011-04-08 Thread Patrick ALLAERT
2011/4/8 Sebastian Bergmann sebast...@php.net:
 Am 04.04.2011 17:22, schrieb Sebastian Bergmann:
 Any thoughts?

  Are there any objections to applying the latest version of the patch [1]
  to trunk? I still think that debug_backtrace and debug_print_backtrace
  are in need of refactoring but that should be kept separate, I think.

Not that this is a true blocker, but as said previously, and
considering the very few changes it introduces
(https://gist.github.com/910580), I'm not really in favor to introduce
it only in debug_bactrack() and not in debug_print_backtrace().
Both should be refactored, true, but I don't see the point to make them diverge.
I would even say that there is more benefit in terms of maintenance to
make them common now than introducing new feature to them.

Regards,
Patrick

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php