0% found this document useful (0 votes)
16 views6 pages

PHP Practical Solution

The document contains practical PHP code snippets for various programming tasks such as calculating sums of even numbers, finding prime numbers, reversing numbers, working with arrays, and performing database operations. It also includes theoretical questions comparing concepts like sessions vs cookies and GET vs POST methods. Additionally, it covers user-defined functions, cloning, serialization, and input validation.

Uploaded by

leomanthan10
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)
16 views6 pages

PHP Practical Solution

The document contains practical PHP code snippets for various programming tasks such as calculating sums of even numbers, finding prime numbers, reversing numbers, working with arrays, and performing database operations. It also includes theoretical questions comparing concepts like sessions vs cookies and GET vs POST methods. Additionally, it covers user-defined functions, cloning, serialization, and input validation.

Uploaded by

leomanthan10
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
You are on page 1/ 6

Practical Questions

1) Even numbers up to 10 and sum

php
CopyEdit
<?php
$sum = 0;
for ($i = 1; $i <= 10; $i++) {
if ($i % 2 == 0) {
echo $i . " ";
$sum += $i;
}
}
echo "<br>Sum = " . $sum;
?>

2) Prime numbers up to n

php
CopyEdit
<?php
$n = 20;
for ($i = 2; $i <= $n; $i++) {
$prime = true;
for ($j = 2; $j <= sqrt($i); $j++) {
if ($i % $j == 0) {
$prime = false;
break;
}
}
if ($prime) echo $i . " ";
}
?>

3) Reverse of a number

php
CopyEdit
<?php
$num = 12345;
$rev = 0;
while ($num > 0) {
$rev = ($rev * 10) + ($num % 10);
$num = (int)($num / 10);
}
echo "Reversed number: " . $rev;
?>

4) Multidimensional array

php
CopyEdit
<?php
$students = [
["Name" => "Alice", "Age" => 20],
["Name" => "Bob", "Age" => 22],
];
foreach ($students as $student) {
echo $student["Name"] . " is " . $student["Age"] . " years old<br>";
}
?>

5) Sort associative and multidimensional array

php
CopyEdit
<?php
// Associative sort
$assoc = ["b" => 2, "a" => 1];
asort($assoc);
print_r($assoc);

// Multidimensional sort by age


$students = [
["name" => "Zara", "age" => 20],
["name" => "Ali", "age" => 19],
];
usort($students, fn($a, $b) => $a["age"] <=> $b["age"]);
print_r($students);
?>

6) Interface

php
CopyEdit
<?php
interface Vehicle {
public function drive();
}
class Car implements Vehicle {
public function drive() {
echo "Driving a car";
}
}
$obj = new Car();
$obj->drive();
?>

7) Introspection

php
CopyEdit
<?php
class MyClass {
public function test() {}
}
$obj = new MyClass();
echo get_class($obj); // Outputs class name
?>

8) Inheritance

php
CopyEdit
<?php
class ParentClass {
public function greet() {
echo "Hello from parent";
}
}
class ChildClass extends ParentClass {
}
$obj = new ChildClass();
$obj->greet();
?>

9) Session

php
CopyEdit
<?php
session_start();
$_SESSION['user'] = 'Admin';
// To stop session
session_destroy();
?>

10) Cookies

php
CopyEdit
<?php
setcookie("user", "John", time() + 3600);
if (isset($_COOKIE['user'])) {
echo "Cookie is set: " . $_COOKIE['user'];
}
// To destroy
setcookie("user", "", time() - 3600);
?>

11) Create database

php
CopyEdit
<?php
$conn = mysqli_connect("localhost", "root", "");
$sql = "CREATE DATABASE myDB";
mysqli_query($conn, $sql);
?>

12) Select table

php
CopyEdit
<?php
$conn = mysqli_connect("localhost", "root", "", "myDB");
$result = mysqli_query($conn, "SELECT * FROM users");
while ($row = mysqli_fetch_assoc($result)) {
echo $row["name"] . "<br>";
}
?>

13) Insert data


php
CopyEdit
<?php
$conn = mysqli_connect("localhost", "root", "", "myDB");
mysqli_query($conn, "INSERT INTO users(name) VALUES('John')");
?>

14) Update data

php
CopyEdit
<?php
$conn = mysqli_connect("localhost", "root", "", "myDB");
mysqli_query($conn, "UPDATE users SET name='Doe' WHERE name='John'");
?>

15) Delete data

php
CopyEdit
<?php
$conn = mysqli_connect("localhost", "root", "", "myDB");
mysqli_query($conn, "DELETE FROM users WHERE name='Doe'");
?>

16) String functions

php
CopyEdit
<?php
echo strlen("Hello");
echo strtolower("HELLO");
echo strtoupper("hello");
echo strrev("PHP");
?>

17) Math functions

php
CopyEdit
<?php
echo abs(-5);
echo pow(2, 3);
echo sqrt(16);
echo round(3.6);
?>

18) Date functions

php
CopyEdit
<?php
echo date("Y-m-d");
echo date("l");
echo time();
echo date("h:i:sa");
?>
Theory Questions

1) Session vs Cookies

• Session is stored on the server, Cookies are stored in the browser.


• Sessions expire when the browser is closed, cookies can persist.

2) GET vs POST

• GET appends data to the URL, POST sends data in the body.
• GET is less secure, POST is more secure and supports more data.

3) for vs foreach

• for is used with index arrays or numbers.


• foreach is used specifically with arrays.

4) implode vs explode

• implode() joins array elements into a string.


• explode() splits a string into an array.

5) User-defined function

php
CopyEdit
function greet($name) {
return "Hello, $name";
}
echo greet("Alice");

6) Cloning

php
CopyEdit
<?php
class Test {
public $name = "Original";
}
$obj1 = new Test();
$obj2 = clone $obj1;
$obj2->name = "Clone";
?>

7) Serialization

php
CopyEdit
<?php
$arr = ["a" => 1, "b" => 2];
$serialized = serialize($arr);
echo $serialized;
?>

8) Validating user input

php
CopyEdit
<?php
$name = trim($_POST['name']);
if (!preg_match("/^[a-zA-Z ]*$/", $name)) {
echo "Only letters allowed";
}
?>

You might also like