0% found this document useful (0 votes)
4 views8 pages

PHP_Practical_Theory_Questions (2)

The document contains various PHP code snippets demonstrating fundamental programming concepts such as even numbers, prime numbers, reversing a number, working with arrays, object-oriented programming, sessions, cookies, and database operations. It also includes comparisons of different programming constructs like GET vs POST and session vs cookies. Additionally, it covers user-defined functions, cloning, serialization, and input validation.

Uploaded by

kallukaliya811
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

PHP_Practical_Theory_Questions (2)

The document contains various PHP code snippets demonstrating fundamental programming concepts such as even numbers, prime numbers, reversing a number, working with arrays, object-oriented programming, sessions, cookies, and database operations. It also includes comparisons of different programming constructs like GET vs POST and session vs cookies. Additionally, it covers user-defined functions, cloning, serialization, and input validation.

Uploaded by

kallukaliya811
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

**1.

1 Even Numbers and Sum**


```php
<?php
$sum = 0;
for ($i = 1; $i <= 10; $i++) {
if ($i % 2 == 0) {
echo $i . " ";
$sum += $i;
}
}
echo "\nSum of even numbers: " . $sum;
?>
```

**1.2 Prime Numbers up to N**


```php
<?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 . " ";
}
}
?>
```

**1.3 Reverse of a Number**


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

**1.4 Multidimensional Array**


```php
<?php
$students = array(
array("Name"=>"John", "Age"=>20, "Grade"=>"A"),
array("Name"=>"Alice", "Age"=>22, "Grade"=>"B"),
array("Name"=>"Bob", "Age"=>19, "Grade"=>"A")
);
foreach ($students as $student) {
foreach ($student as $key => $value) {
echo "$key: $value ";
}
echo "\n";
}
?>
```

**1.5 Sort Associative and Multidimensional Array**


```php
<?php
$marks = array("John"=>85, "Alice"=>95, "Bob"=>75);
asort($marks); // Sort by value
print_r($marks);

$students = array(
array("Name"=>"John", "Age"=>20),
array("Name"=>"Alice", "Age"=>22),
array("Name"=>"Bob", "Age"=>19)
);
usort($students, function($a, $b) {
return $a['Age'] <=> $b['Age'];
});
print_r($students);
?>
```

**1.6 Interface Example**


```php
<?php
interface Animal {
public function makeSound();
}

class Dog implements Animal {


public function makeSound() {
echo "Bark";
}
}
$dog = new Dog();
$dog->makeSound();
?>
```

**1.7 Introspection Example**


```php
<?php
class Test {
public $a = 1;
public function show() {
echo "Showing";
}
}

$obj = new Test();


print_r(get_class_methods($obj));
print_r(get_object_vars($obj));
?>
```

**1.8 Inheritance Example**


```php
<?php
class Vehicle {
public function move() {
echo "Moving";
}
}

class Car extends Vehicle {


public function honk() {
echo "Honking";
}
}

$obj = new Car();


$obj->move();
$obj->honk();
?>
```

**1.9 Session Start and Destroy**


```php
<?php
session_start();
$_SESSION["username"] = "JohnDoe";
echo $_SESSION["username"];
session_destroy();
?>
```

**1.10 Cookies (isset, name, destroy)**


```php
<?php
setcookie("user", "John", time() + 3600);
if(isset($_COOKIE["user"])) {
echo "User is " . $_COOKIE["user"];
}
setcookie("user", "", time() - 3600);
?>
```

**1.11 Create Database**


```php
<?php
$conn = new mysqli("localhost", "root", "");
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created";
}
$conn->close();
?>
```

**1.12 Select a Table**


```php
<?php
$conn = new mysqli("localhost", "root", "", "myDB");
$sql = "SELECT * FROM students";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo $row["name"];
}
$conn->close();
?>
```

**1.13 Insert into Table**


```php
<?php
$conn = new mysqli("localhost", "root", "", "myDB");
$sql = "INSERT INTO students (name, age) VALUES ('John', 25)";
$conn->query($sql);
$conn->close();
?>
```

**1.14 Update Table**


```php
<?php
$conn = new mysqli("localhost", "root", "", "myDB");
$sql = "UPDATE students SET age=26 WHERE name='John'";
$conn->query($sql);
$conn->close();
?>
```

**1.15 Delete from Table**


```php
<?php
$conn = new mysqli("localhost", "root", "", "myDB");
$sql = "DELETE FROM students WHERE name='John'";
$conn->query($sql);
$conn->close();
?>
```

**1.16 String Functions**


```php
<?php
echo strlen("Hello");
echo strrev("Hello");
echo strpos("Hello world", "world");
echo str_replace("world", "PHP", "Hello world");
?>
```

**1.17 Math Functions**


```php
<?php
echo abs(-5);
echo pow(2,3);
echo sqrt(16);
echo round(3.6);
?>
```
**1.18 Date Functions**
```php
<?php
echo date("Y-m-d");
echo date("l");
echo time();
echo date("H:i:s");
?>
```

**2.1 Session vs Cookies**


Session stores data on server, cookies store data in user's browser.
Sessions are more secure; cookies are limited in size and expire faster.

**2.2 GET vs POST**


GET appends data in URL, POST sends data invisibly.
POST is safer and supports larger data.

**2.3 For vs Foreach**


For is used with indexed arrays or numeric conditions.
Foreach is easier for looping through arrays.

**2.4 Implode vs Explode**


Implode converts array to string.
Explode splits string into array based on delimiter.

**2.5 User-defined Function**


```php
<?php
function greet($name) {
return "Hello " . $name;
}
echo greet("John");
?>
```

**2.6 Cloning**
```php
<?php
class Test {
public $a = 1;
}
$obj1 = new Test();
$obj2 = clone $obj1;
?>
```

**2.7 Serialization**
```php
<?php
$data = array("a"=>1, "b"=>2);
$str = serialize($data);
echo $str;
?>
```

**2.8 Validate User Inputs**


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

You might also like