Jump to content

PHP manual has a confusing piece of code for Passing by reference..


Slips

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :D

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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 :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.