Thursday, 25 October 2012

Creating (Declaring) PHP Variables

PHP has no command for declaring a variable.
A variable is created the moment you first assign a value to it:

$myCar="Volvo"; 

After the execution of the statement above, the variable myCar will hold the value Volvo.
Tip: If you want to create a variable without assigning it a value, then you assign it the value of null.
Let's create a variable containing a string, and a variable containing a number:

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

PHP is a Loosely Typed Language

In PHP, a variable does not need to be declared before adding a value to it.
In the example above, notice that we did 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.

No comments:

Post a Comment