PHP Answers
PHP Answers
Page 1 of 11
4. Write syntax of PHP. 2 marks (*1)
ANS
A PHP script starts with the tag <?php and end with the tag ?>.
The PHP delimiter in the following example simply tells the PHP engine to treat the enclosed code block
as PHP code, rather than simple HTML
Syntax:
<?php
echo ‘Hello World’;
?>
1) Boolean: A boolean value represents either true(1) or false(0). Boolean values are often used in
conditional testing.
Example: $a = true;
3) String: A string is a sequence of characters. A string are declares using single quotes or double quotes.
Example: $s1 = “Hello PHP”;
4) Integer: Holds whole numbers, both positive and negative, without decimals.
Example: $x = 100;
Page 2 of 11
6. State any 2 difference between for and for each. 2 marks (*1)
Write syntax of for each loop. 2 marks (*1)
Write a program using Foreach loop. 2 marks (*1)
Explain use of for and for each with example. 4 marks (*1)
List loop control structures. Explain any one loop control structure. 4 marks (*1)
Explain different loops in PHP. 4 marks (*1)
Write a program using do-while loop. 4 marks (*1)
Explain different loops in PHP with example . 6 marks (*1)
Write a PHP program to display numbers from 1-10 in a sequence using for loop.6marks (*1)
ANS
Type of Loops in PHP:
1) while loop
2) do while loop
3) for loop
4) foreach loop
for loop foreach loop while loop do while loop
The for loop is used foreach loop is a loop It is an entry controlled It is an exit controlled
when we know in structure used for arrays. loop. Condition is given loop. Condition is given
advance how many at the beginning of loop. at the end of the loop.
times the script should Loop statements never Loop statements
run. executes if condition executes once even if
does not satisfy. condition does not
satisfy.
Syntax: Syntax: Syntax: Syntax:
for(initialization; foreach($array as While(condition) do
condition; $value) { {
increment/decrement) { Statements; Statements;
{ Statements; } }
Statements; } while(condition);
}
Example: Example: Example: Example:
<?php <?php <?php <?php
for ($i = 1; $i <= 10; $fruits = array("Apple", $i=1; $i=1;
$i++) "Banana", "Mango", while($i<=10) do
{ "Orange"); { {
echo "$i<br>"; echo "$i<br/>"; echo "$i<br/>";
} foreach ($fruits as $i++; $i++;
?> $fruit) } }
Output: { ?> while($i<=10);
1 echo "Fruit: $fruit<br>"; Output: ?>
2 } 1 Output:
3 ?> 2 1
4 Output: 3 2
5 Fruit: Apple 4 3
6 Fruit: Banana 5 4
7 Fruit: Mango 6 5
8 Fruit: Orange 7 6
9 8 7
10 9 8
10 9
10
Page 3 of 11
7. Describe the syntax of if-else control statement with example in PHP. 4 marks (*1)
Explain the use of break and continue statements. 4 marks (*2)
Explain decision making statements along with their syntax in PHP. 6 marks (*1)
ANS
Decision making statements are:
1) if statement: Executes a block of code only if the condition is true.
Syntax:
if (condition)
{
// code to execute if condition is true
}
Example:
<?php
$a = 5;
if ($a > 0)
{
echo "Positive";
}
?>
Output:
Positive
2) if else statement: Executes one block of code if condition is true, otherwise executes another block.
Syntax:
if (condition)
{
// code to execute if condition is true
}
else
{
// code to execute if condition is false
}
Example:
<?php
$marks = 40;
if ($marks >= 50)
{
echo "You passed!";
}
else
{
echo "You failed.";
}
?>
Output:
You failed.
Page 4 of 11
3) else if statement: Checks multiple conditions in sequence. Executes the first true condition’s block.
Syntax:
if (condition1)
{
// code to execute if condition1 is true
}
elseif (condition2)
{
// code to execute if condition2 is true
}
else
{
// code to execute if all conditions are false
}
Example:
<?php
$num1 = 20;
$num2 = 45;
4) switch statement: Compares one expression against multiple values. Executes the matched case.
Syntax:
switch (expression)
{
case value1:
// code to executed if case1 match
break;
case value2:
// code to executed if case2 match
break;
default:
// code to execute if no match
}
Page 5 of 11
Example:
<?php
$color = "blue";
switch ($color)
{
case "red":
echo "Color is red";
break;
case "blue":
echo "Color is blue";
break;
default:
echo "Color is unknown";
}
?>
Output:
Color is blue
5) break statement: Exits from loop or switch case immediately when encountered. Used inside for,
foreach, while, do-while, or switch.
Syntax:
break;
Example:
<?php
for ($i = 0; $i < 10; $i++)
{
if ($i == 5)
{
break;
}
echo "Number: $i<br>";
}
?>
Output:
Number: 0
Number: 1
Number: 2
Number: 3
Number: 4
Page 6 of 11
6) continue statement: Skips current iteration of loop and jumps to the next iteration. Used inside any
loop.
Syntax:
continue;
Example:
<?php
for ($i = 1; $i <= 5; $i++)
{
if ($i == 3)
{
continue;
}
echo "Value: $i<br>";
}
?>
Output:
Value: 1
Value: 2
Value: 4
Value: 5
Output:
Largest number is: 45
Page 7 of 11
9. Explain bitwise operators in PHP. 4 marks (*1)
ANS
Operator Name Description
& Bitwise AND Returns 1 if both corresponding bits are 1, otherwise returns 0.
| Bitwise OR Returns 1 if at least one of the corresponding bits is 1.
^ Bitwise XOR Returns 1 if the corresponding bits are different, otherwise 0.
~ Bitwise NOT Flips all the bits. Converts 1 to 0 and 0 to 1. Also returns negative value due to
2’s complement.
<< Left Shift Shifts all bits to the left, effectively multiplying by 2 for each shift.
>> Right Shift Shifts all bits to the right, effectively dividing by 2 for each shift.
Example:
<?php
$first = 5; // Binary: 0101
$second = 3; // Binary: 0011
// Bitwise AND
$and = $first & $second;
echo "Bitwise & of 5 and 3 is $and<br>";
// Bitwise OR
$or = $first | $second;
echo "Bitwise | of 5 and 3 is $or<br>";
// Bitwise XOR
$xor = $first ^ $second;
echo "Bitwise ^ of 5 and 3 is $xor<br>";
// Bitwise NOT
$not = ~$first;
echo "Bitwise ~ of 5 is $not<br>";
Page 8 of 11
Output:
Bitwise & of 5 and 3 is 1
Bitwise | of 5 and 3 is 7
Bitwise ^ of 5 and 3 is 6
Bitwise ~ of 5 is -6
5 << 1 will be 10
5 >> 1 will be 2
Page 9 of 11
11. Write down rules for declaring PHP variable (any 4). 4 marks (*1)
ANS
1) A variable starts with the $ sign, followed by the name of the variable.
2) A variable name must start with a letter or the underscore character.
3) A variable name should not contain spaces. If a variable name is more than one word, it should be
separated with an underscore ($first_name), or with capitalisation ($firstName).
4) Variables used before they are assigned have default values.
5) A variable name cannot start with a number.
6) A variable name can only contain alpha-numeric characters (A Z, a-z) and underscores.
7) Variable names are case-sensitive ($name and $NAME are two different variables)
(ii) Datatypes
Datatypes tell what kind of data a variable holds.
Common datatypes:
o Integer: Whole numbers like 10, 25
o Float: Decimal numbers like 3.14, 2.5
o String: Text like "Hello", "PHP"
o Boolean: True or False
Example:
<?php
Page 10 of 11
$number = 10; // Integer
$price = 3.50; // Float
$text = "Hello"; // String
$flag = true; // Boolean
?>
(iii) Constant
Constant is a value that never changes.
We use define() to create it.
Constants do not use $.
Example:
<?php
define("PI", 3.14);
echo PI; // Output: 3.14
?>
(iv) Operators
Operators do math or compare values.
Some common types:
o + (add), - (subtract), * (multiply), / (divide)
o == (equal), != (not equal)
o && (and), || (or)
Example:
<?php
$a = 10;
$b = 5;
echo $a + $b; // Output: 15
echo ($a == $b); // Output: (false)
?>
🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅
Page 11 of 11