I need to create a var like $myvar-test; but thats not valid or?
views:
75answers:
6You can't use dashes in variable names.
From the documentation:
Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.
Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
Source: http://www.php.net/manual/en/language.variables.basics.php
minus sign used for subtraction
you will end up with same result with variables like $myvar*test $myvar$test, $myvar;test $myvar(test and such.
Just don't use special syntax in the variables names.
Note that array key names may contain almost any character, even of national languages
No, it's not valid. The reason is the minus sign (-). In PHP, variables can contain 0-9a-zA-Z_ with the exception that it must not begin with a number and Unicode letters are allowed. See: http://www.php.net/manual/en/language.variables.basics.php
Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.
Variable names follow the same rules as other labels in PHP. A validvariable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
And a couple of examples:
<?php
$var = 'Bob';
$Var = 'Joe';
echo "$var, $Var"; // outputs "Bob, Joe"
$4site = 'not yet'; // invalid; starts with a number
$_4site = 'not yet'; // valid; starts with an underscore
$täyte = 'mansikka'; // valid; 'ä' is (Extended) ASCII 228.
?>
From PHP documentation
Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
So your variable name is invalid according to these rules.
No, it isn't.
http://www.php.net/manual/en/language.variables.basics.php
Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
There are workarounds, but there's really, really ugly. Better use an underscore, or camelCase or an array where you can use any string as key names.
This would be the workaround:
${'a-b'} = 'foo';
var_dump(${'a-b'}); // string(3) "foo"