Unit 6
PHP
Very Short Answer
1. What is variable?
A variable in PHP is a container used to store data. It
starts with a dollar sign $ and can store strings,
numbers, or arrays.
2. Define constants in PHP?
Constants are like variables, but once defined, their
value cannot change. In PHP, they are defined using
define() function and do not begin with a $.
3. Discuss unary and binary operators?
Unary operators act on a single operand (e.g., ++$x),
while binary operators require two operands (e.g., $a +
$b). They are used in arithmetic and logic.
4. Discuss break and continue statements?
break exits a loop immediately, while continue skips
current loop iteration and moves to the next. Both are
used for flow control in loops.
5. What is an array?
An array is a data structure that stores multiple values
in one variable. It can be indexed, associative, or
multidimensional in PHP.
Short Answer
1. Explain scope of variables?
Scope defines where a variable is accessible in PHP.
There are three types: global (used outside functions),
local (inside functions), and static (retains value after
function ends). Scope affects variable behavior and
visibility.
2. Explain conditional assignment operators?
Conditional assignment operators assign values based
on a condition. The ternary operator ? : is commonly
used, like $x = ($a > $b) ? $a : $b;. It’s a compact
if-else
form.
3. What is difference between while and do while
loop?
In while, the condition is checked first; loop may not
run if false. In do-while, the loop runs at least once
since condition is checked after execution.
4. Explain foreach loop?
foreach loop is used to iterate over arrays. It
automatically assigns each array value to a variable.
Syntax: foreach($array as $value) { //code }. It
simplifies array handling.
5. What is an index array?
Indexed arrays store elements with numeric indexes
starting from 0. Example: $arr = array("apple",
"banana");. Elements are accessed using their
numeric
positions in the array.
6. What is associative array?
Associative arrays use named keys instead of numeric
indexes. Example: $arr = array("name" =>
"John", "age"
=> 25);. Keys act as identifiers for values.
Long Answer
1. Explain different data types in PHP?
PHP supports several data types:
1. Integer – Whole numbers like 1, -100.
2. Float (Double) – Decimal numbers like 3.14.
3. String – Sequence of characters like "Hello".
4.Boolean – True or False values.
5.Array – A collection of values.
6.Object – Instances of classes.
7.NULL – Represents a variable with no value.
8.Resource – Special variables referencing external
resources (e.g., file handles, database
connections).
Each data type helps PHP handle various operations
efficiently. Strings can be manipulated using functions,
integers and floats are used in arithmetic, and arrays
can store multiple related values. Type juggling in PHP
allows automatic type conversion when needed,
making it flexible but requiring careful handling in
complex logic.
2. Describe various types of operators in PHP?
Operators in PHP include:
1.Arithmetic Operators – +, -,
*
, /, % for math.
2.Assignment Operators – =
, +=, -=, etc.
3. Comparison Operators – ==
,
===
, !=, <, >.
4. Logical Operators – &&, ||, ! for logical
conditions.
5. Increment/Decrement Operators – ++, --.
6. String Operators – . for concatenation.
7.Array Operators – +,
==
,
=== for array comparison.
8. Conditional (Ternary) Operator – ?: for shorthand
if-else.
Operators make coding and logic processing easier.
3. Explain switch statement with an example?
A switch statement evaluates a variable and executes
matching case block. It prevents long if-else chains.
Example:
$day = "Monday";
switch ($day) {
case "Monday":
echo "Start of the week.";
break;
case "Friday":
echo "Weekend is near.";
break;
default:
echo "Midweek day.";
}
The break stops execution after a match. If no case
matches, default runs.
4. Explain different types of loop in PHP?
PHP has four types of loops:
1.while loop – Executes while a condition is true.
2. do-while loop – Executes at least once, then
checks condition.
3.for loop – Best for loops with counters.
4.foreach loop – Ideal for iterating arrays.
Each loop helps reduce code repetition and simplifies
automation. Choosing the right loop depends on the
problem, like foreach for arrays and for for numeric
iterations.
5. Discuss various array functions in detail?
PHP provides many built-in array functions:
• array_push() – Adds elements to the end.
• array_pop() – Removes the last element.
• array_merge() – Merges two arrays.
• array_keys() – Returns all keys.
• array_values() – Returns all values.
• in_array() – Checks if a value exists.
• count() – Counts array elements.
• sort() – Sorts in ascending order.
• rsort() – Sorts in descending order.
• array_slice() – Extracts a portion.
These functions simplify array manipulation, searching,
and sorting. They are essential for data handling and
display in dynamic web applications.
0 Comments