Quakenet/#php Tutorial

Note: If you opened this page from an external URL pay attention that all chapters are linked together. Be sure you also read all prior chapters of this tutorial, otherwise you will miss relevant content explained before.

Variables

  1. What are variables?
  2. Syntax of variables
  3. Assignment operator

1. What are variables?

Typically php scripts are generate the output dynamically. The php scripts itselfs (the contents of the files) are static however. So we need something which can be dynamic filled. For this there are so called variables. These are filled by the php script and can be reuse later in the script.

2. Syntax of variables

All variables in php starts with the dollar character $. After that the name of the variable starts with a letter or the underscore _. And after that the name can also use digits. Variables which begins with an underscore has some special meaning. For this reason the php developer shouldn't use own variables which start with an underscore.

<?php
$valid
$10_not_valid
?>

Variables are case-sensitive, so its important how you write your variables. The variable $foo is not the same as the variable $FOO. This is important as the superglobal variables like $_GET are not written as $_get.

3. Assignment operator

To fill a variable with a value you use the assignment operator =. The variable must be written on the left of the = character, the value to save must be written on the right.

<?php
$var 
'Content';
$
123 "not valid :(";
$some_variable $another_variable;
?>

After we filled the variables with values we can use e.g. with echo

<?php
$email 
'foo@example.com';
echo 
'Your email is: ';
echo 
$email;
?>

Questions about the chapter

1. How is the syntax of variables?

Variables starts with a dollar character $. Then there are any number of letters and underscores _. After the first character of the name digits can be followed, too. Even special characters like umlauts are allowed, however some editor with syntax highlighting doesn't recognize them correct. See the manual chapter of Variables.

Back to Next to
Copyright © to the OPs of #php/QuakeNet Valid XHTML 1.0 Strict Valid CSS!