简体   繁体   English

如何在 PHP 中定义常量

[英]How to define constant in PHP

I do not understand how this code work.我不明白这段代码是如何工作的。 displaying code below , in this code i defined a constant and the value is 10 but it display the output "constant is not defined".显示下面的代码,在这段代码中,我定义了一个常量,值为 10,但它显示输出“常量未定义”。

why?为什么?

define('HELLO', 10);        
if(defined(HELLO))        
{        
    echo "Constant is defined";        
}        
else        
{        
    echo "Constant is not defined";        
}        

?>

change: 更改:

defined(HELLO)

to: 至:

defined('HELLO')

http://php.net/manual/en/function.defined.php http://php.net/manual/zh/function.defined.php

Define Constant in PHP:在 PHP 中定义常量:

define("HELLO", 10);

If condition needs to change, then: defined("HELLO")如果条件需要改变,则: defined("HELLO")

Even you can follow this way of checking and defining constant in PHP.甚至您也可以按照这种方式在 PHP 中检查和定义常量。

defined('CONSTANT') or define('CONSTANT', 'SomeDefaultValue');

For your question, you have made a mistake of quote .对于您的问题,您在quote犯了一个错误。 follow as :如下:

if(defined('HELLO')){
  echo "Constant is already defined";
}else{
  echo "Constant is not defined, you can define it.";
}

https://www.php.net/manual/en/function.defined.php#84439 https://www.php.net/manual/en/function.defined.php#84439

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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