简体   繁体   中英

Is it safe to use $_ prefix on custom PHP variables

I know it's possible to set custom PHP variables using the $_ prefix, however I am unsure if it is safe to do this? I wouldn't be setting many, nor would I be overwriting pre-set variables like $_SERVER or $_COOKIE. Should I just avoid doing it at all?

It is completely safe to initialize variables that start with $_ as long as you don't override any of the superglobals (that you need).

However, the $_ prefix on variables indicates that they are superglobals or have some other kind of magic behavior, so to do so on "normal" variables would be nonstandard.

This notation is normally used inside classes to indicate private or protected members (either properties or methods). In this situation it is completely safe.

Note, the php manual has a caution on using double underscore to prefix methods, as this notation is reserved for "magic" methods. This caution does not apply to the single underscore prefix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM