How to check if a string can be used as a variable name in PHP?

In PHP, you can use variable variables ...

For example...

class obj { } $fieldName = "Surname"; $object = new obj(); $object->Name = "John"; $object->$fieldName = "Doe"; echo "{$object->Name} {$object->Surname}"; // This echoes "John Doe". 

However, the string $ fieldName may contain some characters that are not valid in variable names. PHP will still create a field with that name (like an associative array), but I cannot access it using $ object → ...... because it will not parse correctly.

Now, is there any function that can check if a string can be used as a valid PHP variable name. If not, how will this be created using regular expressions? What are the rules for variable names in PHP?

+7
variables object php names variable-variables
source share
7 answers

From the manual :

Variable names follow the same rules as other labels in PHP. The correct variable name begins with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it is expressed as follows: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

So, if you ran your line through RegEx, you can find out if it is valid or not.

It should be noted that the ability to access the "invalid" names of Object objects using a variable variable is the right approach for some XML parsing.

For example, from SimpleXML docs:

Access to elements in an XML document that contains characters that are not permitted by the PHP naming convention (such as a hyphen) can be achieved by encapsulating the element name in curly braces and an apostrophe.

The following is a sample code:

 echo $xml->movie->{'great-lines'}->line; 

Therefore, it is not necessary to have properties that can only be accessed in this way.

However, if your code creates and uses an object, you are wondering why you are using these properties. Resolving, of course, a situation similar to the SimpleXML example where an object is created to represent something outside your control.

+15
source share

As already mentioned, but not with a full line of code:

 if ( preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/','evaluation string') ) { // the string is valid } else { // the string is not valid } 
+5
source share

'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' will check the name of the PHP variable.

This is a regular expression directly from the documentation at: http://www.php.net/manual/en/language.variables.basics.php

+4
source share

However, the string `$ fieldName may contain some characters that are not valid in variable names. PHP will still create a field with that name (like an associative array), but I cannot access it using $ object → ...... because it will not parse correctly.

You can still access the field through the syntax $object->{"fieldname"} .

As far as I know, the only limitation is that you cannot access properties with \x00 in the name and you cannot define variables starting with \x00 .

Example:

 $a = new stdclass; $a->{"\x00dd"} = 8; //invalid $a->{"dd\x00dd"} = 8; //valid, but... echo $a->{"dd\x00dd"}; //can't read (no property "dd") 
+1
source share

but I will not be able to access it with $ object → ...... because it is not correctly understood

but look:

 class A {} $varName = '!asd asd'; $a = new A(); $a->$varName = '1'; echo "{$a->{'!asd asd'}}"; // 1 

Of course, not recommended, but it can .

+1
source share

Validation with RegEx, if you want to allow $ or &$ (pass a variable by reference) to validate in a string, you can use this regular expression:

 /^([\$]|(&\$))[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/ 
0
source share

I think regex is the way to go, and as far as I remember, the limitations are as follows:

  • alphanumeric
  • must begin with a letter
  • may contain an underscore

therefore, the regular expression will be "/ [a-zA-Z] + [0-9a-zA-Z_] * /" - from the top of the head so that your movement can change.

-one
source share

All Articles