Hey,
This is Anup, And in this blog I will let you know about the PHP Questions & Answers.
1.User listout php programming assignment with example?
Calculator:---<?php
$num1 = 10;
$num2 = 5;
echo "Addition: " . ($num1 + $num2) . "<br>";
echo "Subtraction: " . ($num1 - $num2) . "<br>";
echo "Multiplication: " . ($num1 * $num2) . "<br>";
echo "Division: " . ($num1 / $num2) . "<br>";
?>
output:-addition-15
subtraction- 5
multiplication-50
division-2
2.User Registration Form:-
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$email = $_POST["email"];
$password = $_POST["password"];
// Process and store user data
// ...
echo "Registration successful!";
}
?>
<form method="post" action="">
Username: <input type="text" name="username"><br>
Email: <input type="email" name="email"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Register">
</form>
output-anup
anupr1945@gmail.com
1234anup
3..Prime Number Checker:
<?php
function isPrime($num) {
if ($num < 2) {
return false;
}
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) {
return false;
}
}
return true;
}
$number = 17; // Change to the desired number
if (isPrime($number)) {
echo "$number is a prime number.";
} else {
echo "$number is not a prime number.";
}
?>
output-17 is prime number
4.Email Validation:
<?php
function validateEmail($email) {
$pattern = '/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/';
return preg_match($pattern, $email);
}
$email = "anup@example.com"; // Change to the desired email
if (validateEmail($email)) {
echo "$email is a valid email address.";
} else {
echo "$email is not a valid email address.";
}
?>
output-anup@example.com is validation email
5.Password Strength Checker:
<?php
function checkPasswordStrength($password) {
$length = strlen($password);
$uppercase = preg_match('/[A-Z]/', $password);
$lowercase = preg_match('/[a-z]/', $password);
$digit = preg_match('/\d/', $password);
if ($length >= 8 && $uppercase && $lowercase && $digit) {
return "Strong password.";
} else {
return "Weak password. Please follow the password criteria.";
}
}
$password = "StrongPass123"; // Change to the desired password
echo checkPasswordStrength($password);
?>
output-anuprajak@123 match code
6.URL Extraction:
<?php
function extractURLs($text) {
$pattern = '/\b(?:https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i';
preg_match_all($pattern, $text, $matches);
return $matches[0];
}
$text = "Visit our website at https://example.com. For more info, check http://another-example.org";
$urls = extractURLs($text);
echo "Found URLs: " . implode(", ", $urls);
?>
output-Found URLs: https://example.com, http://another-example.org
7.String Reversal:
<?php
function reverseString($str) {
$reversed = '';
for ($i = strlen($str) - 1; $i >= 0; $i--) {
$reversed .= $str[$i];
}
return $reversed;
}
$originalString = "Hello, World!"; // Change to the desired string
echo "Reversed: " . reverseString($originalString);
?>
output--Reversed: !dlroW ,olleH
8.String Anagram Checker:
<?php
function areAnagrams($str1, $str2) {
$str1 = str_replace(' ', '', strtolower($str1));
$str2 = str_replace(' ', '', strtolower($str2));
return count_chars($str1, 1) == count_chars($str2, 1);
}
$string1 = "Listen";
$string2 = "Silent";
if (areAnagrams($string1, $string2)) {
echo "$string1 and $string2 are anagrams.";
} else {
echo "$string1 and $string2 are not anagrams.";
}
?>
output-Listen and Silent are anagrams
PHP Array Types:
There are 3 types of array in PHP.
- Indexed Array
- Associative Array
- Multidimensional Array
PHP Indexed Array:
PHP index mainly is represented by number which starts from 0. We can store number, string and object in the PHP array. All PHP array elements are assigned to an index number by default.
There are two ways to define indexed array:
1st way.
$season=array("summer","winter","spring","autumn");
2nd way.
$season[0]="summer";
$season[1]="winter";
$season[2]="spring";
$season[3]="autumn";