PHP warning riddle: Can only flip STRING and INTEGER values

Every once in a while I go over the Drupal watchdog log to verify everything is working as should, find warnings and errors and fix them. Once in a while the following warning was logged:

Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 173 of /data/www/includes/entity.inc).

The warning was logged from one of the cron tasks and happened sporadically, i.e. it didn’t always happen. I have had such warnings in the past and it usually was a result of a task that tried to node_load with a NULL nid.

To further understand what happens I added trace debug code to entity.inc to track the errors. Here is the code:

Debug code added to entity.inc immediately after problematc array_flip()

To my surprise I got the following watchdog log:

wrong params during entity load ids: Array ( [0] => 156874 )

I won’t quote the debug trace here, but it was consistent with the above and let to a seemingly innocent function. The parameter logged is not NULL for sure.

Code runs on PHP 5.3.10

Since the warning didn’t seem to cause much damage, I didn’t put a lot of effort to figure what is going on here, but it kept on bugging me. Eventually I did the right thing and asked a colleague to help. So thank you David First for coming up with the answer.

But what is causing the warning here? Do you have an explanation?

Scroll down for the answer

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

Some more scrolling…

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

Its always simple once you know the answer. Its not a string, its not an integer, so it must be a float.

Since print_r doesn’t print the type (like var_dump), this wasn’t obvious from the watchdog log.

Its very confusing that print_r prints a float as an integer (156874 and not 156874.0)

And how in the first place the nid got to become a float? The nid was calculated, and part of the calculation included a call to floor. Turns out that floor returns a float (which isn’t obvious either), and once you got a float, the type remains float after any integer operation.

And last rant: it would have saved me a lot of time if the PHP warning itself simply indicated the type: Can only flip STRING and INTEGER values. Float given!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.