1. Write a PHP script to print 'Hello, World!'.
<?php
echo 'Hello, World!';
?>
Output: Hello, World!
2. Write a PHP script to add two numbers.
<?php
$a = 5;
$b = 10;
$sum = $a + $b;
echo $sum;
?>
Output: 15
3. Write a PHP script to find the length of a string.
<?php
$str = 'Hello';
echo strlen($str);
?>
Output: 5
4. Write a PHP script to reverse a string.
<?php
$str = 'Hello';
echo strrev($str);
?>
Output: olleH
5. Write a PHP script to check if a number is even or odd.
<?php
$num = 4;
echo ($num % 2 == 0) ? 'Even' : 'Odd';
?>
Output: Even
6. Write a PHP script to swap two variables.
<?php
$a = 5;
$b = 10;
$temp = $a;
$a = $b;
$b = $temp;
echo "a = $a, b = $b";
?>
Output: a = 10, b = 5
7. Write a PHP script to find the largest of three numbers.
<?php
$a = 10; $b = 20; $c = 15;
echo max($a, $b, $c);
?>
Output: 20
8. Write a PHP script to check if a number is positive, negative or zero.
<?php
$num = -5;
if ($num > 0) echo 'Positive';
elseif ($num < 0) echo 'Negative';
else echo 'Zero';
?>
Output: Negative
9. Write a PHP script to print numbers from 1 to 5 using a loop.
<?php
for ($i = 1; $i <= 5; $i++) {
echo $i . ' ';
?>
Output: 1 2 3 4 5
10. Write a PHP script to create an array and display its elements.
<?php
$arr = array(1, 2, 3);
foreach ($arr as $value) {
echo $value . ' ';
?>
Output: 1 2 3
11. Write a PHP script to check if a key exists in an associative array.
<?php
$arr = array("a" => 1, "b" => 2);
echo array_key_exists("a", $arr) ? 'Exists' : 'Not Exists';
?>
Output: Exists
12. Write a PHP script to sort an array in ascending order.
<?php
$arr = array(3, 1, 2);
sort($arr);
foreach ($arr as $val) {
echo $val . ' ';
?>
Output: 1 2 3