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 |
|---|---|---|
| Integer | Whole numbers | $a = 10; |
| Float (Double) | Numbers with decimal points | $a = 10.5; |
| String | Sequence of characters | $name = "PHP"; |
| Boolean | Represents TRUE or FALSE | $x = true; |
| Array | Stores multiple values | $a = array(10,20,30); |
| Object | Instance of a class | $obj = new Student(); |
| NULL | Represents a variable with no value | $a = NULL; |
| Resource | Reference 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:
Symbols: +, -, *, /, %
Used for mathematical calculations.
Symbols: =, +=, -=, *=, /=, %=
Used to assign or modify values.
Symbols: ==, ===, !=, >, <, >=, <=
Compare two values and return TRUE or FALSE.
Symbols: ++$a, $a++, --$a, $a--
Increase or decrease a variable by 1.
Symbols: .
Joins / concatenates strings, for example: "Hello " . "PHP" → Hello PHP.
Symbols: &&, ||, !
Used to combine or reverse conditions.
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:
<?php
$names = array("Fabio", "Klevi", "John");
echo $names[0];
?>
Output: Fabio
<?php
$namesAge = array(
"Fabio" => "20",
"Klevi" => "16",
"John" => "43"
);
echo $namesAge["Fabio"];
?>
Output: 20
<?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. |
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:
<?php
echo "Hello";
?>
<?= "Hello"; ?>
<?
echo "Hello";
?>
<script language="php">
echo "Hello";
</script>
<?php ... ?> tag is the recommended method because it is universally supported.
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
| Filter | Purpose |
|---|---|
| FILTER_VALIDATE_EMAIL | Checks for a valid email |
| FILTER_VALIDATE_INT | Checks for a valid integer |
| FILTER_VALIDATE_URL | Checks for a valid URL |
Common Sanitization Filters
| Filter | Purpose |
|---|---|
| FILTER_SANITIZE_EMAIL | Removes invalid characters from an email |
| FILTER_SANITIZE_NUMBER_INT | Keeps only digits, + and - |
| FILTER_SANITIZE_SPECIAL_CHARS | Encodes special HTML characters |
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
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";
}
?>