0% found this document useful (0 votes)
12 views

1.3. PHP Data Types

This document provides an overview of PHP data types, including Integer, Float, Boolean, String, Array, Object, Null, and Constant. It explains how PHP is a loosely typed language that allows dynamic data type assignment and includes examples for each data type. Additionally, it covers the usage of constants and various string functions in PHP.

Uploaded by

Karthikeyini S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

1.3. PHP Data Types

This document provides an overview of PHP data types, including Integer, Float, Boolean, String, Array, Object, Null, and Constant. It explains how PHP is a loosely typed language that allows dynamic data type assignment and includes examples for each data type. Additionally, it covers the usage of constants and various string functions in PHP.

Uploaded by

Karthikeyini S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

SRI KRISHNA COLLEGE OF ENGINEERING AND TECHNOLOGY

Kuniamuthur, Coimbatore, Tamilnadu, India


An Autonomous Institution, Affiliated to Anna University,
Accredited by NAAC with “A” Grade & Accredited by NBA (CSE, ECE, IT, MECH ,EEE, CIVIL& MCT)

Course : 21CSI504 – PHP and JS Framework


Module : 1
Topic : PHP Data Types
Faculty : Dr. S. Karthikeyini
Department : M.Tech – Computer Science & Engineering

1
Topics Covered

▪ PHP Data Type


▪ Integer
▪ Float
▪ Boolean
▪ String
▪ Array
▪ Object
▪ Null
▪ Constant

2
PHP Data Types

• Just like JavaScript, PHP has data types.

• Like JavaScript, PHP is known as a


“loosely typed” language. That means it
decides the data type dynamically. This
means variables storing data can change
types.
PHP Data Types
o Variables can store data of different types, and different
data types can do different things.
o PHP supports the following data types:
• String
• Integer
• Float (floating point numbers - also called double)
• Boolean
• Array
• Object
• NULL
• Resource
PHP Data Types
PHP Integer

• An integer is a number without decimals.


• Rules for integers:
• An integer must have at least one digit (0-9)
• An integer cannot contain comma or blanks
• An integer must not have a decimal point
• An integer can be either positive or negative
• Integers can be specified in three formats: decimal (10-
based), hexadecimal (16-based - prefixed with 0x) or
octal (8-based - prefixed with 0)
Example
<?php Output:
$x = 5985; int(5985)
var_dump($x); int(-345)
echo "<br>"; int(140)
$x = -345; // negative number int(39
var_dump($x);
echo "<br>";
$x = 0x8C; // hexadecimal number
var_dump($x);
echo "<br>";
$x = 047; // octal number
var_dump($x); The var_dump() function dumps information about one
or more variables. The information holds type and value
?> of the variable(s).The PHP var_dump() function returns the
data type and value of variables:
Integer

o The integer data type is used to specify a numeric value


without a fractional component.
o You can declare as given below

Integer $variable;

$variable = 10;
Real Number

o It is also known as a floating number or floating point


number.
o It is not a whole number and has fractions such as 1.22,
2.45, 100.765 etc.
o Some examples of valid floating point numbers include:
o 3.14
o 0.001
o -1.234
o 0.314E2 // 31.4
o 1.234E-5 // 0.00001234
o -3.45E-3 // -0.00345
PHP Floating Point Numbers
A floating point number is a number with a decimal point or a number in
exponential form.
In the following example we will test different numbers.

<?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

o Boolean values are true or false, also 0 and empty string


evaluates to false, and any numeric value rather than zero
or a string that is not empty evaluates to true.
o You can declare as given below

Boolean $variable;

Where Boolean denotes the type of the variable.


Boolean

• Booleans can be either TRUE or FALSE.


• $x=true;
$y=false;
• Booleans are often used in conditional testing.
String

• 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 String values are sequence of characters, included in a


single quote or double quote for example,

o $str1 = “This is a string data type variable”;

o A string literal can be specified in four different ways:


o Single quoted
o Double quoted
o Heredoc syntax
o Nowdoc syntax
Single Quoted

o The simplest way to specify a string is to enclose it in single


quotes

o Example:
<?php
echo ‘this is a simple string’;
?>
Double Quoted

o If the string is enclosed in double-quotes (“)

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 Nowdocs are to single-quoted strings what heredocs are to


double-quoted strings.
o A nowdoc is specified similarly to heredoc, but no parsing is
done inside a nowdoc
Array

o An array in PHP is actually an ordered map. A map is a type


that associates values to keys. This type is optimized for
several different uses; It can be treated as an array, list
(vector), hash table (an implementation of a map),
dictionary, collection, stack, queue and probably more. As
array values can be other array s, trees and
multidimensional array s are also possible.
Array

• An array stores multiple values in one single variable.


• In this example we create an array, and then use the
PHP var_dump() function to return the data type and
value of the array:
<?php
$cars=array("Volvo","BMW","Toyota");
var_dump($cars);
?>
• Output:
array(3) { [0]=> string(5) "Volvo" [1]=> string(3)
"BMW" [2]=> string(6) "Toyota" }
Resource

o Resources are not an actual data type, but the storing of a


reference to functions and resource external to PHP.
o The most common example of using the resource data type
is a database call.
Null

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 Float  PHP Boolean


PHP Data Types
o PHP Array
• An array stores multiple values in one single variable.
• $cars is an array. The PHP var_dump() function
returns the data type and value:
PHP Data Types
 PHP Object
 An object is a data type which
stores data and information on
how to process that data.
 In PHP, an object must be
explicitly declared.
 First we must declare a class of
object. For this, we use the class
keyword. A class is a structure
that can contain properties and
methods:
PHP Data Types
 PHP NULL Value
 Null is a special data type which
can have only one value: NULL.
 A variable of data type NULL is a
variable that has no value
assigned to it.
 If a variable is created without a
value, it is automatically assigned
a value of NULL.
 Variables can also be emptied by
setting the value to NULL:
PHP Strings
o PHP String Functions
• strlen() - Return the Length of a String

• str_word_count() - Count Words in a String


PHP Strings
 strrev() - Reverse a String

 strpos() - Search For a Text Within a String

 str_replace() - Replace Text Within a String


PHP Constants
o Constants are like variables except that once they are
defined they cannot be changed or undefined.
o A constant is an identifier (name) for a simple value. The
value cannot be changed during the script.
o A valid constant name starts with a letter or underscore
(no $ sign before the constant name).
o Unlike variables, constants are automatically global
across the entire script.
o To create a constant, use the define() function.
Set a PHP Constant
• To set a constant, use the define() function - it takes three
parameters:
• The first parameter defines the name of the constant,
• the second parameter defines the value of the constant, and
• the optional third parameter specifies whether the constant
name should be case-insensitive. Default is false.
• The example below creates a case-sensitive constant, with the
value of "Welcome to OSP class!":
• Example:
<?php
// define a case-sensitive constant
define("GREETING", "Welcome to OSP class!");
echo GREETING;
echo "<br>";
// will not output the value of the constant
echo greeting;
?>
The example below creates a case-insensitive constant, with the value
of "Welcome to OSP class!":

<?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

You might also like