Why does false[0] not throw an error for undefined offset?

Viewed 23

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.

0 Answers
Related