Unit Test 1 PHP
Unit Test 1 PHP
2 MARKS
Q1) Write a features of PHP
Ans:
i. Open Source & Free – PHP is free to use.
ii. Server-Side Scripting – Runs on the server to create dynamic web pages.
iii. Platform Independent – Works on Windows, Linux, and macOS.
iv. Database Support – Compatible with MySQL, PostgreSQL, and more.
v. Easy to Learn – Simple syntax, similar to C and Java.
vi. Embedded in HTML – Can be mixed with HTML code.
vii. Fast Execution – Faster than many other scripting languages.
viii. Built-in Functions – Many ready-to-use functions
Q2) Explain Comments in PHP with example
Ans :
1. Single-Line Comments (// or #)
1. Local Variable
Declared inside a function and can only be used within that function.
Cannot be accessed outside the function.
2. Global Variable
Declared outside any function and can be accessed anywhere in the script.
To use inside a function, the global keyword is required.
Retains its value even after the function has finished execution.
Declared using the static keyword inside a function.
Control structures in PHP manage the flow of execution. They are categorized into Conditional
Statements and Looping Statements.
b) if-else Statement
Executes one block if true, otherwise executes another block.
Used for two possible outcomes.
c) if-elseif-else Statement
2. Looping Statements
a) while Loop
Runs the loop as long as the condition is true.
c) for Loop
Used when the number of iterations is known.
Combines initialization, condition, and increment in one line.
1. String
A sequence of characters enclosed in single (' ') or double (" ") quotes.
Used to store text
2. Integer
A whole number (positive or negative) without decimals.
Can be stored in decimal, hexadecimal, or octal format
4. Boolean
Stores only true or false.
Used in conditional statements
5. Array
Stores multiple values in a single variable.
Can be indexed or associative.
7. NULL
Represents a variable with no value.
Used to clear a variable
8. Resource
Holds a reference to external resources (e.g., database connections).
Used in advanced applications.
Arrays in PHP
An array in PHP is a data structure that can store multiple values in a single variable. PHP
supports three types of arrays:
Types of Arrays in PHP
1. Indexed Array
Stores values with numeric indexes (starting from 0).
Used for storing multiple values in a single variable.
3. Multidimensional Array
Contains one or more arrays inside an array.
Used for representing tabular or complex data.
Access elements using multiple indexes.
Example:
Example:
2. Assignment Operators
Used to assign values to variables.
Example operators: = (Assign), += (Add & Assign), -= (Subtract & Assign), *= (Multiply &
Assign).
Example:
3. Comparison Operators
Used to compare two values.
Example operators: == (Equal), != (Not Equal), > (Greater), < (Lesser), >= (Greater or
Equal), <= (Lesser or Equal).
Example:
Example:
5. Bitwise Operators
6. String Operators
Used for string concatenation.
Example operators: . (Concatenation), .= (Concatenation Assignment).
Example:
Example: