PHP Variable types

Variable names begin with the $ sign and are case sensitive (language keywords are not).

In PHP it is not necessary to define the type before using it, they are created when using them. Variables are declared when assigned a value, for example:

$ day = 24; // A variable of type integer is declared.
$ salary = 758.43; // A variable of type double is declared.
$ name = “juan”; // A variable of type string is declared.
$ exite = true; // A boolean variable is declared.

We can also note that to have line comments we must use two characters //

For the printing of variables we will initially use the echo command. A complete program that initializes and displays the content of four variables of different types is:

<html>
<head>
<title> Problem </title>
</head>
<body>
<? php
$ day = 24; // A variable of type integer is declared.
$ salary = 758.43; // A variable of type double is declared.
$ name = “juan”; // A variable of type string is declared.
$ exite = true; // A boolean variable is declared.
echo “Integer variable:”;
echo $ dia;
echo “<br>”;
echo “Variable double:”;
I pay $ salary;
echo “<br>”;
echo “Variable string:”;
echo $ name;
echo “<br>”;
echo “Boolean variable:”;
echo $ exite;
?>
</body>
</html>

We have used an echo command to display the messages, another the variable content and finally another to print HTML marks. This process can be shortened a bit but for the sake of clarity we will initially take the long way of printing a data with each echo command.

Leave a Reply