PHP WARNING: USE OF UNDEFINED CONSTANT [SOLUTION]

ASSUMED 'XXXX' (THIS WILL THROW TO ERROR IN A FUTURE VERSION OF PHP)

    Inhaltsangabe
  1. The solution
    1. Search
    2. Replace
  2. Conclusion

After the last update of our php version we got a warning. This warning was that caused because we did not use quotation marks when specifying the associative array keys. For example, we wrote $section[title] instead of $section[‘title’] .

I solved this problem by simply search and replace. You can also apply this method to projects , if your editor supports a regex search and replace.

The solution

I use the atom editor and would recommend this editor as well. If you disagree, please write me.

Search

$([a-zA-Z]+)[([a-z_A-Z0-9]+)]

In your search, you should enter the above snippet. Please make sure that the regex search is activated. It is searched for the following:

  • Dollar sign
  • Square bracket on
  • Letters, numbers and underscores
  • Square bracket close

The search stores the variable name and the associative array key for reuse.

Replace

The string that we enter when replacing is much easier to understand. The array is rebuilt, but this time quotes are used when specifying the key. The snippet looks like this:

$$$1['$2']

Conclusion

This solution has already helped me on several projects and thanks to the REGEX search I have saved a lot of time. The problem was that specifying non-quoted array keys would appear to be a constant.

Topics

php Program technology Tips

Beitrag teilen

WhatsApp it

Folgen Sie uns