2

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?

Qirel
  • 25,449
  • 7
  • 45
  • 62
S55
  • 45
  • 6
  • 1
    Two words: error reporting ;-) – Álvaro González Jul 05 '19 at 19:45
  • 2
    It won't give an error unless you run it directly, because it sends it's error message on it own interface - not yours. If you don't want to send a variable, you could use `function updateCustomer($uniqueid,$name,$title,$job = null)`. – kry Jul 05 '19 at 19:49
  • PHP will give you "*Fatal error: Uncaught ArgumentCountError: Too few arguments to function updateCustomer()*" if you don't give it all the parameters expect. You must be doing something different (like an optional parameter (that has a default value)). – Qirel Jul 05 '19 at 19:50
  • From the error log. [05-Jul-2019 15:49:18 America/New_York] PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function updateCustomer(), 3 passed in – S55 Jul 05 '19 at 19:51
  • Exactly - thus, you need to give `$job` a default value (like `$job = null` in the declaration). – Qirel Jul 05 '19 at 19:52
  • I understand just sending another variable makes it work. But why does it work on one machine and not another? Is this a php setup difference or did something change from version 7.0 to 7.2. Because it works flawlessly on the raspberry running 7.0 – S55 Jul 05 '19 at 19:54

2 Answers2

8

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)
jnadon
  • 126
  • 5
  • But if this was just an error reporting issue why would everything work on the raspberry (nothing in the error log) and get the white page of death on the other? – S55 Jul 05 '19 at 19:56
  • updated my post. Looks like changes between php 7.0 and 7.1. – jnadon Jul 05 '19 at 19:57
  • @jnadon To err is mortal. To fix it, divine. – kry Jul 05 '19 at 19:59
2

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) {
   // ..
}
Qirel
  • 25,449
  • 7
  • 45
  • 62
  • That's what I was looking for! I had it fixed but really didn't understand what was going on. – S55 Jul 05 '19 at 19:58