PHP.nl

Basics

Basics

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.

A valid variable name starts with a letter
(, , or the bytes from 128 through 255)
or underscore, followed
by any number of letters, numbers, or underscores. As a regular
expression, it would be expressed thus:

A-Z``a-z``^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$

Opmerking: > PHP doesn't support Unicode variable names, however, some character encodings (such as UTF-8) encode characters in such a way that all bytes of a multi-byte character fall within the allowed range, thus making it a valid variable name.

Opmerking: > is a special variable that can't be assigned. Prior to PHP 7.1.0, indirect assignment (e.g. by using ) was possible. $thisvariable variables

Voorbeeld: Valid variable names

<?php
$var = 'Bob';
$Var = 'Joe';
echo "$var, $Var";      // outputs "Bob, Joe"

$_4site = 'not yet';    // valid; starts with an underscore
$täyte = 'mansikka';    // valid; 'ä' is (Extended) ASCII 228.
?>

Voorbeeld: Invalid variable names

<?php
$4site = 'not yet';     // invalid; starts with a number
PHP accepts a sequence of any bytes as a variable name. Variable
names that do not follow the above-mentioned naming rules can only be
accessed dynamically at runtime. See

for information on how to access them.

variable variables

Voorbeeld: Accessing obscure variable names

<?php
${'invalid-name'} = 'bar';
$name = 'invalid-name';
echo ${'invalid-name'}, " ", $$name;
?>
bar bar
By default, variables are always assigned by value. That is to say,
when an expression is assigned to a variable, the entire value of
the original expression is copied into the destination
variable. This means, for instance, that after assigning one
variable's value to another, changing one of those variables will
have no effect on the other. For more information on this kind of
assignment, see the chapter on .

Expressions

PHP also offers another way to assign values to variables:
. 
This means that the new variable simply references (in other words, 
"becomes an alias for" or "points to") the original variable. 
Changes to the new variable affect the original, and vice versa. 

assign by reference

To assign by reference, simply prepend an ampersand (&) to the
beginning of the variable which is being assigned (the source
variable). For instance, the following code snippet outputs '' twice:

`My name is Bob````php





    One important thing to note is that only variables may be
    assigned by reference.
    
   ```php
<?php
$foo = 25;
$bar = &$foo;      // This is a valid assignment.
$bar = &(24 * 7);  // Invalid; references an unnamed expression.

function test()
{
   return 25;
}

$bar = &test();    // Invalid because test() doesn't return a variable by reference.
?>
It is not necessary to declare variables in PHP, however, it is a very
good practice. Accessing an undefined variable will result in an
 (prior to PHP 8.0.0, ).
An undefined variable has a default value of null.
The  language
construct can be used to detect if a variable has already been initialized.

E_WARNING``E_NOTICE``isset

Voorbeeld: Default value of an uninitialized variable

<?php
// Unset AND unreferenced (no use context) variable.
var_dump($unset_var);
?>
Warning: Undefined variable $unset_var in ...
NULL

PHP allows array autovivification (automatic creation of new arrays) from an undefined variable. Appending an element to an undefined variable will create a new array and will not generate a warning.

Voorbeeld: Autovivification of an array from an undefined variable

<?php
$unset_array[] = 'value'; // Does not generate a warning.
?>

Waarschuwing: > Relying on the default value of an uninitialized variable is problematic when including one file in another which uses the same variable name.

A variable can be destroyed by using the 
language construct.

unset

For information on variable-related functions, see the
.

Variable Functions Reference