Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why do you put constants first when using if in PHP

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "PHP when using if why to put the constant in front", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "why the constant should be put in front of the PHP when using if" bar!

Why is it recommended to put constants first when using if in PHP?

In some frameworks or handwritten code, we will find that many people like to write constants first when judging conditions, such as:

If (1 = = $a) {

Echo 111

}

What's the advantage of doing this? Let's assume that an inadvertent carelessness will lead to the absence of an = sign.

$a ='a'

If ($a ='b') {

Echo 111

}

Echo $a

Yes, 111 is output, and the value of $a becomes b. Without an equal sign, it becomes an assignment operation, which assigns a value to $an and then judges based on the value of $a. If $a ='', 111 will not be output, but the value of $a will still become''.

Highlight: this way of writing php will not be wrong, which is also possible to cause BUG.

And the other way around?

$a ='a'

If ('b' = $a) {

Echo 111

}

First of all, most IDE will report syntax errors, that is, directly draw a red line. Constants cannot be assigned and modified, whether they are numbers, strings, the system or constants that we have defined ourselves.

Second, in this case, it doesn't matter if you can't find a problem here, it will report an error when it runs, and the code can't go down.

Of course, this is just a small trick, and the main purpose is to deal with elaborate problems. So it's not a mandatory specification, and some companies may emphasize it in code audits or specification documents, and of course, it's best to put an end to such careless errors.

Test code: https://github.com/zhangyue0503/dev-blog/blob/master/php/201911/source/PHP%E4%B8%AD%E4%BD%BF%E7%94%A8if%E7%9A%84%E6%97%B6%E5%80%99%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BB%BA%E8%AE%AE%E5%B0%86%E5%B8%B8%E9%87%8F%E6%94%BE % E5%9C%A8%E5%89%8D%E9%9D%A2%EF%BC%9F.php

Thank you for your reading, the above is the content of "Why do you put constants first when using if in PHP?" after the study of this article, I believe you have a deeper understanding of why you should put constants in front of PHP when using if, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report