0% found this document useful (0 votes)
272 views

Practical Outcomes (Pros) :: Practical No 05: Strings in PHP

This document discusses strings in PHP. It provides examples of how to calculate the length of a string, count words in a string, and use built-in string functions without string functions. It also includes sample code to demonstrate various string functions and exercises to concatenate strings and convert strings to title case without string functions. Assessment criteria for a practical assignment on strings in PHP is outlined.

Uploaded by

Rutuja Bhagat
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)
272 views

Practical Outcomes (Pros) :: Practical No 05: Strings in PHP

This document discusses strings in PHP. It provides examples of how to calculate the length of a string, count words in a string, and use built-in string functions without string functions. It also includes sample code to demonstrate various string functions and exercises to concatenate strings and convert strings to title case without string functions. Assessment criteria for a practical assignment on strings in PHP is outlined.

Uploaded by

Rutuja Bhagat
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/ 6

Practical No 05: Strings in PHP

Practical Outcomes (PrOs):


a. Write a PHP program to –
i. Calculate length of string.
ii. Count the number of words in string -without using string function.
b. Write a simple PHP program to demonstrate use of various built-in string functions.

A. Practical Significance
PHP supports strings. It provides rich set of string functions for programmers to manipulate
with the strings. In web-development the strings play much important role in scripting, such as,
inserting some html element in web page or forming queries to be fired on database etc.

B. Minimum Theoretical Background


There are two ways to create strings in PHP – using single quote and using double quote as
shown below –
$str = 'India is my country.';
Or
$str = "India is my country.";
In PHP, the string can be treated as array of characters and can be processed like an array as
shown below –
$name = "Janhavi";
for($i=0; $name[$i] != ''; $i++)
{
echo $name[$i] . "</br>";
}
The above program segment prints string character by character each on new line.

String functions in PHP –


1. The str_word_count() function counts the number of words in a string.
2. The strlen() function returns the length of a string.
3. The strrev() function reverses a string.
4. The strpos() function finds the position of the first occurrence of a string inside another string.
5. The str_replace() function replaces some characters with some other characters in a string.
6. The ucwords() function converts the first character of each word in a string to uppercase.
7. The strtolower() function converts a string to lowercase.
8. The strtoupper() function converts a string to uppercase.
9. The strcmp() function compares two strings.

C. Resources Used
A desktop personal computer having Windows 8.1 Professional Operating System,
Intel Core i3 Processor with speed of 2.4 GHz and 4GB RAM.
Editor and parser at –
https://www.w3schools.com/php/phptryit.asp?filename=tryphp_intro
D. Program Code
a) Write a php program to calculate the length of string without using string function.
<!DOCTYPE html>
<html> <body>
<?php
$str = 'Avantika';
$length = 0;

for($i=0; $str[$i] != ''; $i++)


{
$length++;
}
print("Length of the given string: " . $length);
?>

</body>
</html>

b) Write a php program to count the number of words in string without using string function.
<!DOCTYPE html>
<html>
<body>

<?php
$str = "India is my country.";
$no_of_words = 0;

for($i=0; $str[$i] != ''; $i++)


{
if($str[$i] == ' ')
{
$no_of_words++;
}
}
$no_of_words++; // last word ...

print("The number of words are " . $no_of_words);


?>

</body>
</html>

E. Result (Output of Code)


Program a) Length of the given string: 8
Program b) The number of words are 4
F. Practical Related Questions
a) Which string function is used in php for displaying or extracting the substring from a string from
given position? Explain.
b) State the use of str_repeat() function with example.
c) Write a php program to check whether given two strings are equal using string function.

a) Answer: In PHP, the substr() is the function used to displaying or extracting the substring from a
string from given position.
The function can be used in given two ways –
1. substr(string, start) - Such use of function displays or extracts substring from given start
position.
For example,
<!DOCTYPE html>
<html>
<body>

<?php
$str = substr("India is my country.", 9);
echo $str . "</br>";
?>

</body>
</html>

Output: my country.

2. substr(string, start, length) - Such use of function displays or extracts substring from given
start position and of given length.
For example,
<!DOCTYPE html>
<html>
<body>

<?php
$str = substr("Yesterday Sameera went to America.",
10, 7);
echo $str . "</br>";
?>

</body>
</html>

Output: Sameera
b) Answer: In PHP, the str_repeat() is the function that repeats the given string for specific number
of times.
For example,
<!DOCTYPE html>
<html>
<body>

<?php
$str = str_repeat("My Pen ", 5);
echo $str . "</br>";
?>

</body>
</html>

Output: My Pen My Pen My Pen My Pen My Pen

c) Answer: Program for string comparisons.


<!DOCTYPE html>
<html>
<body>
<?php
$str1 = "Ravina is an actress.";
$str2 = "Ravina is an actress.";
$str3 = "Ravina is a cricketer.";

if(strcmp($str1, $str2) == 0) {
echo "Strings are equal.</br>";
}
else {
echo "Strings aren't equal.</br>";
}

if(strcmp($str1, $str3) == 0) {
echo "Strings are equal.</br>";
}
else {
echo "Strings aren't equal.</br>";
}
?>
</body>
</html>

Output:
Strings are equal.
Strings aren't equal.
G. Exercise
a) Write a php program to concatenate strings without using string function. (Write output)
b) Write a php program to convert the given string in title-case. (Write output)

a) Answer: Program for concatenation of strings –


<!DOCTYPE html>
<html>
<body>
<?php
$str1 = 'Rasika';
$str2 = ' ';
$str3 = 'Kamankar';

$i = 0;
while($str1[$i] != '') {
$i++;
}
for($j=0; $str2[$j] != ''; $j++) {
$str1[$i] = $str2[$j];
$i++;
}
for($j=0; $str3[$j] != ''; $j++) {
$str1[$i] = $str3[$j];
$i++;
}

print($str1);
?>
</body></html>

Output Rasika Kamankar

b) Answer:
<!DOCTYPE html>
<html>
<body>
<?php
$str = "met's institute of technology, polytechnic";

$s = ucwords($str);
print($s);
?>
</body></html>

Output: Met's Institute Of Technology, Polytechnic


c) Explain in brief – Formatting of string in php.
Like many other languages, PHP features the versatile printf() and sprintf() functions that you
can use to format strings in many different ways. These functions are handy when you need convert
data between different formats — either to make it easy for people to read, or for passing to another
program. PHP features many other functions to format strings in specific ways — for example, the
date() function is ideal for formatting date strings. However, printf() and sprintf() are great for
general-purpose formatting. The printf() function outputs a formatted string. The sprintf() function
writes a formatted string to a variable.

Assessment Scheme
Performance Indicator Weightage

Process Related (30 Marks) 60 %


Write appropriate code to generate desired output
1 30 %
in Web application.
2 Debug, Test and Execute the programs 20 %

3 Following Ethical Practice 10 %

Product Related (20 Marks) 40 %

4 Presentation of Output 20 %

5 Timely Submission of Practical Assignment 10 %

6 Able to answer the sample questions 10 %

Total (50 Marks) 100 %

List of Students / Team Members


1) ………………………………………………………………………………….
2) ………………………………………………………………………………….
3) ………………………………………………………………………………….
4) ………………………………………………………………………………….

Assessment
Date Signature of
Marks Obtained
Subject Teacher
Process Related Product Related
Total (50 Marks)
(30 Marks) (20 Marks)

You might also like