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

10. PHP Data Types , Conditional & Iterative Statements

The document provides an overview of PHP data types, operators, conditional statements, iterative statements, and functions. It details built-in data types such as strings, integers, floats, booleans, arrays, and NULL, along with various operators and control structures used in PHP programming. Additionally, it highlights the advantages of using functions and the concept of user-defined functions in PHP.

Uploaded by

sheikhoo1274
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)
0 views

10. PHP Data Types , Conditional & Iterative Statements

The document provides an overview of PHP data types, operators, conditional statements, iterative statements, and functions. It details built-in data types such as strings, integers, floats, booleans, arrays, and NULL, along with various operators and control structures used in PHP programming. Additionally, it highlights the advantages of using functions and the concept of user-defined functions in PHP.

Uploaded by

sheikhoo1274
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/ 20

www.tuf.edu.

pk
unioffaisalabad

Web Design and Development


PHP Data Types , Conditional & Iterative Statements

Aasma Khalid
Lecturer
The University of Faisalabad
PHP DataTypes www.tuf.edu.pk
unioffaisalabad

Data type specifies the type of value a variable requires to do various


operations without causing an error. By default, PHP provides the
following built-in data types:
• String
• Integer
• Float
• Boolean
• Array
• NULL
www.tuf.edu.pk
unioffaisalabad

• 1. String
• A string is a sequence of characters that holds letters and
numbers. It can be anything written inside single or double
quotes.
• For Example:
www.tuf.edu.pk
unioffaisalabad

2. Integer
An integer is a non-decimal number typically ranging
between -2,147,483,648 and 2,147,483,647.
3. Float
A float is a number with a decimal point. It can be an
exponential number or a fraction.
4. Boolean
A Boolean represents two values: True or False.
www.tuf.edu.pk
unioffaisalabad

5. Array
Array is a collection of similar data elements stored in a
single variable.
6. NULL
Null is a special data type with only one value which is
NULL. In PHP, if a variable is created without passing a
value, it will automatically assign itself a value of NULL.
PHP Operators www.tuf.edu.pk
unioffaisalabad

PHP has different types of operators for different operations.


They are as follows:
1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. PHP Increment/ Decrement Operators
5. PHP Logical Operators
6. PHP String Operators
7. PHP Array Operators
8. PHP Conditional Operators
PHP Conditional Statements www.tuf.edu.pk
unioffaisalabad

Conditional Statements are used to perform actions based on


different conditions. Sometimes when we write a program, we
want to perform some different actions for different actions. We
can solve this by using conditional statements.
In PHP we have these conditional statements:
– if Statement.
– if-else Statement
– If-elseif-else Statement
– Switch statement
if Statement www.tuf.edu.pk
unioffaisalabad

• This statement executes the block of code inside the if


statement if the expression is evaluated as True.
• Example:
if-else Statement www.tuf.edu.pk
unioffaisalabad

• This statement executes the block of code inside the if


statement if the expression is evaluated as True and executes
the block of code inside the else statement if the expression is
evaluated as False.
• Example:
If-else-if-else www.tuf.edu.pk
unioffaisalabad

• This statement executes different expressions for more than


two conditions.
• Example:
Switch Statement www.tuf.edu.pk
unioffaisalabad

• This statement allows us to execute different blocks of code


based on different conditions. Rather than using if-elseif-if, we
can use the switch statement to make our program.
• Example:
PHP Iterative Statements www.tuf.edu.pk
unioffaisalabad

Iterative statements are used to run same block of code over and
over again for a certain number of times.
In PHP, we have the following loops:
– while Loop
– do-while Loop
– for Loop
– foreach loop
While Loop www.tuf.edu.pk
unioffaisalabad

The While loop in PHP is used when we need to execute a block


of code again and again based on a given condition. If the
condition never becomes false, the while loop keeps getting
executed. Such a loop is known as an infinite loop.
Do-While Loop www.tuf.edu.pk
unioffaisalabad

The do-while loop is similar to a while loop except that it is


guaranteed to execute at least once. After executing a part of a
program for once, the rest of the code gets executed based on a
given boolean condition.
For Loop www.tuf.edu.pk
unioffaisalabad

• The for loop is used to iterate a block of code multiple times.


Foreach loop www.tuf.edu.pk
unioffaisalabad

The foreach loop in PHP can be used to access the array indexes
in PHP. It only works on arrays and objects.
Function Basics www.tuf.edu.pk
unioffaisalabad

Function arguments are variables of some supported data type that are processed
within the body of the function. It can take input as an argument and return value.
PHP has more than 1000 built-in functions, and in addition, you can also create
your own functions.

Advantages:
• Functions reduce the complexity of a program and give it a modular structure.
• A function can be defined only once and called many times.
• It saves a lot of code writing because you don't need to write the same logic
multiple times, you can write the logic once and reuse it.
www.tuf.edu.pk
unioffaisalabad

• User Defined Functions: Apart from built-in functions, We can


also create our own functions and call them easily.
• A user-defined function looks something like this:
Arrays www.tuf.edu.pk
unioffaisalabad

An array is a collection of data items of the same data type. And


it is also known as a subscript variable.
www.tuf.edu.pk
unioffaisalabad

Any question?

You might also like