Изучение PHP на практических примерах
  256ПрограммированиеPHPИспользование define

Использование define. Один и тот же код PHP 5, 7, 8 даёт разный результат

Начиная с PHP 7.3.0, определение нечувствительных к регистру констант объявлено устаревшим и удалено в версии 8.0.0, в результате ранее работавший код будет приводить к фатальным ошибкам.

Пример 1

<?php
error_reporting(E_ALL);
echo '<p>Версия PHP ', phpversion(), '</p>';
define("TEXT", "Здравствуй, мир.");
echo TEXT, '<br>';
echo text, '<br>';
?>

Результаты:

Версия PHP 5.6.40

Здравствуй, мир.
Notice: Use of undefined constant text - assumed 'text' in G:\server\php-test\p1.php on line 6
text

Версия PHP 7.1.33

Здравствуй, мир.
Notice: Use of undefined constant text - assumed 'text' in G:\server\php-test\p1.php on line 6
text

Версия PHP 7.2.34

Здравствуй, мир.
Warning: Use of undefined constant text - assumed 'text' (this will throw an Error in a future version of PHP) in G:\server\php-test\p1.php on line 6
text

Версия PHP 7.4.27

Здравствуй, мир.
Warning: Use of undefined constant text - assumed 'text' (this will throw an Error in a future version of PHP) in G:\server\php-test\p1.php on line 6
text

Версия PHP 8.0.14

Здравствуй, мир.
Fatal error: Uncaught Error: Undefined constant "text" in G:\server\php-test\p1.php:6 Stack trace: #0 {main} thrown in G:\server\php-test\p1.php on line 6

Версия PHP 8.1.1

Здравствуй, мир.
Fatal error: Uncaught Error: Undefined constant "text" in G:\server\php-test\p1.php:6 Stack trace: #0 {main} thrown in G:\server\php-test\p1.php on line 6

Пример 2

<?php
// error_reporting(E_ALL);
echo '<p>Версия PHP ', phpversion(), '</p>';
define("TEXT", "Здравствуй, мир.");
echo TEXT, '<br>';
echo text, '<br>';
?>

Результаты:

Версия PHP 7.1.33

Здравствуй, мир.
text

Версия PHP 7.2.34

Здравствуй, мир.
Warning: Use of undefined constant text - assumed 'text' (this will throw an Error in a future version of PHP) in G:\server\php-test\p1.php on line 7
text

Версия PHP 8.0.14

Здравствуй, мир.
Fatal error: Uncaught Error: Undefined constant "text" in G:\server\php-test\p1.php:6 Stack trace: #0 {main} thrown in G:\server\php-test\p1.php on line 6

Пример 3

<?php
error_reporting(E_ALL);
echo '<p>Версия PHP ', phpversion(), '</p>';
define("TEXT", "Здравствуй, мир.", true);
echo TEXT, '<br>';
echo text, '<br>';
?>

Результаты:

Версия PHP 5.6.40

Здравствуй, мир.
Здравствуй, мир.

Версия PHP 7.1.33

Здравствуй, мир.
Здравствуй, мир.

Версия PHP 7.2.34

Здравствуй, мир.
Здравствуй, мир.

Версия PHP 7.4.27

Deprecated: define(): Declaration of case-insensitive constants is deprecated in G:\server\php-test\p1.php on line 4
Здравствуй, мир.
Deprecated: Case-insensitive constants are deprecated. The correct casing for this constant is "TEXT" in G:\server\php-test\p1.php on line 6
Здравствуй, мир.

Версия PHP 8.0.14

Warning: define(): Argument #3 ($case_insensitive) is ignored since declaration of case-insensitive constants is no longer supported in G:\server\php-test\p1.php on line 4
Здравствуй, мир.
Fatal error: Uncaught Error: Undefined constant "text" in G:\server\php-test\p1.php:6 Stack trace: #0 {main} thrown in G:\server\php-test\p1.php on line 6

Версия PHP 8.1.1

Warning: define(): Argument #3 ($case_insensitive) is ignored since declaration of case-insensitive constants is no longer supported in G:\server\php-test\p1.php on line 4
Здравствуй, мир.
Fatal error: Uncaught Error: Undefined constant "text" in G:\server\php-test\p1.php:6 Stack trace: #0 {main} thrown in G:\server\php-test\p1.php on line 6

Пример 4

<?php
// error_reporting(E_ALL);
echo '<p>Версия PHP ', phpversion(), '</p>';
define("TEXT", "Здравствуй, мир.", true);
echo TEXT, '<br>';
echo text, '<br>';
?>

Результаты:

Версия PHP 7.2.34

Здравствуй, мир.
Здравствуй, мир.

Версия PHP 7.4.27

Deprecated: define(): Declaration of case-insensitive constants is deprecated in G:\server\php-test\p1.php on line 4
Здравствуй, мир.
Deprecated: Case-insensitive constants are deprecated. The correct casing for this constant is "TEXT" in G:\server\php-test\p1.php on line 6
Здравствуй, мир.

Версия PHP 8.0.14

Warning: define(): Argument #3 ($case_insensitive) is ignored since declaration of case-insensitive constants is no longer supported in G:\server\php-test\p1.php on line 4
Здравствуй, мир.
Fatal error: Uncaught Error: Undefined constant "text" in G:\server\php-test\p1.php:6 Stack trace: #0 {main} thrown in G:\server\php-test\p1.php on line 6

Рекламный блок

Информационный блок