DECLARE VARIABLES IN PHP


Variables are containers for storing information.
In PHP, variables can be declared anywhere in the script.

Rules for PHP variables:

  • A variable starts with the $ sign
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores
  • Variable names are case sensitive

Let's try creating a variable containing a string, and a variable containing a number :

            <?php
           $txt="Hello World!";
           $x=16;
         ?>


PHP is a Freely Typed Language 

In the example above, you see that you do not have to tell PHP which data type the variable is. 
PHP automatically converts the variable to the correct data type, depending on its value. 
In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it. 
In PHP, the variable is declared automatically when you use it.

Comments