Thursday February 9, 2012 @ 06:58:52 GMT+10    ( Weather:  n/a )
Home » Weblog Archives

Info *NOTE* This page is NOT the actual weblog entry. The purpose for this page is to provide extra background information on how this particular entry is constructed, by using custom markups and tags. To read the actual content of this entry, please go to here.

The Raw Structure:

Markup | Tag used: BBCode


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:
[code=lang:php|highlight]
// function declaration with default value
function foo($bar = 'default value here'){ ... };
// function declaration with passing by reference
function foo(&$bar){ ... };
[/code]

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:
[code=lang:php|highlight]
// This won't work in PHP < 5
function foo(&$bar = 'default value here'){ ... };
[/code]

[b]Workaround :[/b]
Set [b]allow_call_time_pass_reference[/b] 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.

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

Base on the above code:
[box]
[h=3]PHP < 5 complains:[/h]
Parse error: parse error, expecting ')' and later on by a fatal error and killing the script execution.

[h=3]PHP >= 5 complains:[/h]
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.
[/box]

Live Rendering:

The real-time rendered result...

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.
$ view_blog.php 2009.09.17 18:16:41 $
Lost? | XML/HTML sitemap | Contact
38.107.179.244 , 23 queries , 0.2916s
Gzip enabled , CSS compressed , JS compressed
Copyright © 2005-2011 Darren's Outpost