php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41542 The '@' operator changes the behaviour of the code
Submitted: 2007-05-30 15:11 UTC Modified: 2007-05-30 16:33 UTC
From: linfo2003 at libero dot it Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.2.2 OS: WindowsXP
Private report: No CVE-ID: None
 [2007-05-30 15:11 UTC] linfo2003 at libero dot it
Description:
------------
The '@' operator to not only suppresses non-fatal errors, but it seems that it changes the behaviour of the code.

I don't know if it's a bug or a normal behaviour.

The fact is that if you pass by reference an undefined index of an array to a function, that index will be defined and set to NULL.

While if you use the '@' operator, this won't happen.

See the code.

Reproduce code:
---------------
  error_reporting(E_ALL);

  print '<pre>';

  function byVal( $v) {}
  function byRef(&$v) {}

  echo "byVal(first['undefined'])\n";
  byVal ($first['undefined']);          // gives a notice
  echo "var_dump(first)\n";
  var_dump($first);                     // gives a notice

  print '<hr />';
	
  echo "byRef(second['undefined'])\n";
  byRef ($second['undefined']);         // does NOT give a notice
  echo "var_dump(second)\n";
  var_dump($second);                    // does NOT give a notice

  print '<hr />';

  echo "byRef(@third['undefined'])\n";
  byRef (@$third['undefined']);         // does NOT give a notice
  echo "var_dump(third)\n";
  var_dump($third);                     // gives a notice


Expected result:
----------------
The $second or the $third case should be wrong, IMHO.

AKAIK, the '@' operator should only suppress the notice, while it's changing the behaviour of the code, by not defining the $third array and not defining the 'undefined' index into the $third array.

I don't know if the $second case is an expected behaviour, maybe it should NOT add the 'undefined' index into the (undefined) $third array.

However, is a fact that the '@' operator makes something unexpected: it does NOT only suppress the notice.

Actual result:
--------------
byVal(first['undefined'])


Notice:  Undefined variable: first in ... on line 12

var_dump(first)


Notice:  Undefined variable: first in ... on line 14

NULL
-----------------------------------------------------
byRef(second['undefined'])
var_dump(second)
array(1) {
  ["undefined"]=>
  NULL
}
-----------------------------------------------------
byRef(@third['undefined'])
var_dump(third)


Notice:  Undefined variable: third in ... on line 28

NULL

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-05-30 15:38 UTC] tony2001@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Passing a variable by reference is expected to create it.
 [2007-05-30 15:43 UTC] linfo2003 at libero dot it
Then it's the $third case that is wrong.

byRef(@$third['undefined']) does NOT creates that index.

Should the '@' operator only suppress the notice?
Then that index should be added to the $third array.

It isn't added.

Is this a normal behaviour?

Thanks.
 [2007-05-30 16:00 UTC] tony2001@php.net
@$var is not a variable, it's an expression.
So you end up passing result of this expression to the function, not the variable itself.
 [2007-05-30 16:06 UTC] linfo2003 at libero dot it
But AFAIK, only *variables* can be passed by reference.

However, if you say it's a normal behaviour, I'll trust you.

Thank you for your courtesy.
 [2007-05-30 16:10 UTC] tony2001@php.net
>But AFAIK, only *variables* can be passed by reference.
Exactly. Add E_STRICT to the error level and you'll see a message about it.
 [2007-05-30 16:33 UTC] linfo2003 at libero dot it
You're totally right.
I thought it was raised a fatal error, not just a strict message, when passing an expression instead of a variable where a variabile reference is expected, so I hadn't used E_STRICT.

Thank you very much!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 11:01:30 2024 UTC