Slips Posted November 4, 2010 Share Posted November 4, 2010 Hi, I'm just trying out some basic code and playing around with passing variables by reference and i was reading this on the php manual at this page http://php.net/manual/en/language.references.pass.php : No other expressions should be passed by reference, as the result is undefined. For example, the following examples of passing by reference are invalid: <?php function foo(&$var) { $var++; } function bar() // Note the missing & { $a = 5; return $a; } foo(bar()); // Produces fatal error since PHP 5.0.5 foo($a = 5); // Expression, not variable foo(5); // Produces fatal error ?> So, i decided to try it out myself like i always do, and i noticed that i'm not getting an error when i do foo(bar()); i.e calling bar() without the & in the function declaration. Infact it works perfectly fine and returns an incremented $a after its passed to foo(). Likewise foo($a = 5); also works great and returns an incremented $a after being passed to foo(). Is this a mistake in the manual or am i missing something? Running PHP 5.3.2-1ubuntu4.5 Quote Link to comment https://forums.phpfreaks.com/topic/217724-php-manual-has-a-confusing-piece-of-code-for-passing-by-reference/ Share on other sites More sharing options...
Airzooka Posted November 4, 2010 Share Posted November 4, 2010 Is your version of the script any different than the one you posted? If it is, post it. Quote Link to comment https://forums.phpfreaks.com/topic/217724-php-manual-has-a-confusing-piece-of-code-for-passing-by-reference/#findComment-1130161 Share on other sites More sharing options...
salathe Posted November 4, 2010 Share Posted November 4, 2010 Is this a mistake in the manual or am i missing something? You're probably missing something, mistakes in the manual do happen but it doesn't look to be the case this time. Make sure that error reporting is turned on (the simplest way is to put error_reporting(-1); ini_set('display_errors', true); at the top of your script) when you're trying to figure out problems with code. Quote Link to comment https://forums.phpfreaks.com/topic/217724-php-manual-has-a-confusing-piece-of-code-for-passing-by-reference/#findComment-1130162 Share on other sites More sharing options...
Slips Posted November 4, 2010 Author Share Posted November 4, 2010 Wow, turns out my error_reporting was set to 22527, not -1. All this while it was throwing out other regular errors - parse,fatal errors etc.. but for the first time i'm seeing "Strict Standards :.." .So im guessing -1 is the highest level or error reporting that pukes out all errors possible?. Maybe the PHP manual should specify that this error is shown only if error_reporting to -1 because its really confusing when the exact piece of code works fine without a -1 error_reporting level, confusing me into thinking that my code is fine because it outputs perfectly fine (except for foo(5) that gives me a fatal error). Even if it didn't output anything that would have atleast given me a clue that something might be wrong at the error reporting level. Thanks for the help anyways guys Quote Link to comment https://forums.phpfreaks.com/topic/217724-php-manual-has-a-confusing-piece-of-code-for-passing-by-reference/#findComment-1130166 Share on other sites More sharing options...
salathe Posted November 4, 2010 Share Posted November 4, 2010 It looks like your error reporting level was E_ALL & ~E_DEPRECATED --- this level does not include E_STRICT (since it is not part of E_ALL). For what it's worth, the manual page for error_reporting() says (in a big "Tip" box): Passing in the value -1 will show every possible error, even when new levels and constants are added in future PHP versions. Quote Link to comment https://forums.phpfreaks.com/topic/217724-php-manual-has-a-confusing-piece-of-code-for-passing-by-reference/#findComment-1130169 Share on other sites More sharing options...
Slips Posted November 4, 2010 Author Share Posted November 4, 2010 Yea yeah, i read that part in the page of error_reporting(), infact i even remember writing a function in my profiling class to change it to -1. But it completely slipped my mind when i was testing it on a blank php page with just that standalone code since its not mentioned in the php manual about passing by reference.. anyways thats that, i guess this is the good way to learn Quote Link to comment https://forums.phpfreaks.com/topic/217724-php-manual-has-a-confusing-piece-of-code-for-passing-by-reference/#findComment-1130186 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.