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

More PHP Assignment Questions

The document contains a series of PHP scripts demonstrating various programming tasks, including counting vowels in a string, converting a string to uppercase, checking for prime numbers, calculating factorials, merging arrays, getting the current date and time, adding two numbers, and removing duplicates from an array. Each script is accompanied by example output. The examples illustrate fundamental PHP functionalities and operations.

Uploaded by

anagha16nair
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)
12 views3 pages

More PHP Assignment Questions

The document contains a series of PHP scripts demonstrating various programming tasks, including counting vowels in a string, converting a string to uppercase, checking for prime numbers, calculating factorials, merging arrays, getting the current date and time, adding two numbers, and removing duplicates from an array. Each script is accompanied by example output. The examples illustrate fundamental PHP functionalities and operations.

Uploaded by

anagha16nair
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

13. Write a PHP script to count the number of vowels in a string.

<?php

$str = 'hello world';

preg_match_all('/[aeiou]/i', $str, $matches);

echo count($matches[0]);

?>

Output: 3

14. Write a PHP script to convert a string to uppercase.

<?php

$str = 'hello';

echo strtoupper($str);

?>

Output: HELLO

15. Write a PHP script to check if a number is prime.

<?php

$num = 7;

$is_prime = true;

if ($num <= 1) $is_prime = false;

for ($i = 2; $i <= sqrt($num); $i++) {

if ($num % $i == 0) {

$is_prime = false;

break;

echo $is_prime ? 'Prime' : 'Not Prime';

?>

Output: Prime

16. Write a PHP script to calculate the factorial of a number.

<?php

$num = 5;
$fact = 1;

for ($i = 1; $i <= $num; $i++) {

$fact *= $i;

echo $fact;

?>

Output: 120

17. Write a PHP script to merge two arrays.

<?php

$a1 = array(1, 2);

$a2 = array(3, 4);

$merged = array_merge($a1, $a2);

print_r($merged);

?>

Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )

18. Write a PHP script to get the current date and time.

<?php

echo date('Y-m-d H:i:s');

?>

Output: 2025-04-22 [Link] (example)

19. Write a PHP script to create a function that adds two numbers.

<?php

function add($a, $b) {

return $a + $b;

echo add(3, 4);

?>

Output: 7

20. Write a PHP script to remove duplicate values from an array.


<?php

$arr = array(1, 2, 2, 3);

$unique = array_unique($arr);

print_r($unique);

?>

Output: Array ( [0] => 1 [1] => 2 [3] => 3 )

You might also like