Saturday February 11, 2012 @ 10:50:54 GMT+10    ( Weather:  n/a )
Home » Weblog Archives

Powered byD's Bloggie
Weblog » Archive » 2005 » October » Day 16 » PHP Passing by Reference
Browse by specific entry ...
 Changing WeatherWeblog Archive 

Last edited: Mon 2005-10-17 @ 19:01 , by DaRen 1 time(s)

PHP Passing by Reference - 3:07 pm
Bug , Coding , PHP  -  poster 

PHP's function supports passing by reference. However, default values may ONLY be passed by reference starting from PHP >= 5. Default values in PHP functions are like:
[ Hide ]
[ Highlight ] [ Text ]
// function declaration with default value
function foo($bar = 'default value here'){ ... };
// function declaration with passing by reference
function foo(&$bar){ ... };


What pissed me off is that I'm using PHP 5 for my computer but the hosting server is using PHP4. I only realize that PHP 4 doesn't supports that after I uploaded all my files to the server. Example of default values passed by reference:
[ Hide ]
[ Highlight ] [ Text ]
// This won't work in PHP < 5
function foo(&$bar = 'default value here'){ ... };


Workaround :
Set allow_call_time_pass_reference to true in php.ini if you're using php >= 5 so that you won't get a bunch of warning messages filling your screen.

Trick to do default values passed by reference [ Hide ]
[ Highlight ] [ Text ]
<?
// function declaration
function foo($bar = 'default value here') { ... };
// function calling
foo(); // valid call
foo(&$var); // this, however is consider deprecated in PHP >= 5
?>


Base on the above code:

PHP < 5 complains:

Parse error: parse error, expecting ')' and later on by a fatal error and killing the script execution.

PHP >= 5 complains:

Warning: Call-time pass-by-reference has been deprecated - argument passed by value; If you would like to pass it by reference, modify the declaration of [runtime function name](). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file. However, future versions may not support this any longer.
 Changing WeatherWeblog Archive 

Reader comments ( Add your comments )

- No comments (yet) -

Leave a public comment

Info Note: Neither BBCode nor HTML tags are supported currently.
Why ? Just to make my life easier and keep things simple.
Others: Comments that are longer than 1000 characters will be truncated.

    ) Info

Info

Info


Before you submit... verification

Please enter verification codes as seen in the image above to post your comment.
If you can't read the word, click here
(Your comment will be manually approved by admin before it's displayed to public)

$ view_blog.php 2009.09.17 18:16:41 $
Lost? | XML/HTML sitemap | Contact
38.107.179.241 , 22 queries , 0.18s
Gzip enabled , CSS compressed , JS compressed
Copyright © 2005-2011 Darren's Outpost