php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #39150 PHP allows invalid names for constants and variables
Submitted: 2006-10-13 15:47 UTC Modified: 2012-09-22 08:39 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: z_rules55 at hotmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.1.6 OS: WinXP
Private report: No CVE-ID: None
 [2006-10-13 15:47 UTC] z_rules55 at hotmail dot com
Description:
------------
The PHP manual states that:
    A valid variable name starts with a letter or
    underscore, followed by any number of letters,
    numbers, or underscores.
And that constants must follow the same naming rules as variables. However, PHP allows me to define variables and constants with an invalid character like a hyphen or a dollar sign, as well as starting with a number. Those are the only three invalid names I tested; I assume others could also be used.

Reproduce code:
---------------
<?php
define('my$definition', 'value');
define('my-definition', 'value');
define('2mydefinition', 'value');
$var = '2morrow$is-another-day';
$$var = 'this should be invalid';

echo constant('my$definition')."\n";
echo constant('my-definition')."\n";
echo constant('2mydefinition')."\n";
echo $GLOBALS['2morrow$is-another-day']."\n";
echo "\n";
//echo my$definition."\n";
echo my-definition."\n";
//echo 2mydefinition."\n";
//echo $2morrow$is-another-day."\n";
?>

Expected result:
----------------
Some kind of error upon defining the constant or variable with the invalid name.

Actual result:
--------------
PHP happily accepts the define() with the invalid name, as well as the assignment to the variable variable with the invalid name. Using constant() and $GLOBALS to read the value of the invalid constant and variable work fine. Echoing the value of my-definition gives the usual warning of:
    Notice: Use of undefined constant my - assumed 'my' in
    C:\path\to\script.php on line 14

    Notice: Use of undefined constant definition - 
    assumed 'definition' in C:\path\to\script.php
    on line 14

Only when you try to access the invalid constant or variable directly does PHP throw parse errors.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-10-13 15:52 UTC] tony2001@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 [2006-10-13 19:12 UTC] z_rules55 at hotmail dot com
I checked the docs, and I don't see anyone else having submitted the same bug to the bug database.

If this is not a bug, maybe I should change it to feature request? Why would PHP allow the creation of variables and constants that can only be accessed with a work-around instead of directly, as is most natural and intuitive? You say "read the variable", not "access the superglobal array with the associative index equal to the variable's name". Likewise, why would you have to call a function to read a constant that you already know the name of?

Or, change the docs so they make this fact more clear: that you *can* in fact create variables and constants with supposedly invalid names, but you'll have a harder time using them later on.
 [2006-10-16 19:40 UTC] z_rules55 at hotmail dot com
Changing the status of this back to Open because I think it deserves further consideration.
 [2012-09-22 08:39 UTC] nikic@php.net
PHP allows arbitrary variable names using the ${'string'} syntax. The restrictions only apply to the $name syntax. The same applies to constants and various other names in PHP, which have a "plain" access method and an access method based on a string.
 [2012-09-22 08:39 UTC] nikic@php.net
-Status: Open +Status: Not a bug -Package: Feature/Change Request +Package: *General Issues
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 11 21:01:31 2024 UTC