Khai báo hằng
Cú pháp : define(name, value, case-insensitive)
- name: tên hằng
- value : giá trị hằng
- case-insensitive: mặc định fasle là phân biệt hoa với thường.
Ví dụ :
<!DOCTYPE html>
<html>
<body>
<?php
// case-sensitive constant name
define("Hi", "Welcome to CodeWR.com!");
echo Hi;
?>
</body>
</html>
Kết quả
Welcome to CodeWR.com!
Ví dụ 2:
<!DOCTYPE html>
<html>
<body>
<?php
// case-insensitive constant name
define("Hi", "Welcome to CodeWR.com!", true);
echo hi;
?>
</body>
</html>
Kết quả:
Welcome to CodeWR.com!
Hằng được xem là biến toàn cục.
Ví dụ 3:
<!DOCTYPE html>
<html>
<body>
<?php
define("Hi", "Welcome to CodeWR.com!");
function test() {
echo Hi;
}
test();
?>
</body>
</html>
Kết quả
Welcome to CodeWR.com!