Reputation: 45
I have an issue passing a variable to a PHP function. I'm running a Raspberry Pi webserver with PHP 7.0.33. Everything runs fine on the raspberry. When I upload my pages to my Godaddy server which is running PHP 7.2 I get the dreaded white page of death. I traced it down to the following. This is simplified.
On the raspberry:
This is how I'm sending the variables.
updateCustomer($uniqueid, $name, $title);
This is how I receive them in the function.
function updateCustomer($uniqueid, $name, $title, $job){
}
On the raspberry I send 3 vars ($uniqueid
, $name
, $title
). The function is looking for 4 vars ($uniqueid
, $name
, $title
, $job
) but ignores the last one ($job
) if it doesn't exist.
This will not work on the Godaddy server unless I send 4 vars and receive 4 vars. So for testing I just plugged in $x
like this and it works.
function updateCustomer($uniqueid, $name, $title, $x){
}
So my question...Is this a function difference between PHP 7.033 and the 7.2 that's running on Godaddy? Or is there a setting within the PHP setup that would allow this to work?
Upvotes: 2
Views: 64
Reputation: 26450
Yes, there was a change between PHP 7.0 and PHP 7.1.
Previously (PHP <=7.0), a warning would be emitted for invoking user-defined functions with too few arguments. Now (PHP >=7.1), this warning has been promoted to an Error exception. This change only applies to user-defined functions, not internal functions.
Which is what you're seeing in effect - it was changed from a warning (so it works, no errors - just a message), to an actual error.
The solution is to simply fix it, by adding a default value to the parameter, thereby making it optional.
function updateCustomer($uniqueid, $name, $title, $job = null) {
// ..
}
Upvotes: 2
Reputation: 126
The difference between the two is likely related to the PHP error reporting/logging configuration on both machines.
EDIT: looks like php 7.1 promoted the too few arguments warning to an error. https://php.net/manual/en/migration71.incompatible.php
As for the fourth parameter, you can give it a default value of null so that only 3 parameters are required.
function updateCustomer($uniqueid, $name, $title, $job = null)
Upvotes: 8