php 4.3.11 compat problems?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
programmermatt
Forum Commoner
Posts: 65
Joined: Tue Mar 15, 2005 5:03 pm
Contact:

php 4.3.11 compat problems?

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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) {
programmermatt
Forum Commoner
Posts: 65
Joined: Tue Mar 15, 2005 5:03 pm
Contact:

Post 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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

php4 does not allow support for setting a default value to a reference, while php5 does.
Post Reply