Practical Outcomes (Pros) :: Practical No 05: Strings in PHP
Practical Outcomes (Pros) :: Practical No 05: Strings in PHP
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.
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;
</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;
</body>
</html>
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>
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)
$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>
b) Answer:
<!DOCTYPE html>
<html>
<body>
<?php
$str = "met's institute of technology, polytechnic";
$s = ucwords($str);
print($s);
?>
</body></html>
Assessment Scheme
Performance Indicator Weightage
4 Presentation of Output 20 %
Assessment
Date Signature of
Marks Obtained
Subject Teacher
Process Related Product Related
Total (50 Marks)
(30 Marks) (20 Marks)