Page 1 of 1

php 4.3.11 compat problems?

Posted: Tue Sep 20, 2005 4:10 pm
by programmermatt
Does anyone know why I get parse errors in 4.3.11 for the following:

Code: Select all

function test( &$param1 = null ) {

}
and

Code: Select all

$string = "This is a {$daystatus} day!";
The first complains that it hit a '=' when it was expecting a ')'. But if I remove the '&' to make it not passed by reference it functions.

The second complains about an unexpected '{'.

My development platform is php5 and my production enviornment is 4.3.11, so that is why I attempt to use 4.3.11 coding, but I was reasonably sure that these aren't php5. The online manual as far as I can tell doesn't state what version these features were added.

Posted: Tue Sep 20, 2005 4:17 pm
by John Cartwright

Code: Select all

function test( &$param1 = null ) {
$param1 is already passed by reference, no need.
As for your other, why do have have curly brackets around your varname. Double quotes allows you to parse variables within the string.
There isn't a parse error so I don't really know what to say, except it must be another line nearby.

Posted: Tue Sep 20, 2005 6:31 pm
by Jenk
I'm pretty sure you must define variables within functions in PHP5, similar to how you do in classes.

Having

Code: Select all

function test ($var) {
is fine, but to do what you want to above, you must use:

Code: Select all

function test () {

    global $param1;
I heartily recommend you do not develop on PHP5, if productionis PHP4.x, install PHP4.x and use that to develop, there are too many differences. (Hence it becoming PHP5 and not staying as PHP4.x.x)

Posted: Tue Sep 20, 2005 7:01 pm
by John Cartwright
Jenk wrote:I'm pretty sure you must define variables within functions in PHP5, similar to how you do in classes.
Of course you do if you want to use it in the function scope. I was refering to pass with by reference or not.

I'm pretty sure you must define variables within functions in PHP5, similar to how you do in classes.

Code: Select all

function test (&$var) {
vs.

Code: Select all

//php5 passes by reference automatically
function test ($var) {

Posted: Tue Sep 20, 2005 9:36 pm
by programmermatt
Jcart wrote:

Code: Select all

function test( &$param1 = null ) {
$param1 is already passed by reference, no need.
As for your other, why do have have curly brackets around your varname. Double quotes allows you to parse variables within the string.
There isn't a parse error so I don't really know what to say, except it must be another line nearby.
No, I need to pass $param1 by reference, but I get an error when I put '( &$param1 = null )'. I can remove the '&' and it produces no error, but the variable isn't by reference anymore. I cannot use global scope for reference handeling because what is being passed to it could be any random variable name. Also, I am pretty sure all variables are passed by value in php5 by default.

The other is a matter of coding preference, but I guess it can change...

Something just hit me, maybe php4.3 doesn't allow function declarations of by-ref, but instead you pass by-ref when calling the function:

Code: Select all

function test( $a ) {
$a++;
}
$b = 5;
test( $b );
test( &$b );
echo $b;
Though that is against php5's methods of doing things, and I think it will raise a warning...

Posted: Tue Sep 20, 2005 10:43 pm
by feyd
php4 does not allow support for setting a default value to a reference, while php5 does.