tags:

views:

75

answers:

6

I need to create a var like $myvar-test; but thats not valid or?

A: 

You 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

kemp
A: 

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

Col. Shrapnel
+8  A: 

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 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]*'

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.
?>
Kai Sellgren
According to your quote from the documentation, Unicode isn't allowed. Given PHP's very bad track record of supporting Unicode it would have surprised me, actually.
Joey
To be honest, I'm not sure what the given regex in the documentation exactly matches (does it match Unicode letters?), but I can use even chinese characters if I wanted to like $華語 = 5; and define function names the same way.
Kai Sellgren
A: 

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.

Marek Karbarz
A: 

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.

deceze
A: 

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"
Ionuț G. Stan