0% found this document useful (0 votes)
9 views14 pages

Unit II Web Technology Notes

This document provides an overview of PHP conditional statements and loops, detailing their syntax and usage with examples. It covers the if, if...else, if...elseif...else, and switch statements for conditional logic, as well as while, do...while, for, and foreach loops for repetitive tasks. Additionally, it introduces string functions and numeric functions available in PHP, including examples for each function.

Uploaded by

gayathri murugan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views14 pages

Unit II Web Technology Notes

This document provides an overview of PHP conditional statements and loops, detailing their syntax and usage with examples. It covers the if, if...else, if...elseif...else, and switch statements for conditional logic, as well as while, do...while, for, and foreach loops for repetitive tasks. Additionally, it introduces string functions and numeric functions available in PHP, including examples for each function.

Uploaded by

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

UNIT II

PHP Conditional Statements

Very often when you write code, you want to perform different actions for
different conditions. You can use conditional statements in your code to do this.

In PHP we have the following conditional statements:


• if statement - executes some code if one condition is true
• if...else statement - executes some code if a condition is true and another code
if that condition is false
• if...elseif....else statement - executes different codes for more than two conditions
• switch statement - selects one of many blocks of code to be executed.

The if Statement
The if statement executes some code if one condition is true.

Syntax
if (condition) {
code to be executed if condition is true;
}
The example below will output "Have a good day!" if the current time (HOUR) is
less than 20:
Example
<html>
<body>
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
}
?>
</body>
</html>
OUTPUT:
Have a good day!
The if...else Statement

The if....else statement executes some code if a condition is true and another code
if that condition is false.
Syntax
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
The example below will output "Have a good day!" if the current time is less than
20,
and "Have a good night!" otherwise:
Example
<html>
<body>
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
</body>
</html>
OUTPUT:
Have a good day!

The if... elseif ....else Statement

Syntax
elseif...
if (condition) {
else statement executes different codes for more than two conditions.
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if this condition is true;
} else {
code to be executed if all conditions are false;
}
The example below will output "Have a good morning!" if the current time is less
than
10, and "Have a good day!" if the current time is less than 20. Otherwise it will
output
"Have a good night!":

Example
<html>
<body>
<?php
$t = date("H");
echo "<p>The hour (of the server) is " . $t;
echo ", and will give the following message:</p>";
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
</body>
</html>
OUTPUT:
The hour (of the server) is 01, and will give the following message:
Have a good morning!
The switch Statement
• The switch statement is used to perform different actions based on
different conditions.
Use the switch statement to select one of many blocks of code to be executed.
Syntax
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
This is how it works: First we have a single expression n (most often a variable),
that is
evaluated once. The value of the expression is then compared with the values for
each
case in the structure. If there is a match, the block of code associated with that case
is
executed. Use break to prevent the code from running into the next case
automatically.
The default statement is used if no match is found.
Example
<html>
<body>
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
</body>
</html>
OUTPUT:
Your favorite color is red!

PHP Loops

Often when you write code, you want the same block of code to run over and over
again
in a row. Instead of adding several almost equal code-lines in a script, we can use
loops
to perform a task like this.

In PHP, we have the following looping statements:


• while- loops through a block of code as long as the specified condition is true
• do...while - loops through a block of code once, and then repeats the loop as
long as the specified condition is true
• for- loops through a block of code a specified number of times
• foreach- loops through a block of code for each element in an array
The PHP while Loop
• The while loop executes a block of code as long as the specified condition is
true.
Syntax
while (condition is true) {
code to be executed;
}
The example below first sets a variable $x to 1 ($x = 1). Then, the while loop will
continue to run as long as $x is less than, or equal to 5 ($x <= 5). $x will increase
by 1
each time the loop runs ($x++):
Example
<html>
<body>
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x
<br>"; $x++;
}
?>
</body>
</html>

OUTPUT:
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5

The PHP do...while Loop

The do...while loop will always execute the block of code once, it will then check
the
condition, and repeat the loop while the specified condition is true.
Syntax
do {
code to be executed; }
while (condition is true);
The example below first sets a variable $x to 1 ($x = 1). Then, the do while loop
will
write some output, and then increment the variable $x with 1. Then the condition is
checked (is $x less than, or equal to 5?), and the loop will continue to run as long
as $x
is less than, or equal to 5:
Example
<html>
<body>
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <=
5); ?>
</body>
</html>

OUTPUT:
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5

Notice that in a do while loop the condition is tested AFTER executing the
statements
within the loop. This means that the do while loop would execute its statements at
least
once, even if the condition is false the first time.
The example below sets the $x variable to 6, then it runs the loop, and then the
condition is checked:

Example
<html>
<body>
<?php
$x = 6;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
</body>
</html>
OUTPUT:
The number is: 6

for Loops

• PHP for loops execute a block of code a specified number of times.


The PHP for Loop
• The for loop is used when you know in advance how many times the script
should run.
Syntax
for (init counter; test counter; increment counter) {
code to be executed;
}
Parameters:
• init counter: Initialize the loop counter value
• test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the
loop continues. If it evaluates to FALSE, the loop ends.
• increment counter: Increases the loop counter value
The example below displays the numbers from 0 to 10:
Example
<html>
<body>
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
</body>
</html>
OUTPUT:
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10

The PHP foreach Loop


• The foreach loop works only on arrays, and is used to loop through each
key/value pair in an array.
Syntax
foreach ($array as $value)
{
}
code to be executed;
• For every loop iteration, the value of the current array element is assigned to
$value and the array pointer is moved by one, until it reaches the last array
element.
• The following example demonstrates a loop that will output the values of the
given array ($colors):
Example
<html>
<body>
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value)
{ echo "$value <br>";
}
?>
</body>
</html>
OUTPUT:
red
green
blue
yellow

PHP String
• 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
<html>
<body>
<?php
$x = "Hello world!";
$y = 'Hello world!';
echo $x;
echo "<br>";
echo $y;
?>
</body>
</html>
OUTPUT:
Hello world!
Hello world!

String Functions
• Get The Length of a String
• The PHP strlen() function returns the length of a string.
• The example below returns the length of the string "Hello world!":
Example
<html>
<body>
<?php
echo strlen("Hello
world!"); ?>
</body>
</html>
OUPUT:
12

Count
The Number of Words in a String
The PHP str_word_count() function counts the number of words in a string:
Example
<html>
<body>
<?php
echo str_word_count("Hello world!");
?>
</body>
</html>
OUPUT:
2

Reverse a String
• The PHP strrev() function reverses a string:
Example
<html>
<body>
<?php
echo strrev("Hello world!");
?>
</body>
</html>
OUTPUT:
!dlrow olleH

Search For a Specific Text Within a String


• The PHP strpos() function searches for a specific text within a string.
• If a match is found, the function returns the character position of the first match.
If
no match is found, it will return FALSE.
• The example below searches for the text "world" in the string "Hello world!":
Example
<html>
<body>
<?php
echo strpos("Hello world!", "world");
?>
</body>
</html>
OUPUT:
6

Replace Text Within a String


• The PHP str_replace() function replaces some characters with some other
characters in a string.
• The example below replaces the text "world" with "Dolly":
Example
<html>
<body>
<?php
echo str_replace("world", "Dolly", "Hello world!");
?>
</body>
</html>
OUPUT:
Hello Dolly!

Numeric Functions
abs() :
Returns the absolute value of the number.
Syntax:
abs(number);
Example;
<?php
echo abs(5)."<br />";
echo abs(-5)."<br />";
echo abs(8.5)."<br />";
echo abs(-8.5)."<br />";
?>
Output Will be :
5
5
8.5
8.5
pi() :
Return the value of pi.
Syntax:
pi();
Example :
<?php
echo(pi());
?>
Output will be
3.1415926535898

round() :
Rounds a number to nearest integer.
Syntax
round($number, $precision, $mode);
$number : Number which you wnat to round
$precision : Optional Parameter. It specifies the number of
decimal digits to round to. The default value of this parameter is
zero.
$mode : Rounding mode.
Possible values :
PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN,
PHP_ROUND_HALF_EVEN, PHP_ROUND_HALF_ODD.
Example :
<?php
echo round(8.4)."<br />";
//Output will be 8
echo round(8.5)."<br />";
//Output will be 9
echo round(8.5,0)."<br />";
//Output will be 9
echo round(10.4556,2)."<br />";
//Output will be 10.46
echo round(0.45)."<br />";
//Output will be 0
echo round(0.65)."<br />";
//Output will be 1
echo round(-1.45)."<br />";
//Output will be 1
?>

sqrt() :
Retruns the sqaure root of a number.
Syntax :
sqrt(<em>number</em>);
Example :
<?php
echo sqrt(1);
//Output will be 1
echo sqrt(2);
//Output will be 4
echo sqrt(5);
//Output will be 25
echo sqrt(-);
//Output will be NAN
?>

You might also like