Q2 and Notes PHP
Q2 and Notes PHP
Constant Variable:
A constant is a name or A variable is a name for
an identifier for a simple a storage location that
value. holds data that can be
changed during script
execution.
The value cannot be Variables are declared
changed during the script using the dollar sign ($)
execution. followed by the variable
name.
Constants are defined Variables can have their
using the define() values changed at any
function or the const time.
keyword.
Constants do not require Variables are local to the
a dollar sign ($) before scope in which they are
their name. defined, unless declared
global.
GET Method: Data is sent appended to the URL as query strings. It is visible to
everyone and has length limitations.
POST Method: Data is sent in the request body. It is not visible in the URL and has
no size limitations.
e)what is a session in PHP? Explain it.
A session in PHP is a way to store information (in variables) to be used across multiple pages. Unlike a
cookie, the information is not stored on the user's computer.
Starting a Session:
php
session_start();
php
$_SESSION["username"] = "John";
php
echo $_SESSION["username"];
Ending a Session:
php
session_unset(); // remove all session variables
session_destroy(); // destroy the session
POST:
Explanation
1. Condition: This is an expression that evaluates to either true or false. It can be any
expression that PHP can evaluate to a boolean value.
2. Code Block for if: If the condition evaluates to true, the code inside the first block
(following if) will be executed.
3. Code Block for else: If the condition evaluates to false, the code inside the else block
will be executed instead.
They are used to store data about the user for tracking and personalization
purposes.
a) Write a PHP Program to check whether given year is leap year or not (use ifelse)
<?php
function isLeapYear($year) {
if (($year % 4 == 0 && $year % 100 != 0) || ($year % 400 == 0)) {
return true;
} else {
return false;
}
}
$year = 2024;
if (isLeapYear($year)) {
echo "$year is a leap year.";
} else {
echo "$year is not a leap year.";
}
?>
b) Write a PHP script to define an interface which has methods area () volume ().
<?php
interface Shape {
?>
c) constant PI. Create a class cylinder which implements this interface and
calculate area and volume
<?php
interface Shape {
public function area();
public function volume();
}
class Cylinder implements Shape {
const PI = 3.14159;
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;
}
}
$cylinder = new Cylinder(3, 5);
echo "Area of the cylinder: " . $cylinder->area() . "\n";
echo "Volume of the cylinder: " . $cylinder->volume() . "\n";
?>
d) What are the built in functions of string?
PHP provides a wide range of built-in functions for string manipulation. Some of the
commonly used string functions are:
function reverseArray($array) {
$reversedArray = array();
$reversedArray[] = $array[$i];
return $reversedArray;
$reversedArray = reverseArray($array);
print_r($array);
print_r($reversedArray);
?>
Ease of Use: PHP is relatively easy to learn and has a simple and straightforward
syntax.
Open Source: PHP is open-source, meaning it's free to use and has a large
community of developers who contribute to its continuous improvement.
<?php
function sumOfDigits($number) {
$sum = 0;
return $sum;
$number = 12345;
?>
The for loop is used when you know in advance how many times you want to execute a
statement or a block of statements. It is commonly used for iterating over a sequence
(like an array).
Example:
<?php
}
?>
The foreach loop is used to iterate over arrays. It provides an easy way to access
array elements without needing an index counter.
Example:
<?php
?>
Cookies are small pieces of data that a server sends to the user's web browser. The
browser may store these cookies and send them back to the same server with
subsequent requests. Cookies are often used to store user preferences, session
information, or track user behavior.
e) explain any two built-in array functions in php.
The array_merge() function merges one or more arrays into one array.
Example:
array_merge(array1, array2, ...);
The array_push() function inserts one or more elements at the end of an array.
Example:
<?php
print_r($stack);
?>
e) Write a php program to check whether Entertainment age from user is allowed for
vote or not.
<?php
// Function to check voting eligibility
function checkVotingEligibility($age) {
if ($age >= 18) {
return "You are allowed to vote.";
} else {
return "You are not allowed to vote.";
}
}