biscuitstack biscuitstack - 1 year ago 170
PHP Question

PHP str_replace argument syntax error

My argument for str_replace is throwing a

PHP Parse error: syntax error, unexpected '['
on my remote PHP 5.3. Locally on my PHP 5.4 it works fine. I've seen elsewhere here that this is a known issue but I can't figure out a way around it with my code.

$removeChars = ['"','[',']'];
$orderListDeQuoted = str_replace($removeChars, "", $orderList);

This is preventing an ajax call from working and throwing a server 500 error.

Answer Source

PHP 5.3 does not support the short array syntax.

To make your code work in PHP 5.3, you need to replace the [ ... ] array syntax with array( ... ).

$removeChars = array('"','[',']');

However, it is also important that you are aware that PHP 5.3 is no longer supported -- it has been out of support for quite a long time now, so if your web host is still only offering PHP 5.3, then you need to be asking them to upgrade, and if they don't then you need to be considering finding a better quality web host.

PHP 5.4 is also out of support as well, but not as long ago as 5.3. Ideally you should be upgrading to 5.5 or 5.6. (or even PHP 7.0, but I appreciate that is quite a big jump!)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download