0% found this document useful (0 votes)
10 views72 pages

IT WS03 Chapter 5 and 6

The document covers data types in PHP, focusing on strings and arrays, including their creation, manipulation, and commonly used functions. It also discusses control structures such as if statements, loops, and the ternary operator, which are essential for decision-making and flow control in PHP programming. Additionally, it highlights the integration of PHP with HTML for dynamic content generation, emphasizing the importance of mastering these concepts for effective PHP development.

Uploaded by

Arjay Vicencio
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)
10 views72 pages

IT WS03 Chapter 5 and 6

The document covers data types in PHP, focusing on strings and arrays, including their creation, manipulation, and commonly used functions. It also discusses control structures such as if statements, loops, and the ternary operator, which are essential for decision-making and flow control in PHP programming. Additionally, it highlights the integration of PHP with HTML for dynamic content generation, emphasizing the importance of mastering these concepts for effective PHP development.

Uploaded by

Arjay Vicencio
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/ 72

Lesson 5

More
Data Types in
PHP
Strings
In PHP, a string is a sequence of
characters. Strings can be created
using single quotes or double
quotes. Double quotes allow for the
use of escape sequences and
variable substitution.
Commonly used String Functions
in PHP
PHP provides
several built-in
functions for
working with
Commonly used String Functions
strlen
in PHP

This function is used to get the


length of a string. It takes a
string as input and returns the
number of characters in the
string.
Commonly used String Functions
 strpos
in PHP : This function is used to
find the position of a substring within
a string. It takes two strings as input
and returns the position of the first
occurrence of the substring within the
string.
Commonly used String Functions
 strpos
in PHP : This function is used to
find the position of a substring within
a string. It takes two strings as input
and returns the position of the first
occurrence of the substring within the
string.
Commonly used String Functions
in PHP
 str_replace : This function is used to
replace all occurrences of a substring within a
string with another substring. It takes three
strings as input and returns a new string with
all occurrences of the second string replaced
with the third string.

This will replace all occurrences of the substring "fox" with the
substring "cat" within the string "The quick brown fox jumps
Using Strings to Represent Dates

In PHP, strings can be used to


represent dates and times.
The two main functions used
for working with dates and
times in PHP are `strtotime`
and `date`.
Using Strings to Represent Dates
 The strtotime() function
The `strtotime` function is used to convert a
string representation of a date and/or time into a
Unix timestamp. A Unix timestamp is a numeric
value representing the number of seconds since
January 1, 1970, 00:00:00 UTC. Here is an
example of using `strtotime`:

This will convert the string "July 22, 2023" into a Unix timestamp.
Using Strings to Represent Dates
 The date() function
The `date` function is used to
format a Unix timestamp into a
string representation of a date
and/or time. Here is an example
of using `date`:
Using Strings to Represent Dates
 Setting the timezone
By default, PHP uses the server's timezone
when working with dates and times.
However, it is possible to set a different
timezone using the
`date_default_timezone_set` function. Here
is an example, if you want to set the default
timezone to "Asia/Manila" (Philippines). The
correct timezone identifier for Manila is
"Asia/Manila.“
Arrays
In PHP, an array is a
variable that can hold
multiple values. Arrays
can be used to store
related data, such as a
Creating an Array
Arrays can be created using
the `array` function or using
square brackets. Here is an
example of creating an array
using the `array` function:

This will create an array named "names" with three


Displaying the Content of Arrays
The content of an array can
be displayed using the
`print_r` or `var_dump`
function. Here is an example
of using `print_r`:
Adding Elements to Arrays
Elements can be added to an
array using the `[]` operator or
the `array_push` function. Here
is an example of adding an
element to an array using the
`[]` operator:
Commonly used Array Functions
in PHP
PHP provides several
built-in functions for
working with arrays.
Here are some
commonly used array
Commonly used Array Functions
in PHP
`count`: This function is used to
get the number of elements in an
array. It takes an array as input
and returns the number of
elements in the array. Here is an
example of using `count`:
Commonly used Array Functions
in PHP
`sort`: This function is used to sort
an array in ascending order. It takes
an array as input and sorts the
elements in the array in ascending
order. Here is an example of using
`sort`:
Commonly used Array Functions
in PHP
`array_merge`: This function is used
to merge two or more arrays into a
single array. It takes two or more arrays
as input and returns a new array with
all the elements from the input arrays.
Here is an example of using
`array_merge`:
Commonly used Array Functions
in
It PHP
is important to note that the
array_merge() function does not
preserve numeric key values. If you
need to preserve the numeric keys,
then using the union operator (+) will
do that. Here is an example of using the
union operator:
Lesson 6

Control
Structures in
PHP
Control Structures in PHP

Control structures are like the


building blocks of decision-
making and looping in PHP. They
enable you to control the flow of
your program based on specific
conditions. Think of them as the
architects designing the path
If Statement
If Statement
The if statement is the most
fundamental control structure. It
allows you to execute a block of
code only if a given condition is
true. Imagine you are faced with a
fork in the road, and you want to
choose the right path. The if
statement helps you decide which
If Statement

In this example, the condition


`$num > 0` is evaluated, and if it's
true, the code inside the if block
will be executed, displaying "num is
If-Else
Statement
If-Else Statement
The if-else statement is an
extension of the if statement and
allows you to add an
alternative block of code to be
executed when the condition is
false. It provides a way to handle
two different paths of execution
based on the evaluation of the
If-Else Statement

The structure of the if-else


statement is as follows:
Here's how the if-else statement works:

1. The condition within


the parentheses is
evaluated. It can be any
expression that results
in a boolean value (true
Here's how the if-else statement works:

2. If the condition is
true, the code block
inside the first set of
curly braces
immediately following
Here's how the if-else statement works:

3. If the condition is
false, the code block
inside the else clause
(the second set of
If-Else Statement | Example:

In this example, the variable `$age` is assigned a value


of 25. The if-else statement checks whether the value of
`$age` is greater than or equal to 18. If the condition is
true (which it is in this case), the code inside the first
block will be executed, displaying "You are an adult." If
the condition is false (for example, if `$age` was 15), the
The if-else statement is a
powerful tool to handle two
distinct outcomes based on a
single condition. It allows
you to create dynamic and
responsive PHP scripts that
adapt to different scenarios
Ternary
Operator
Ternary Operator
The ternary operator is a
concise way of writing an
if-else statement. It's like
a quick decision maker,
allowing you to choose
between two options
Ternary Operator

Here, the ternary operator `($num >


0) ? "positive" : "negative"` checks if
`$num` is positive. If it is, the value
"positive" is assigned to `$result`;
otherwise, "negative" is assigned. The
Switch
Statement
Switch Statement
The switch statement is a
versatile control
structure, often used
when you have multiple
conditions to check. It's
like a decision tree with
Switch Statement
In this example,
the switch
statement checks
the value of
`$num` and
executes the
corresponding
case block. The
For Loop
For Loop
The for loop is a powerful
looping construct that allows
you to repeat a block of code
a specific number of times.
It's like a conveyor belt
moving items along a
production line; each
For Loop

Here, the for loop will execute the


block of code inside it five times,
displaying the values of `$i` from 0
Foreach Loop
Foreach Loop

The foreach loop is used


for iterating over the
elements of an array. It's
like a waiter serving each
dish from a platter to
every guest at a table.
Foreach Loop

In this example, the foreach loop


will iterate over the elements of
the `$names` array and display
While Loop
While Loop
The while loop is another
looping structure, which
executes a block of code
as long as a given
condition is true. It's like a
worker repeating a task
While Loop

The while loop will execute the block


of code inside it as long as the value of
`$num` is less than 5, displaying the
values of `$num` from 0 to 4.
Do-while Loop
Do-while Loop
The do-while loop is similar to
the while loop, but it guarantees
that the block of code is
executed at least once, even if
the condition is false. It's like a
rider getting on a roller coaster
ride and enjoying it at least once
do-While Loop

In this example, the do-while loop will


execute the block of code at least
once, displaying "The value of num is
0" before continuing as long as the
Other Topics
in Flow Control
Other Topics in Flow Control
Now that you have a solid
understanding of the core
control structures in PHP,
let's explore some
additional concepts to
enhance your control flow
Other Topics in Flow Control
1. Booleans
Booleans are simple yet powerful
data types in PHP. They represent
binary values, either true or false,
and play a significant role in
conditional statements and
decision-making. Booleans act like
on/off switches, determining
Other Topics in Flow Control
2 Break, Continue
The `break` and `continue` statements
offer fine-grained control over loops.
`break` allows you to exit a loop
prematurely, while `continue` skips the
rest of the current iteration and moves
on to the next one. These statements
are like emergency exits in loops,
providing a way to handle specific
Other Topics in Flow Control
3 Alternative Syntax
PHP provides an alternative syntax
for control structures that can
improve code readability, especially
in complex templates or HTML files.
The alternative syntax is like a
stylistic choice, allowing you to
create more visually appealing code
Other Topics in Flow Control
Instead of using the traditional curly
braces `{}` to define the beginning and
end of control structures, you can use
alternative syntax with keywords like
`if`, `else`, `for`, `foreach`, `while`, and
`do-while`. The alternative syntax
enhances the clarity of your code,
making it easier to distinguish PHP
logic from HTML markup when you are
The alternative syntax for an `if` statement
looks like this:
Similarly, the alternative syntax for a
`for` loop is:
The alternative syntax for a `foreach`
loop is:
The alternative syntax for a `while` loop
is:
And the alternative syntax for a `do-
while` loop is:
Here's an example of using the alternative
syntax in an HTML template:

In this example, the alternative syntax for


the `if-else` statement is used to
conditionally display different messages
based on the `$loggedIn` variable. The
alternative syntax helps clearly demarcate
Remember that using the
alternative syntax is entirely
optional, and you can choose
whichever syntax you find more
comfortable or suitable for your
coding style. It's essential to
maintain consistency throughout
your project to ensure code
readability and reduce potential
Other Topics in Flow Control
4 Displaying HTML code
One of the powerful features of PHP is
its ability to seamlessly integrate PHP
code with HTML, enabling dynamic
content generation in web applications.
This means you can embed PHP
variables, conditionals, and loops
directly within HTML templates,
creating interactive web pages with
Let's take a look at an example to
illustrate how PHP and HTML can
work together:
Let's take a look at an example to
illustrate how PHP and HTML can
work together:
In this example, we have a simple
HTML template that includes PHP
code sections enclosed within `<?
php ... ?>` tags. Inside the PHP
sections, we have defined a variable
`$username` and an `$age`. We
then use an `if-else` statement to
check whether the user is an adult
(age greater than or equal to 18) or
a kid (age less than 18). Depending
Furthermore, we embed
PHP variables directly
within the HTML content
using `<?php echo ... ?>`,
allowing us to display the
values of `$username`
Additionally, we use a
`foreach` loop to iterate
through the `$items`
array and display each
item as an HTML list item
(`<li>`) within an
This example demonstrates
the power of combining
PHP's control structures with
HTML to create dynamic and
interactive web pages,
providing a personalized
experience for users based
Remember, mastering control
structures in PHP is an essential
step towards becoming a proficient
PHP developer. By understanding
these concepts and practicing their
application, you'll gain the power to
control the flow of your programs
and create more efficient and
interactive PHP applications. Keep

You might also like