2

Why does false[0] not throw an undefined offset error? (and should I count on this behavior)

Background Story

I am sending the options object of a Vuetify datatable component to a php backend. The backend is slim3.

I noticed that the query parameter for sortDesc looked like: api/search/order?sortDesc[]=true

I didn't even know you could pass an array in a URL parameter! It's an array because multisort is possible, but I will never use it. Well, there are three possibilities being: sortDesc[0]=true, sortDesc[0]=false, and sortDesc not present at all. I figured out the parameter may not be present after I made the change to my code to look at the zero index of the parameter like this:

$options['orderDir'] = $request->
    getQueryParam('sortDesc', $default = false)[0] ? 'DESC' : 'ASC';

I realize this isn't the best way to do this parameter init/check, but now I'm wondering why I'm not getting an exception when the parameter isn't present, so I made a test an executed the code echo false[0]; and sure enough, no error.

These also do not throw an error

echo 'some thing' . false[0]
echo 'some thing' . false[20]
echo false[-10]
echo 'some thing' . true[0]

I could not find any documentation or write up detailing this behavior and was wondering if anyone had some incite. Especially whether I should count on this behavior.

Kenneth
  • 535
  • 2
  • 17
  • You are correct. My search skills are apparently lacking! – Kenneth Oct 10 '19 at 04:27
  • 2
    Don't delete this question as it will only help to flesh out the search terms :) – Phil Oct 10 '19 at 04:28
  • 2
    FYI: this issue will be fixed with PHP 7.4, it will throw an `E_WARNING` message, see more [here](https://core.trac.wordpress.org/ticket/47704) – catcon Oct 10 '19 at 04:33

0 Answers0