0% found this document useful (0 votes)
13 views9 pages

4 Mark PHP

The document provides explanations and examples of various PHP concepts including multidimensional arrays, leap year checking, interfaces, string functions, variable scopes, loops, cookies, and MySQL queries. It includes code snippets demonstrating how to implement these concepts in PHP. Additionally, it outlines the differences between various loop types and array types in PHP.

Uploaded by

sushanttandale6
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)
13 views9 pages

4 Mark PHP

The document provides explanations and examples of various PHP concepts including multidimensional arrays, leap year checking, interfaces, string functions, variable scopes, loops, cookies, and MySQL queries. It includes code snippets demonstrating how to implement these concepts in PHP. Additionally, it outlines the differences between various loop types and array types in PHP.

Uploaded by

sushanttandale6
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/ 9

a. Explain multidimensional array in PHP with example.

Ans. A multidimensional array in PHP is an array that contains


one or more arrays as its elements. It allows storing data in a
matrix-like structure.
Example:
<?php
$students = array(
array("John", 25, "A"),
array("Alice", 22, "B"),
array("Bob", 24, "A")
);
echo "Name: " . $students[0][0] . ", Age: " . $students[0][1] . ",
Grade: " . $students[0][2] . "<br>";
?>
b) Write a PHP Program to check whether given year is leap
year or not
(use if else)
Ans. <?php
$year = 2024; // Change the year as needed
if (($year % 4 == 0 && $year % 100 != 0) || ($year % 400 == 0))
{
echo "$year is a Leap Year.";
} else {
echo "$year is not a Leap Year.";
}
?>
c) Write a PHP script to define an interface which has methods
area () volume (). Define constant PI. Create a class cylinder
which implements
this interface and calculate area and volume
Ans. <?php
interface Shape {
const PI = 3.14159;
public function area();
public function volume();
}
class Cylinder implements Shape {
private $radius;
private $height;
public function __construct($radius, $height) {
$this->radius = $radius;
$this->height = $height;
}
public function area() {
return 2 * self::PI * $this->radius * ($this->radius + $this-
>height);
}
public function volume() {
return self::PI * pow($this->radius, 2) * $this->height;
}
}
$cyl = new Cylinder(5, 10);
echo "Area: " . $cyl->area() . "<br>";
echo "Volume: " . $cyl->volume();
?>
d) What are the built in functions of string?
Ans. Some common string functions in PHP:
strlen($string): Returns string length
strrev($string): Reverses a string
strpos($string, "text"): Finds the position of a substring
str_replace("old", "new", $string): Replaces text
substr($string, start, length): Extracts part of a string
e) Write a PHP program to reverse an array
Ans. <?php
$array = array(1, 2, 3, 4, 5);
$reversedArray = array_reverse($array);
print_r($reversedArray);
?>
a) What is variable in PHP? Explain its scope with example.
Ans. A variable in PHP starts with $ and stores data.
Scope types:
Local: Defined inside a function
Global: Defined outside a function
Static: Retains value between function calls
Superglobal: Available everywhere ($_GET, $_POST)
Example:
<?php
$x = 10; // Global Variable
function test() {
global $x;
echo "Value of x: $x";
}
test();
?>
b) What is the difference between for and for each in PHP?
Ans. for Loop
• Used when the number of iterations is known
beforehand.
• Requires an initialization, condition, and
increment/decrement statement.
• Suitable for iterating over numeric sequences or when
working with indexes.
Syntax:
for ($i = 1; $i <= 5; $i++) {
echo "Iteration $i <br>";
}

foreach Loop
• Specifically designed for iterating over arrays and objects.
• Does not require an explicit counter.
• Automatically traverses through each element in an array.
• $colors = ["Red", "Green", "Blue"];
• foreach ($colors as $color) {
• echo "$color <br>";
• }
c) Write a PHP Program to display reverse of a string.
Ans. <?php
$str = "Hello";
echo strrev($str);
?>
d) How to create cookies? Give an example.
Ans. <?php
setcookie("user", "John", time() + 3600, "/");
echo "Cookie set successfully!";
?>

e) Explain passing values by reference with an example.


Ans. <?php
function increment(&$num) {
$num++;
}
$value = 5;
increment($value);
echo $value; // Output: 6
?>
a) What is array? Explain different types of array in PHP.
Ans. Indexed Array: $arr = array(1, 2, 3);
Associative Array: $arr = array("name" => "John", "age" =>
30);
Multidimensional Array: As explained eearlieA

b) What is the difference between a while loop and do while


loop in PHP.
Ans.
While loop Do while loop

The condition is checked The loop body is executed


before the execution of the at least once, regardless of
loop body. the condition.
If the condition is false The condition is checked
initially, the loop body after the execution of the
does not execute even loop body.
once.
Syntax: Syntax:

$i = 1; $i = 1;
while ($i <= 5) { do {
echo "Iteration $i <br>"; echo "Iteration $i <br>";
$i++; $i++;
} } while ($i <= 5);

c) Write a PHP program to find the sum of digit of a given


number.
Ans. <?php
$num = 1234;
$sum = 0;
while ($num > 0) {
$sum += $num % 10;
$num = (int)($num / 10);
}
echo "Sum of digits: " . $sum;
?>
d) Write a PHP program to use multiple checkbox to select
hobbies
Ans. <form method="post">
<input type="checkbox" name="hobbies[]"
value="Reading"> Reading
<input type="checkbox" name="hobbies[]"
value="Traveling"> Traveling
<input type="checkbox" name="hobbies[]"
value="Gaming"> Gaming
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (isset($_POST['submit'])) {
if (!empty($_POST['hobbies'])) {
echo "Selected hobbies: " . implode(", ",
$_POST['hobbies']);
} else {
echo "No hobby selected.";
}
}
?>
e) List various MYSQL Queries with their Syntax.
Ans. Create Database: CREATE DATABASE dbname;
Create Table:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
age INT
);
Insert Data: INSERT INTO users (name, age) VALUES ('John',
25);
Select Data: SELECT * FROM users;
Update Data: UPDATE users SET age = 26 WHERE name =
'John';
Delete Data: DELETE FROM users WHERE name = 'John';

You might also like