0% found this document useful (0 votes)
7 views7 pages

PHP Programming Basics and Examples

The document contains a list of programming tasks and examples related to basic HTML and PHP programming. It includes topics such as checking even/odd numbers, calculating net amounts with discounts, finding the largest number, and generating Fibonacci series. Additionally, it covers string manipulation and creating a simple calculator using user input.

Uploaded by

nileshbharti894
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)
7 views7 pages

PHP Programming Basics and Examples

The document contains a list of programming tasks and examples related to basic HTML and PHP programming. It includes topics such as checking even/odd numbers, calculating net amounts with discounts, finding the largest number, and generating Fibonacci series. Additionally, it covers string manipulation and creating a simple calculator using user input.

Uploaded by

nileshbharti894
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

[Link] Topics Page No.

1 Introduction to Basic HTML Tags 1

2 Write a Program to check and print whether a given number is even 1


or odd.

3 Write a program to compute net amount from the given quantity 1


purchased and rate per quantity. Discount @10% is allowed if the
quantity purchased exceeds 100
4 Write a program to find largest among three numbers using ternary 2
operators.

5 Write a program to print sum of digits of a given number. (using 2


while loop)

6 Write a program to print Fibonacci series upto a given number. 2

7 Write a program to enter numbers till the user wants. At the end it 3
should display the count of positive, negative and zeros entered.
(Using do-while loop)

8 Write a function countWords ($str) that takes any string of 4


characters and finds the Number of times each word occurs. You
should ignore the distinction between capital and lowercase letters
9 Create a form with one text field and submit buttons for string 4
length, string reverse and uppercase, lowercase, string replace.
Display the result accordingly.

10 Write a Menu-Driven program to implement a calculator which 5


performs only addition, subtraction, multiplication and division. The
operation should happen based on the user choice. (use switch case)
1. Introduction to basic HTML tags.

<!DOCTYPE html>
<html>
<head>
<title>Basic HTML Tags</title>
</head>
<body>
<h1>R22CA1CA0135</h1>
<p>PRATIK.</p>
<a href="[Link] Example</a>
<img src="[Link] alt="Sample Image">
<ul>
<li>R22CA1CA0135</li>
<li>Item Two</li>
</ul>
</body>
</html>

2. Write a Program to check and print whether a given number is even or odd.

<?php
$number = 7;
if ($number % 2 == 0) {
echo "$number is Even";
} else {
echo "$number is Odd";
}
?>

1
3. Write a program to compute net amount from the given quantity
purchased and rate per quantity. Discount @10% is allowed if the
quantity purchased exceeds 100.

<?php
$qty = 120;
$rate = 50;
$total = $qty * $rate;

if ($qty > 100) {


$discount = $total * 0.10;
} else {
$discount = 0;
}

$netAmount = $total - $discount;


echo "Net Amount: Rs. $netAmount";
?>

4. Write a program to find largest among three numbers using ternary


operators.

<?php
$a = 10; $b = 20; $c = 15;
$largest = ($a > $b) ? (($a > $c) ? $a : $c) : (($b > $c) ? $b : $c);
echo "Largest number is: $largest";
?>

5. Write a program to print sum of digits of a given number. (using while


loop)

<?php
$num = 1234;
$sum = 0;

while ($num > 0) {

2
$digit = $num % 10;
$sum += $digit;
$num = floor($num / 10);
}

echo "Sum of digits: $sum";


?>

6. Write a program to print Fibonacci series upto a given number.

<?php
$n = 10;
$a = 0;
$b = 1;
echo "$a $b ";

for ($i = 2; $i < $n; $i++) {


$c = $a + $b;
echo "$c ";
$a = $b;
$b = $c;
}
?>

7. Write a program to enter numbers till the user wants. At the end it
should display the count of positive, negative and zeros entered. (Using
do-while loop)

<?php
$positive = $negative = $zeros = 0;

do {
$num = (int)readline("Enter a number (type 999 to stop): ");

if ($num == 999) break;

3
if ($num > 0) $positive++;
elseif ($num < 0) $negative++;
else $zeros++;
} while (true);

echo "Positive: $positive\nNegative: $negative\nZeros: $zeros";


?>

8. Write a function countWords ($str) that takes any string of characters


and finds the Number of times each word occurs. You should ignore the
distinction between capital and lowercase letters.

<?php
function countWords($str) {
$str = strtolower($str);
$words = str_word_count($str, 1);
$counted = array_count_values($words);
foreach ($counted as $word => $count) {
echo "$word: $count<br>";
}
}

countWords("Hello hello world! This is a world test.");


?>

9. Create a form with one text field and submit buttons for string length,
string reverse and uppercase, lowercase, string replace. Display the
result accordingly.

<!-- HTML Form -->


<form method="post">
<input type="text" name="inputStr" required>
<button name="action" value="length">Length</button>
<button name="action" value="reverse">Reverse</button>
<button name="action" value="upper">Uppercase</button>
<button name="action" value="lower">Lowercase</button>
<button name="action" value="replace">Replace 'a' with '@'</button>
4
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$str = $_POST["inputStr"];
$action = $_POST["action"];

switch ($action) {
case "length":
echo "Length: " . strlen($str);
break;
case "reverse":
echo "Reversed: " . strrev($str);
break;
case "upper":
echo "Uppercase: " . strtoupper($str);
break;
case "lower":
echo "Lowercase: " . strtolower($str);
break;
case "replace":
echo "Replaced: " . str_replace("a", "@", $str);
break;
}
}
?>

10. Write a Menu-Driven program to implement a calculator which performs


only addition, subtraction, multiplication and division. The operation
should happen based on the user choice. (use switch case)

<?php
$a = 20;
$b = 10;
$choice = "multiply";

switch ($choice) {
case "add":

5
echo "Addition: " . ($a + $b);
break;
case "subtract":
echo "Subtraction: " . ($a - $b);
break;
case "multiply":
echo "Multiplication: " . ($a * $b);
break;
case "divide":
if ($b != 0) {
echo "Division: " . ($a / $b);
} else {
echo "Division by zero not allowed.";
}
break;
default:
echo "Invalid Choice";
}
?>

You might also like