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

PHP Practical 5

The document outlines a practical assignment for manipulating strings in PHP, detailing string data types and built-in functions. It includes two programs: one for calculating string length and word count without built-in functions, and another demonstrating various built-in string functions. The provided code examples illustrate these concepts effectively.

Uploaded by

mohsin shaikh
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)
4 views3 pages

PHP Practical 5

The document outlines a practical assignment for manipulating strings in PHP, detailing string data types and built-in functions. It includes two programs: one for calculating string length and word count without built-in functions, and another demonstrating various built-in string functions. The provided code examples illustrate these concepts effectively.

Uploaded by

mohsin shaikh
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/ 3

Name: Shaikh Mudassir Class: CO6IB Batch: B2

Enroll: 2205690366 Subject: WBP Practical No:5

AIM: Write a program for manipulating Strings and using String Functions
THEORY:
In PHP, strings are sequences of characters enclosed within single, double, or backticks.
They are a fundamental data type and can be manipulated using a wide range of built-in
functions. These functions allow you to perform various operations, such as finding the
length of a string, concatenating strings, converting case (uppercase, lowercase),
extracting substrings, comparing strings, finding and replacing substrings, removing
whitespace, splitting strings into arrays, and joining array elements into a single string.

PROGRAM 1:
AIM: a) Calculate the length of the string
b) Count the number of words in string -without using string function

THEORY:
To determine the length of a string without using built-in functions, we can iterate through
each character in the string and increment a counter for each character encountered. To
count words, we can iterate through the string and increment a counter each time a space
is encountered. Since words are typically separated by spaces, the number of words is
generally one more than the number of spaces. These approaches demonstrate how to
perform basic string operations using fundamental programming concepts and without
relying on pre-built string functions.

CODE A):
<?php
$str1 = "Rupesh Kumar Shukla";
echo "Original string = ",$str1;
echo "<br>";
echo "Lenght of the string = ",strlen($str1);
?>

OUTPUT:
CODE B): Using user define function
<?php
function counting_words() {
$text = "Life is fight between you and what you want. The moment you stop fighting for
what you want, what you dont want will automatically take over";
$word_count = 1;
for ($i = 0; $i < strlen($text); $i++) {
if ($text[$i] === ' ') {
$word_count++;
}
}
echo "Actual String : $text <br>";
return $word_count;
}
$word_count = counting_words();
echo "Number of words: $word_count";
?>
OUTPUT:

#Using Pre-Define Function


<?php
function count_words($text) {
return str_word_count($text);
}
$my_string = "Rupesh Kumar Shukla";
echo "Actual String : $my_string <br>";
$word_count = count_words($my_string);
echo "Number of words: " . $word_count;
?>
OUTPUT:
PROGRAM 2:
AIM: Write a simple PHP program to demonstrate the use of various built-in string function

THEORY:
PHP program demonstrates the use of various built-in string functions. It starts by
defining a sample string. Then, it utilizes functions like strlen() to determine the string's
length, strtoupper() and strtolower() to convert the string to uppercase and lowercase,
respectively. It also demonstrates string concatenation using the dot operator (.),
extracting a substring using substr(), finding the position of a substring within the string
using strpos(), and replacing a substring with another using str_replace(). This example
showcases the versatility of PHP's string functions in manipulating and working with
textual data effectively.

CODE:
<?php
$str1 = "Bhutkun Kumar Chaturvedi";
echo "Original string = ",$str1;
echo "<br> <br>";
echo "Lenght of the string = ",strlen($str1);echo "<br>";
echo "Reversing the string = ",strrev($str1);echo "<br>";
echo "Changing the string into uppercase = ",strtoupper($str1);echo "<br>";
echo "Changing the string into lowercase= ",strtolower($str1);echo "<br>";
echo "Counting the words in the string = ",str_word_count($str1);echo "<br>";
echo "Changing the first word to uppercase= ",ucwords($str1);
?>
OUTPUT:

You might also like