codescracker


php

PHP Data Types



« Previous Tutorial Next Tutorial »


As PHP is a loosely types language, that is, we didn't have to tell PHP which data types the variable is. Because PHP automatically converts a variable to the correct data type, it depends on the value of the variable. Basically PHP has eight data types, can be used to construct variables in PHP scripts:

PHP Integer Data Type

PHP integer data types are simply whole numbers, without any decimal point such as 23, 3, 543, etc. Here is an example of assigning integer values to the PHP Variables:

$int_var = 145;
$sum_ints = -1235 + 1235;

PHP Double Data Type

PHP double data types are whole numbers with decimal point, such as 32.4332, 2332.432, 432.40, etc. Here is an example, using double data types in PHP:

<!DOCTYPE html>
<html>
<body>

<?php

$many = 5.2888800;
$many_2 = 5.2111200;
$few = $many + $many_2;
echo "$many + $many_2 = $few<br>";
?>

</body>
</html>

Here is the output produced by the above PHP scripts:

PHP Double Data Type

PHP Boolean Data Type

PHP boolean data type have only two possible values, which is true or false. Here is an example, uses boolean data type in PHP:

if (TRUE)
   print("This will always print
"); else print("This will never print
");

PHP NULL Data Type

PHP NULL data type is a special type, has only one value, which is NULL. Here is an example, assigning NULL to a variable in PHP:

$var_name = NULL;

PHP Strings Data Type

PHP strings data types are used to store sequence of characters. Here is an example, uses some string data types in PHP:

$string_1 = "This is a string in double quotes";
$string_2 = "This is a somewhat longer, singly quoted string";
$string_39 = "This string has thirty-nine characters";
$string_0 = ""; // a string with zero characters

In PHP, strings which are in single quote, are treated almost literally, whereas strings which are in double quote, replace variables with their values, as well as, specially interpreting certain character sequences. Here is an example, uses string data types in PHP:

<!DOCTYPE html>
<html>
<body>

<?php
$variable = "name";
$literally = 'This $variable will not print';
print($literally);
echo "<br/>";
$literally = "This $variable will print";
print($literally);
?>

</body>
</html>

Here is the output of the above PHP scripts:

PHP Strings Data Types

PHP Here Document

Using PHP here document, you are able to assign multiple lines to a single string vaeiable. Here is an example:

<!DOCTYPE html>
<html>
<body>

<?php
$channel =<<<_XML_
<channel>
<title>Here Document Example<title>
<link>http://codescracker.com/php/<link>
<description>PHP Tutorials from beginner to advance.</description>
</channel>
_XML_;

echo <<<END
This uses the "here document" syntax to output
multiple lines with variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon. no extra whitespace!
<br />
END;

print $channel;
?>

</body>
</html>

It will display the following result:

PHP Here Documents

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test