1.3. PHP Data Types
1.3. PHP Data Types
1
Topics Covered
2
PHP Data Types
Integer $variable;
$variable = 10;
Real Number
<?php Output:
$x = 10.365; float(10.365)
var_dump($x); float(2400)
echo "<br>"; float(8.0E-5)
$x = 2.4e3;
var_dump($x);
echo "<br>";
$x = 8E-5;
var_dump($x);
?>
Boolean
Boolean $variable;
• PHP Strings
• A string is a sequence of characters, like "Hello world!".
• A string can be any text inside quotes. You can use single or
double quotes:
• Example
<?php
$x = "Hello world!";
echo $x;
echo "<br>";
$x = 'Hello world!’;
echo $x;
?>
String
o Example:
<?php
echo ‘this is a simple string’;
?>
Double Quoted
o Example:
<?php
echo “This is \n a simple string”;
?>
O/P: This is
a simple string
Here Documents
o The heredoc string structure is a method of including larger
strings inside the code. We can use it to include content of
any length.
o The heredoc syntax is a way to declare a string variable. The
heredoc syntax takes at least three lines of your code and
uses the special character <<< at the beginning.
o To create a heredoc, use a special operator that is made up
of three left brackets (<<<). The syntax is as follows:
o $longString = <<< termination_maker any amount of
content termination_maker;
Example
Example
Examples
output
Cont.,
Nowdoc
o Null is a special data type which can have only one value,
which is itself.
o Which is to say, null is not only a data type, but also a
keyword literal.
o A variable of data type null is a variable that has no value
assigned to it.
PHP Data Types
PHP Integer PHP String
<?php
// define a case-insensitive constant
define("GREETING", "Welcome to OSP class!", true);
echo GREETING;
echo "<br>";
// will also output the value of the constant
echo greeting;
?>
output:
Welcome to OSP class!
Welcome to OSP class!
Summary
▪ PHP Data Type
▪ Integer
▪ Float
▪ Boolean
▪ String
▪ Array
▪ Object
▪ Null
▪ Constant
References
1. Steven Holzner, “PHP:The Complete
Reference”, McGraw Hill Education, 2017
2. https://www.w3schools.com/php/
3. https://www.tutorialspoint.com/php/index.htm
40