Practical Exam Questions
Practical Exam Questions
1) Write PHP program to print even numbers upto 10 and sum of them.
<?php
$sum = 0;
for ($i = 2; $i <= 10; $i += 2) {
echo $i . " ";
$sum += $i;
}
echo "<br>Sum of even numbers: " . $sum;
?>
<?php
function reverse($number)
{
/* writes number into string. */
$num = (string) $number;
/* Reverse the string. */
$revstr = strrev($num);
/* writes string into int. */
$reverse = (int) $revstr;
return $reverse;
}
echo reverse(23456);
?>
echo "<pre>";
print_r(get_class_vars(get_class($obj))); // Output: Array ( [var1] => [var2] =>
constant )
echo "</pre>";
echo "<pre>";
print_r(get_object_vars($obj)); // Output: Array ( [var1] => [var2] => constant )
echo "</pre>";
echo "<pre>";
print_r(get_class_methods('MyClass')); // Output: Array ( [0] => myMethod )
echo "</pre>";
echo "<pre>";
print_r(get_class_methods($obj)); // Output: Array ( [0] => myMethod )
echo "</pre>";
?>
session_unset();
session_destroy();
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
12) Write PHP program based on select a table from database.
<?php
// Database connection parameters
$servername = "localhost"; // Change this to your database server
$username = "username"; // Change this to your database username
$password = "password"; // Change this to your database password
$database = "database_name"; // Change this to your database name
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Execute query
$result = $conn->query($sql);
// Close connection
$conn->close();
?>
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Close connection
$conn->close();
?>
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Close connection
$conn->close();
?>
16) Write PHP program based on string functions (Any four)
<?php
// Sample string
$string = "Hello, World!";
// 1. Addition
$sum = $num1 + $num2;
echo "Sum: $sum<br>";
// 2. Subtraction
$diff = $num1 - $num2;
echo "Difference: $diff<br>";
// 3. Multiplication
$product = $num1 * $num2;
echo "Product: $product<br>";
// 4. Division
$quotient = $num1 / $num2;
echo "Quotient: $quotient<br>";
// 5. Square root
$sqrt = sqrt($num1);
echo "Square root of $num1: $sqrt<br>";
// 6. Exponential
$exp = pow($num1, $num2);
echo "$num1 raised to the power of $num2: $exp<br>";
// 7. Absolute value
$abs = abs(-10);
echo "Absolute value of -10: $abs<br>";