0% found this document useful (0 votes)
6 views7 pages

Web Development Using PHP (4341604)

The document provides PHP scripts demonstrating various string handling and array functions. It includes examples for calculating string length, word count, reversing strings, replacing text, and manipulating arrays with functions like count, list, in_array, and sort. Each example is accompanied by HTML code and expected output.

Uploaded by

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

Web Development Using PHP (4341604)

The document provides PHP scripts demonstrating various string handling and array functions. It includes examples for calculating string length, word count, reversing strings, replacing text, and manipulating arrays with functions like count, list, in_array, and sort. Each example is accompanied by HTML code and expected output.

Uploaded by

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

WEB DEVELOPMENT USING PHP (4341604)

PRACTICAL 5:

1.Write PHP script to demonstrate use of various strings handling functions.

1.Length
<html>
<body>
<?php
echo"Length is ";
echo strlen("Hello world!");
?>
</body>
</html>
Output: Length is 12

2.Word Count
<html>
<body>
<?php
$text = "PHP is an amazing language!";
echo "Word Count: " . str_word_count($text);
?>
</body>
</html>
Output: 5

3.Reverse String
<html>
<body>
<?php
$text = "Hello";
echo "Reversed: " . strrev($text);
?>
</body>
</html>
Output: "olleH"
4.Replace
<html>
<body>
<?php
$text = "I love PHP!";
echo str_replace("PHP", "JavaScript", $text);
?>
</body>
</html>
Output: "I love JavaScript!"

5.Converting Cas
<html>
<body>
<?php
$text = "Hello World!";
echo strtoupper($text);
echo strtolower($text);
?>
</body>
</html>
Output: "HELLO WORLD!"
"hello world!"

5.Removing WhiteSpace
<html>
<body>
<?php
$text = " Hello World! ";
echo trim($text);
?>
</body>
</html>
Output: "Hello World!"

6.Extracting Substring
<html>
<body>
<?php
$text = "Hello World!";
echo substr($text, 6, 5);
?>
</body>
</html>
​ Output: "World"

—------------------------------------------------------------------------------------------------------------------

2.Write PHP script to demonstrate Array functions.

1.​ Count: Returns the number of elements in an array.


<!DOCTYPE html>
<html>
<body>
<?php
$cars=array("Volvo","BMW","Toyota");
echo count($cars);
?>
</body>
</html>
Output: 3

2.​ List: Assigns array values to variables.


<!DOCTYPE html>
<html>
<body>​
<?php
$my_array = array("Dog","Cat","Horse");
list($a, $b, $c) = $my_array;
echo "I have several animals, a $a, a $b and a $c.";
?>
</body>
</html>
Output:​ I have several animals, a Dog, a Cat and a Horse.

3.​ In_array: Checks if a value exists in an array.


<!DOCTYPE html>
<html>
<body>
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
if (in_array("Glenn", $people))
{
​ echo "Match found";
}
else
{
​ echo "Match not found";
}
?>
</body>
</html>
Output: Match found

4.​ Current: Returns the current element of an array.


<!DOCTYPE html>
<html>
<body>
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people) . "<br>";
?>
</body>
</html>
Output: Peter

5.​ Next: Returns the next element of an array.


<!DOCTYPE html>
<html>
<body>
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people) . "<br>";
echo next($people);
?>
</body>
</html>
Output: Peter
Joe

6.​ Previous: Returns the Previous element of an array.


<!DOCTYPE html>
<html>
<body>
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people) . "<br>";
echo next($people) . "<br>";
echo prev($people);
?>
</body>
</html>
Output: Peter
Joe
Peter

7.​ End: Returns the last element of an array.


<!DOCTYPE html>
<html>
<body>
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people) . "<br>";
echo end($people);
?>
</body>
</html>
Output: Peter
Cleveland

8.​ Each: Returns the current element key-value pair of the given array.
<!DOCTYPE html>
<html>
<body>
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
print_r (each($people));
?>
</body>
</html>
Output: Array ( [1] => Peter [value] => Peter [0] => 0 [key] => 0 )

9.​ Sort: Sorts an array in ascending order.


<!DOCTYPE html>
<html>
<body>
<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
foreach ($cars as $key => $val) {
echo $val."<br>";
}
?>
</body>
</html>
Output: BMW
Toyota
Volvo

10.​Array_merge: Merges two or more arrays into one.


<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
?>
</body>
</html>
Output: Array ( [0] => red [1] => green [2] => blue [3] => yellow )

11.​Array_reverse: Returns an array with elements in reverse order.


<!DOCTYPE html>
<html>
<body>
<?php
$a=array("a"=>"Volvo","b"=>"BMW","c"=>"Toyota");
print_r(array_reverse($a));
?>
</body>
</html>

Output: Array ( [c] => Toyota [b] => BMW [a] => Volvo )

You might also like