user9059272
user9059272

Reputation:

How does PHP deal with undefined constant?

I'm using PHP 7.0.2

Consider below text from the PHP Manual :

Why is $foo[bar] wrong?

Always use quotes around a string literal array index. For example, $foo['bar'] is correct, while $foo[bar] is not. But why? It is common to encounter this kind of syntax in old scripts:

<?php
$foo[bar] = 'enemy';
echo $foo[bar];
// etc
?>

This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes). It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.

From the above text I'm not clear that when PHP encountered bar() which does not correspond to any known symbol i.e. undefined constant what PHP actually does with it?

How can PHP convert a bare string bar into a string which contains the bare string i.e. 'bar'?

Is PHP defining a constant titled bar and assigning a string value 'bar' to it?

Like bar = 'bar';

If yes, can I make use of the constant bar somewhere in the further code?

Because in PHP only a variable and a constant can contain value/hold the value and not type like string contain/hold any value.

Upvotes: 1

Views: 2871

Answers (2)

n0099
n0099

Reputation: 1323

The fallback for bareword to string literal is deprecated in PHP 7.2 and removed in PHP 8.0: https://3v4l.org/cT1ie

<?php
var_dump(Y);
var_dump(date(Y));
  • Output for 8.0.0 - 8.0.28, 8.1.0 - 8.1.16, 8.2.0 - 8.2.3
Fatal error: Uncaught Error: Undefined constant "Y" in /in/cT1ie:3
Stack trace:
#0 {main}
  thrown in /in/cT1ie on line 3

Process exited with code 255.
  • Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Warning: Use of undefined constant Y - assumed 'Y' (this will throw an Error in a future version of PHP) in /in/cT1ie on line 3
string(1) "Y"

Warning: Use of undefined constant Y - assumed 'Y' (this will throw an Error in a future version of PHP) in /in/cT1ie on line 4
string(4) "2023"
  • Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33
Notice: Use of undefined constant Y - assumed 'Y' in /in/cT1ie on line 3
string(1) "Y"

Notice: Use of undefined constant Y - assumed 'Y' in /in/cT1ie on line 4
string(4) "2023"

From https://wiki.php.net/rfc/deprecate-bareword-strings:

When PHP encounters an unquoted token such as FROB_ACTIVE, it tries to resolve it as a built-in or user-defined constant, but if no such constant exists, it treats it as equivalent to the quoted string 'FROB_ACTIVE', and issues an E_NOTICE message. This behaviour has been around since very early versions of PHP, but is inconsistent with the rest of the language, and can lead to serious bugs. This RFC proposes three things: to raise the level of the message to E_WARNING; to officially deprecate the fallback; and to remove it in PHP 8.0.

and its discussion: https://externals.io/message/98025

Is there an alternative way to write string literals in PHP (without ' or ")?

Upvotes: 1

user149341
user149341

Reputation:

From the above text I'm not clear that when PHP encountered bar() which does not correspond to any known symbol i.e. undefined constant what PHP actually does with it?

The warning associated with this behavior says it all, really:

Warning: Use of undefined constant bar - assumed 'bar' (this will throw an Error in a future version of PHP)

The bare word is treated as a string, not as a constant -- it's treated exactly as if you'd written 'bar'. The expression defined('bar') will still be false, other instances of bar in your code will also throw warnings, and code which depends on this behavior will stop working entirely in future versions of PHP.

Upvotes: 1

Related Questions