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 :