0% found this document useful (0 votes)
4 views

Unit Test 1 PHP

This document is a unit test on PHP covering various topics such as features of PHP, comments, variable types, control structures, data types, PHP code tags, arrays, and operators. It includes definitions, examples, and explanations of each topic. The test is structured in a question-and-answer format, created by Mayur Gaikwad.

Uploaded by

Mayur Gaikwad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unit Test 1 PHP

This document is a unit test on PHP covering various topics such as features of PHP, comments, variable types, control structures, data types, PHP code tags, arrays, and operators. It includes definitions, examples, and explanations of each topic. The test is structured in a question-and-answer format, created by Mayur Gaikwad.

Uploaded by

Mayur Gaikwad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

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 #)

 Used for short explanations or disabling a single line of code.


 Written using // or #.

2. Multi-Line Comments (/* ... */)

 Used for longer explanations or disabling multiple lines of code.


 Written between /* and */.

Created by Mayur Gaikwad


Q3) Explain types of variable in PHP (Local, Global & Static)
Ans :

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.

Created by Mayur Gaikwad


3. Static Variable

 Retains its value even after the function has finished execution.
 Declared using the static keyword inside a function.

Q4) Explain Control Structures in PHP with example


Ans :
Control Structures in PHP

Control structures in PHP manage the flow of execution. They are categorized into Conditional
Statements and Looping Statements.

Created by Mayur Gaikwad


1. Conditional Statements
a) if Statement
 Executes a block of code if the condition is true.

 Used for simple decision-making.

b) if-else Statement
 Executes one block if true, otherwise executes another block.
 Used for two possible outcomes.

c) if-elseif-else Statement

 Checks multiple conditions one by one.


 Used when more than two outcomes are possible

Created by Mayur Gaikwad


d) switch Statement
 Used to compare a variable with multiple values.

 Avoids multiple if-elseif conditions

2. Looping Statements
a) while Loop
 Runs the loop as long as the condition is true.

 Used when the number of iterations is unknown

Created by Mayur Gaikwad


b) do-while Loop
 Executes at least once before checking the condition.
 Used when execution must happen once, even if false

c) for Loop
 Used when the number of iterations is known.
 Combines initialization, condition, and increment in one line.

Created by Mayur Gaikwad


d) foreach Loop
 Used to iterate through each element of an array.
 Best suited for working with arrays.

5) Explain Data types in PHP.


Ans :

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

Created by Mayur Gaikwad


3. Float (Double)
 A number with a decimal point or in exponential form.
 Used for precise calculations

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.

Created by Mayur Gaikwad


6. Object
 Stores an instance of a class.
 Used in object-oriented programming (OOP)

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.

Created by Mayur Gaikwad


Q6) Which tag is used to define PHP Code with example
Ans :
PHP code is written inside special tags. The Standard PHP Tag is the most commonly used, but
there are other types as well.
1. Standard PHP Tag (Recommended)

 Most widely used and supported in all PHP versions.


 Always preferred for writing PHP code.

2. Short PHP Tag (Requires Configuration)


 Uses <? instead of <?php.

 Requires short_open_tag enabled in php.ini (not recommended).

3. ASP Style Tag (Deprecated – Not Recommended)


 Uses <% and %>, similar to ASP.

 Removed from newer PHP versions.

Created by Mayur Gaikwad


4. Script Tag (Rarely Used)
 Uses <script language="php"> like JavaScript.
 Not commonly used in modern PHP.

Q7) Explain array and its types in php


Ans :

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.

 Access elements using their index number.


Example:

Created by Mayur Gaikwad


2. Associative Array
 Uses named (key-value) pairs instead of numeric indexes.
 Useful for mapping meaningful keys to values.

 Access elements using their keys.


Example:

3. Multidimensional Array
 Contains one or more arrays inside an array.
 Used for representing tabular or complex data.
 Access elements using multiple indexes.
Example:

Q8) Explain operators in php ?


Ans:
Operators in PHP

 Symbols used for calculations, comparisons, and logic.


 Types include Arithmetic, Assignment, Comparison, Logical, etc.
 Help in data manipulation and decision-making.

Created by Mayur Gaikwad


Types of Operators in PHP
1. Arithmetic Operators
 Used for mathematical calculations.

 Example operators: + (Addition), - (Subtraction), * (Multiplication), / (Division), %


(Modulus).

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:

Created by Mayur Gaikwad


4. Logical Operators
 Used to combine multiple conditions.
 Example operators: && (AND), || (OR), ! (NOT).

Example:

5. Bitwise Operators

 Perform operations at the binary level.


 Example operators: & (AND), | (OR), ^ (XOR), << (Left Shift), >> (Right Shift).
Example:

6. String Operators
 Used for string concatenation.
 Example operators: . (Concatenation), .= (Concatenation Assignment).
Example:

Created by Mayur Gaikwad


7. Ternary Operator
 A shorthand for if-else conditions.
 Syntax: condition ? value_if_true : value_if_false;

Example:

8. Null Coalescing Operator (??)


 Returns the first non-null value.

 Useful for handling undefined variables.


Example:

Created by Mayur Gaikwad

You might also like