0% found this document useful (0 votes)
105 views3 pages

Practical No 4 (WBP)

This document contains 3 questions from a PHP practical assignment. Question 1 asks to write a program to calculate the length of a string. Question 2 asks to count the number of words in a string without string functions. Question 3 demonstrates various built-in PHP string functions like strlen(), strtoupper(), str_replace(), and strpos(). Code snippets with sample output are provided for each question.

Uploaded by

dishabalsaraf
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)
105 views3 pages

Practical No 4 (WBP)

This document contains 3 questions from a PHP practical assignment. Question 1 asks to write a program to calculate the length of a string. Question 2 asks to count the number of words in a string without string functions. Question 3 demonstrates various built-in PHP string functions like strlen(), strtoupper(), str_replace(), and strpos(). Code snippets with sample output are provided for each question.

Uploaded by

dishabalsaraf
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/ 3

Name: Balsaraf Disha Sampat

Roll no: 06
Batch: C1
Practical no: 4

Q1. Write a PHP program to calculate length of String.


<html>
<head>
<title> String Functions</title>
</head>
<body>
<?php
$str="Welcome to PHP";
$res=strlen($str);
echo"Length of String is ".$res;
?>
</body>
</html>

Output:

Q2. Write a PHP program to count the number of words in string without using string
functions.
<html>
<head>
<title>Hello</title>
</head>
<body>
<?php
function get_str_num_count($string)
{
$string=preg_replace('/\s+/',' ',trim($string));
$words=explode(" ",$string);
return count($words);
}
$str="jaihind Polytechnic";
$len=get_str_num_count($str);
echo"Given string: ".$str;
echo"<br>no of words is: ",$len;
?>
</body>
</html>
Output:

Q3. Write a simple PHP program to demonstrate use of various built-in string function.
<html>
<head>
<title>String Function</title>
</head>
<body>
<?php
$string = "Hello, World! This is a PHP string functions example.";
$str="PHP";
echo"String length: <br>";
$length = strlen($string);
echo "String Length: $length<br>";

echo"Convert to uppercase: <br>";


$uppercase = strtoupper($string);
echo "Uppercase: $uppercase<br>";

echo"Convert to lowercase: <br>";


$lowercase = strtolower($string);
echo "Lowercase: $lowercase<br>";

echo"ucwords: <br>";
$ucwords = ucwords($string); // Get first 10 characters
echo "ucwords: $ucwords<br>";

echo"Replace:<br>";
$replace = str_replace('PHP', 'JavaScript', $string);
echo "Replace: $replace<br>";

echo"Comparision: <br>";
$strcmp = strcmp($string,$str);
echo "Compared: $strcmp<br>";

echo"Word Count: <br>";


$wordCount = str_word_count($string);
echo "Word Count: $wordCount<br>";

echo"Position of PHP: <br>";


$position = strpos($string, 'PHP');
echo "Position of 'PHP': $position<br>";

echo"Reverse: <br>";
$reversed = strrev($string);
echo "Reversed: $reversed<br>";
?>
</body>
</html>

Output:

You might also like