Web Framework and Services — Study Notes

Unit 01

Core PHP Programming

5 Questions 5 Marks Each Web Framework and Services
01
5 Marks Question

Explain PHP Data Types and Operators

PHP Data Types

A data type specifies the type of value stored in a variable. PHP is loosely typed, so we do not need to explicitly declare the data type of a variable; PHP determines it automatically.

PHP supports 8 main data types.

Data Type Meaning Example
IntegerWhole numbers$a = 10;
Float (Double)Numbers with decimal points$a = 10.5;
StringSequence of characters$name = "PHP";
BooleanRepresents TRUE or FALSE$x = true;
ArrayStores multiple values$a = array(10,20,30);
ObjectInstance of a class$obj = new Student();
NULLRepresents a variable with no value$a = NULL;
ResourceReference to an external resource, such as a file or database connection$file = fopen(...);

For example, integers represent whole numbers, while floating-point numbers represent real or decimal numbers.

PHP Operators

Operators are symbols used to perform operations on variables and values. The important types of PHP operators are:

01
Arithmetic

Symbols: +, -, *, /, %

Used for mathematical calculations.

02
Assignment

Symbols: =, +=, -=, *=, /=, %=

Used to assign or modify values.

03
Comparison

Symbols: ==, ===, !=, >, <, >=, <=

Compare two values and return TRUE or FALSE.

04
Increment / Decrement

Symbols: ++$a, $a++, --$a, $a--

Increase or decrease a variable by 1.

05
String

Symbols: .

Joins / concatenates strings, for example: "Hello " . "PHP" → Hello PHP.

06
Logical

Symbols: &&, ||, !

Used to combine or reverse conditions.

In short Data types define what kind of data is stored, while operators define what operations are performed on that data.
02
5 Marks Question

Explain Arrays in PHP

An array in PHP is used to store multiple values in a single variable. PHP supports different types of arrays and provides built-in functions for sorting and managing array data.

Types of Arrays in PHP

There are three main types of arrays:

Indexed Array
Stores values using numeric indexes starting from 0.
<?php
$names = array("Fabio", "Klevi", "John");

echo $names[0];
?>
Output: Fabio
Associative Array
Uses named keys instead of numeric indexes.
<?php
$namesAge = array(
    "Fabio" => "20",
    "Klevi" => "16",
    "John" => "43"
);

echo $namesAge["Fabio"];
?>
Output: 20
Multidimensional Array
An array whose elements are themselves arrays.
<?php
$socialNetworks = array(
    array("Facebook", "Feb", 21),
    array("Twitter", "Dec", 2),
    array("Instagram", "Aug", 15)
);
?>

Common Array Functions

PHP provides several built-in functions for working with arrays.

Function Purpose
sort()Sorts values in ascending order and creates new numeric indexes.
asort()Sorts by values while preserving keys.
ksort()Sorts an associative array by keys.
array_merge()Combines two or more arrays into one array.
In short PHP arrays allow multiple values to be stored in one variable and are mainly classified as Indexed, Associative, and Multidimensional arrays.
03
5 Marks Question

Explain PHP Tags

PHP tags are used to indicate where PHP code starts and ends in a file. The opening tag tells the server that the code following it is PHP code.

PHP provides the following types of tags:

Standard Tag
The most commonly used and recommended PHP tag. It is universally supported.
<?php
echo "Hello";
?>
Short Echo Tag
Used to display output quickly. It is equivalent to a standard echo block.
<?= "Hello"; ?>
Short Tag
A shorter form of the PHP tag. It is not recommended because short tags may be disabled on some servers.
<?
echo "Hello";
?>
Script Tag
An old method of writing PHP code and rarely used now.
<script language="php">
echo "Hello";
</script>
Conclusion: The standard <?php ... ?> tag is the recommended method because it is universally supported.
04
5 Marks Question

Explain the Filter Function in PHP

PHP provides the Filter Extension to validate and sanitize data. It helps developers handle user input safely without writing complex regular expressions.

Filtering is mainly used for two purposes: validation checks whether data is in the required format, and sanitization removes or converts unwanted or unsafe characters from data.

1. filter_var() Function

The filter_var() function is used to filter a specific variable.

Syntax: filter_var($variable, $filter, $options);

<?php
$email = "abc@gmail.com";

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "Valid Email";
} else {
    echo "Invalid Email";
}
?>

Here, FILTER_VALIDATE_EMAIL checks whether the given value has a valid email format.

2. filter_input() Function

The filter_input() function directly gets data from superglobals such as $_GET and $_POST and filters it.

Syntax: filter_input($type, $variable_name, $filter);

For example: $age = filter_input(INPUT_POST, "age", FILTER_VALIDATE_INT);

Common Validation Filters

FilterPurpose
FILTER_VALIDATE_EMAILChecks for a valid email
FILTER_VALIDATE_INTChecks for a valid integer
FILTER_VALIDATE_URLChecks for a valid URL

Common Sanitization Filters

FilterPurpose
FILTER_SANITIZE_EMAILRemoves invalid characters from an email
FILTER_SANITIZE_NUMBER_INTKeeps only digits, + and -
FILTER_SANITIZE_SPECIAL_CHARSEncodes special HTML characters
Conclusion PHP filter functions are useful for validating and sanitizing user input, which makes input safer and ensures that the application receives data in the expected format.
05
5 Marks Question

Explain Validation in PHP

Validation in PHP is the process of checking whether user input meets the expected format, type, or rules before it is processed or stored. It helps maintain data accuracy, integrity, and application security.

For example, validation can check whether an email address is properly formatted, whether a name has the correct length, whether a number is within a specified range, or whether a required field is empty.

Types of Input Validation

Format Validation
Checks whether data follows the required format or pattern.
Length Validation
Checks the minimum, maximum, or exact length of input.
Range Validation
Checks whether a value falls within an acceptable range.
Type Validation
Checks whether the input is of the expected data type.
Content Validation
Examines input for malicious or inappropriate content.

Validation Using PHP Filters

PHP provides built-in validation filters such as FILTER_VALIDATE_EMAIL, FILTER_VALIDATE_INT, and FILTER_VALIDATE_URL.

<?php
$email = "abc@gmail.com";

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "Valid Email";
} else {
    echo "Invalid Email";
}
?>
Importance of Validation: Validation protects applications from security problems such as SQL injection and XSS, prevents incorrect data from entering the database, and helps enforce application rules.
Conclusion Validation ensures that user-provided data is correct, relevant, and safe before it is processed or stored.