0% found this document useful (0 votes)
8 views11 pages

PHP Answers

The document outlines key concepts of PHP, including its advantages, data types, control structures, and syntax. It explains the use of variables, decision-making statements, loops, and operators, providing examples for clarity. Additionally, it covers specific PHP functions like echo and print, as well as rules for declaring variables.

Uploaded by

tejas.gokhale09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views11 pages

PHP Answers

The document outlines key concepts of PHP, including its advantages, data types, control structures, and syntax. It explains the use of variables, decision-making statements, loops, and operators, providing examples for clarity. Additionally, it covers specific PHP functions like echo and print, as well as rules for declaring variables.

Uploaded by

tejas.gokhale09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

UNIT 1

1. Describe advantage of PHP. 2 marks (*4)


State advantages of PHP-MySQL. 2 marks (*1)
ANS
 Open Source: PHP source code and software are freely available on the web. We can develop all the
versions of PHP according to our requirement without paying any cost. All its components are free to
download and use.
 Embedded: PHP code can be easily embedded within HTML tags and script.
 Platform Independent: PHP is available for WINDOWS, MAC, and LINUX& UNIX operating
system. A PHP application developed in one OS can be easily executed in other OS also.
 Database Support: PHP supports all the leading databases such as MySQL, SQLite, ODBC, etc.
 Security: PHP is a secure language to develop the website. It consists of multiple layers of security to
prevent threads and malicious attacks.

2. List any four data types in MySQL. 2 marks (*1)


ANS
Data Type Size Description
CHAR(size) Max 255 characters Fixed-length character
strings.
VARCHAR Max 255 characters Variable-length character
(size) string
TEXT(size) Max 65,535 characters Size is the number of
characters to store.
INT(m)/ 4 bytes Standard integer values.
INTEGER( m) Signed: -2,147,483,648 to 2,147,483,647
Unsigned: 0 to 4,294,967,295
DATE() 3 bytes Format: 'YYYY-MM-DD'
DATETIME() 8 bytes Format: 'YYYY-MM-DD
Range: '1000-01-01 00:00:00' to '9999-12-31 23:59:59' HH:MM:SS'

3. State the use of “$” sign in PHP. 2 marks (*1)


ANS
$ sign in PHP is used to indicate a variable.
A variable starts with the $ sign, followed by the name of the variable.
Example: $a=10

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’;
?>

5. State different data types in PHP. 2 marks (*2)


Implement any three data types used in PHP with illustration. 6 marks (*1)
List any four data types of PHP. 2 marks (*1)
ANS
Data Types : PHP provides eight types of data types:
 Scalar (single-value) types:
1) integer
2) float
3) string
4) boolean
 Compound (collection) types:
1) array
2) object
 Special types:
1) resource
2) NULL

1) Boolean: A boolean value represents either true(1) or false(0). Boolean values are often used in
conditional testing.
Example: $a = true;

2) NULL: The NULL value represents a variable that has no value.


Syntax: $var_name= NULL;
Example: $v = NULL;

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;

5) Float: Numbers with decimal point or in exponential form.


Example: $y = 3.14;

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;

if ($num1 > $num2) {


echo "Largest number is: $num1";
} elseif ($num2 > $num1) {
echo "Largest number is: $num2";
} else {
echo "Both numbers are equal.";
}
?>
Output:
Largest number is: 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

8. Write PHP program to find largest of two number. 2 marks (*1)


ANS
<?php
$num1 = 20;
$num2 = 45;

if ($num1 > $num2) {


echo "Largest number is: $num1";
} elseif ($num2 > $num1) {
echo "Largest number is: $num2";
} else {
echo "Both numbers are equal.";
}
?>

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>";

// Bitwise Left Shift


$leftShift = $first << 1;
echo "5 << 1 will be $leftShift<br>";

// Bitwise Right Shift


$rightShift = $first >> 1;
echo "5 >> 1 will be $rightShift<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

10. Explain print and echo statement in PHP. 4 marks (*1)


ANS
The PHP echo Statement:
 The echo statement can be used with or without parentheses: echo or echo().
 The echo statement can display anything that can be displayed to the browser, such as string, numbers,
variables values, the results of expressions etc.
 Example:
<?php
echo "Hello from echo!<br>";

echo("Welcome to PHP Programming!");


?>
 Output:
Hello from echo!
Welcome to PHP Programming!

The PHP print Statement:


 The PHP print statement is similar to the echo statement and can be used alternative to echo at many
times.
 It is also language construct and so we may not use parenthesis : print or print().
 print statement can also be used to print strings and variables.
 Example:
<?php
print "Hello from print!<br>";

print("Learning PHP is fun!");


?>
 Output:
Hello from print!
Learning PHP is fun!

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)

12. Explain the following terms : 4 marks (*1)


(i) Variables (ii) Datatypes (iii) Constant (iv) Operators
ANS
(i) Variables
 Variables are like boxes that store data.
 In PHP, variables start with $.
 You can put numbers, words, or anything inside them.
 Example:
<?php
$name = "Amit";
$age = 20;
echo $name; // Output: Amit
?>

(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

You might also like