Detailed explanation of php custom/system/class constant code examples

伊谢尔伦
Release: 2023-03-11 22:22:02
Original
1698 people have browsed it

Let’s look at a piece of code first:

 "; //第三个参数为 true ,对大小写不敏感 define("GREETING", "Welcome to W3School.com.cn!", true); echo "对大小写不敏感:".greeting."
"; //(2)、系统常量: // PHP_VERSION:当前解析器的版本号 // PHP_OS:执行当前PHP版本的操作系统名称 // 可以直接拿来使用,例如要查看执行当前PHP版本的操作系统名称,就可以写成 echo PHP_OS // 实例代码: echo "
"; //输出当前解析器的版本号 echo "当前解析器的版本号是: ".PHP_VERSION; echo "
"; //输出执行当前PHP版本的操作系统名称 echo "当前操作系统是: ".PHP_OS; echo "
"; // 输出圆周率 echo M_PI; echo "
"; // 输出科学常数e echo M_E; echo "
"; // 输出以2为底e的对数 echo M_LOG2E; echo "
"; // 输出以10为底e的对数 echo M_LOG10E; echo "
"; // 输出2的自然对数(常数e为底数的对数叫做自然对数) echo M_LN2; echo "
"; // 输出10的自然对数 echo M_LN10; echo "
"; //(3)、php类常量 class MyClass{ //类常量,用constant修饰,不带 const cons1 = 'constant value'; function showCons1() { echo self::cons1."
"; } } //常量的表示形式: 类名::常量名 echo "
类常量值为:".MyClass::cons1."
"; ?>
Copy after login

1. If two identical constants are defined, which one will work, the former or the latter?
The former works because a constant cannot be redefined or undefined once it is defined.

2.What are the differences between constants and variables?
1) There is no $ sign before constants;
2) Constants can only be defined through define(), not assignment statements;
3) Constants can be defined and accessed anywhere, while variables have global and local;
4) Once a constant is defined, it cannot be redefined or undefined, while a variable is redefined through assignment;
5) The value of a constant can only be scalar data, and the database type of the variable There are 8 primitivedata types.

The above is the detailed content of Detailed explanation of php custom/system/class constant code examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn