变量还是常量?
使用其中一种比另一种有好处吗?
$lang = isset($_COOKIE['lang']) ? $_COOKIE['lang'] : 'en';
# OR
define("LANG" , isset($_COOKIE['lang']) ? $_COOKIE['lang'] : 'en');
谢谢
Is there a benefit of using one over the other ?
$lang = isset($_COOKIE['lang']) ? $_COOKIE['lang'] : 'en';
# OR
define("LANG" , isset($_COOKIE['lang']) ? $_COOKIE['lang'] : 'en');
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下,常数更合适。由于您希望在整个应用程序中使用相同的值,并且您不希望它可能被错误地更改。
请注意,编程语言有许多不需要用于实现算法的功能,但它们的作用是使算法更具可读性、可维护性并且不易出现错误。常量就是这类特征之一。
In this case a constant is more appropriate. As you want to use the same value through your entire application, and you don't want it possibly changed by mistake.
Do notice programing languages have many features you don't need to use to implement an algorithem, but rather they are there to make the algorithem more readable, maintainable and less prone to get bugs. Constants are one of those type of features.
常量:
变量:
Constants:
Variables:
这取决于你想做什么。常量的值一旦定义就不能更改。变量可以。这种差异应该让您选择您需要的。
It depends on what you want to do. The value of the constant can't be change once it's defined. The variable can. This difference should make you choose the one you need.