0% found this document useful (0 votes)
2 views12 pages

Practical 1

The document contains five practical PHP programming exercises completed by Devkar Mayur Mahadev, a BCA-SY student. Each exercise includes a specific task such as checking for prime numbers, finding the greater of two numbers, counting words in a string, counting word occurrences, and replacing a word in a string, along with the corresponding PHP code and outputs. The exercises demonstrate fundamental programming skills and string manipulation in PHP.

Uploaded by

mayurdevkar818
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)
2 views12 pages

Practical 1

The document contains five practical PHP programming exercises completed by Devkar Mayur Mahadev, a BCA-SY student. Each exercise includes a specific task such as checking for prime numbers, finding the greater of two numbers, counting words in a string, counting word occurrences, and replacing a word in a string, along with the corresponding PHP code and outputs. The exercises demonstrate fundamental programming skills and string manipulation in PHP.

Uploaded by

mayurdevkar818
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

PRACTICAL-1

Name :- Devkar Mayur mahadev


Class :- BCA-SY
Roll no :- 3934

Q.1 Write a PHP program to check whether the given number check prime or not.

<?php
function checkPrime($number) {
if ($number <= 1) {
return false;
}

for ($i = 2; $i * $i <= $number; $i++) {


if ($number % $i == 0) {
return false;
}
}

return true;
}

$givenNumber = 10;
if (checkPrime($givenNumber)) {
echo $givenNumber . " is a prime number.";
} else {
echo $givenNumber . " is not a prime number.";
}
?>
Output :
PRACTICAL-2

Name :- Devkar Mayur Mahadev


Class :- BCA-SY
Roll no :- 3934

Q.2 Write a PHP program to check the greater number from two number and display the
greater number.
<?php
function findGreater($num1, $num2) {
if ($num1 > $num2) {
return $num1;
} else {
return $num2;
}
}

$number1 = 45; // You can change this number


$number2 = 10; // You can change this number

$result = findGreater($number1, $number2);

echo "The greater number between $number1 and $number2 is: $result";
?>
Output :
PRACTICAL-3

Name :- Devkar mayur mahadev


Class :- BCA-SY
Roll no :- 3934

Q.3 Write a php program to count the total number of words in a string.

<?php
function countWords($inputString) {
$wordCount = str_word_count($inputString);
return $wordCount;
}

$string = "This is sample string. You can count the words this string.";

$totalWords = countWords($string);
echo "Total number of words in the string: $totalWords";
?>
Output :
PRACTICAL-4

Name :- Devkar mayur mahadev


Class :- BCA-SY
Roll no :- 3934

Q.4 Writeaphpprogramtofindtheoccuranceofwordsinastring.

<?php

functioncountWordOccurrences($inputString){

$inputString=strtolower(preg_replace('/[^a-zA-Z0-9\s]/', '',$inputString));

$words=explode('',$inputString);

$wordCount=array_count_values($words);

return$wordCount;

}
$string="[Link].";

$wordOccurrences=countWordOccurrences($string);

foreach($wordOccurrencesas$word=>$count){

echo"Theword'$word'occurs$counttimes<br>";

?>

Output:
PRACTICAL-5
Name :- Devkar mayur mahadev
Class :- BCA-SY
Roll no :-3934

Q.5 Write a php program to replace a word in a string.


<?php

function replaceWord($inputString, $oldWord, $newWord) {

$resultString = str_replace($oldWord, $newWord, $inputString);

return $resultString;

$string = " hello world This is a sample string. It contains the word 'sample'.";

$oldWord = "sample";

$newWord = "replacement";

$result = replaceWord($string, $oldWord, $newWord);

echo "Original string: $string <br>";

echo "String after replacement: $result";

?>

Output :

You might also like