News & UpdatesProgrammingWeb programmingPartnersStore
Book Cover
Buy Now
Projects
Links

PHP Tutorial – 03 – Variables

Variables are used for storing values, such as numbers or strings, so that they can be used several times in a script. All variables in PHP start with a dollar sign and are case sensitive. The name can include underscore characters and numbers, but it cannot start with a number.

$myVar;

Data types

PHP variables are not declared explicitly, instead they are declared automatically the first time they’re used. It’s a loosely typed language so the data type of variables are not specified. PHP automatically converts the variable to the appropriate data type, such as strings, integers, or floating-point numbers.

$myString = "Text"; // string
$myInt = 7;         // integer
$myDouble = 5.5;    // float

As for boolean values the keywords true and false are used.

$myBool = false; // boolean

The value of a variable can be printed by using echo followed by the variable’s name.

echo $myString;

Null value

An undefined variable has the value null. This can also be set with the null keyword.

$myNull = null; // undefined

Default values

As mentioned before, a variable’s type is determined by the context in which the variable is used. If an undefined variable is used as a bool it becomes false, as an integer or float it becomes 0, and as a string it becomes “” (an empty string).

If you like this tutorial please +1 it:


Leave a Reply