Short overview on Unit-II
- Get link
- X
- Other Apps
Unit – II: Working with Basic Building Blocks of PHP
1. Understand PHP File Structure
-
PHP stands for Hypertext Preprocessor.
-
PHP code is embedded inside HTML and executed on the server before the page is sent to the browser.
-
File extension:
.php
Basic Structure:
<!DOCTYPE html>
<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<?php
// PHP code starts with <?php and ends with ?>
echo "Hello, World!"; // Output to browser
?>
</body>
</html>
✅ Note: Everything outside <?php ... ?> is treated as normal HTML.
✅ PHP files must be saved with .php extension.
2. Steps to Install & Test Web Server
PHP requires a web server (like Apache or Nginx) to run.
Installation (Windows – using XAMPP):
-
Download XAMPP from https://www.apachefriends.org.
-
Install it → Select Apache + MySQL + PHP components.
-
Start Apache server from XAMPP control panel.
-
Place PHP files inside the folder:
C:\xampp\htdocs\ -
Open browser and type:
http://localhost/myfile.php
Testing:
Create test.php inside htdocs folder:
<?php
phpinfo();
?>
If PHP configuration page opens → Installation successful ✅.
3. Working of PHP
🔹 Steps of execution:
-
Client (browser) requests a PHP page →
http://localhost/page.php. -
Web server (Apache) receives request.
-
Apache sends the request to the PHP interpreter.
-
PHP code is processed on server-side (queries DB, calculations, etc.).
-
Output (usually HTML/CSS/JS) is returned to browser.
⚠️ Note: Browser never sees PHP code, only the final result.
4. Steps to Configure Apache to use PHP
If not using XAMPP/WAMP (manual setup):
-
Install Apache Server.
-
Install PHP (download from php.net).
-
Edit
httpd.conf(Apache configuration file):LoadModule php_module "c:/php/php8apache2_4.dll" AddType application/x-httpd-php .php PHPIniDir "C:/php" -
Restart Apache.
-
Test with
phpinfo()as explained earlier.
5. Use PHP Variables, Data Types, and Operators
(a) Variables
-
Declared using
$sign. -
Rules:
-
Start with
$ -
Must start with a letter or underscore.
-
Case-sensitive.
-
Example:
<?php
$name = "Jaydeep";
$age = 21;
echo "My name is $name and I am $age years old.";
?>
(b) Data Types in PHP
-
String →
"Hello" -
Integer →
10, -50 -
Float/Double →
3.14 -
Boolean →
true/false -
Array →
["red","green","blue"] -
Object
-
NULL
-
Resource
Example:
<?php
$str = "PHP";
$num = 25;
$float = 3.14;
$bool = true;
$arr = array("apple","banana","mango");
?>
6. Describe PHP Operators
Operators perform actions on variables and values.
Categories:
-
Arithmetic Operators
-
+(add),-(subtract),*(multiply),/(divide),%(modulus)
$x=10; $y=3; echo $x % $y; // 1 -
-
Assignment Operators
-
=,+=,-=,*=,/=,.=
$a = 5; $a += 3; // 8 -
-
Comparison Operators
-
==(equal),===(identical),!=(not equal),<,>,<=,>=
if($x == $y) echo "Equal"; -
-
Logical Operators
-
&&(AND),||(OR),!(NOT)
-
-
String Operators
-
.(concatenation),.=(append)
$str1="Hello"; $str2="PHP"; echo $str1.$str2; // HelloPHP -
-
Increment/Decrement
-
++$x,$x++,--$x,$x--
-
-
Ternary Operator
-
(condition) ? value_if_true : value_if_false
-
7. Apply Control Structures in Programming
Control structures manage program flow.
(a) If Statement
<?php
$age = 18;
if($age >= 18){
echo "You can vote";
}
?>
(b) If-Else
if($age >= 18){
echo "Adult";
} else {
echo "Minor";
}
(c) If-Elseif-Else
$marks = 75;
if($marks >= 90) echo "Grade A";
elseif($marks >= 60) echo "Grade B";
else echo "Grade C";
(d) Switch Statement
$day = "Tue";
switch($day){
case "Mon": echo "Monday"; break;
case "Tue": echo "Tuesday"; break;
default: echo "Other Day";
}
(e) Loops
-
While Loop
$i=1; while($i<=5){ echo $i."<br>"; $i++; } -
Do-While Loop
$i=1; do{ echo $i."<br>"; $i++; }while($i<=5); -
For Loop
for($i=1; $i<=5; $i++){ echo $i."<br>"; } -
Foreach Loop (for arrays)
$colors = array("red","green","blue"); foreach($colors as $c){ echo $c."<br>"; }
✅ That completes Unit-II basics of PHP.
📝 Unit – II: Working with Basic Building Blocks of PHP
Part A: MCQs (15 Questions)
Q1. Which file extension is used for PHP files?
a) .html
b) .php
c) .xml
d) .js
✅ Answer: b) .php
Q2. Which tag is used to start PHP code?
a) <php>
b) <?php
c) <?
d) </php>
✅ Answer: b) <?php
Q3. PHP is a _________ language.
a) Client-side scripting
b) Server-side scripting
c) Markup
d) Compiled
✅ Answer: b) Server-side scripting
Q4. In XAMPP, PHP files are placed inside which folder?
a) /bin
b) /htdocs
c) /config
d) /phpfiles
✅ Answer: b) /htdocs
Q5. Which function displays PHP configuration details?
a) phpinfo()
b) config()
c) info()
d) serverinfo()
✅ Answer: a) phpinfo()
Q6. Which operator is used for string concatenation in PHP?
a) +
b) .
c) &
d) *
✅ Answer: b) .
Q7. Which of the following is NOT a PHP data type?
a) String
b) Boolean
c) Float
d) Character
✅ Answer: d) Character (PHP does not have separate char type)
Q8. $a += 5; is equivalent to:
a) $a = $a + 5;
b) $a = $a - 5;
c) $a = $a * 5;
d) $a = $a / 5;
✅ Answer: a) $a = $a + 5;
Q9. Which of the following is a comparison operator?
a) &&
b) ==
c) ++
d) =
✅ Answer: b) ==
Q10. Which control structure is best for checking multiple conditions on the same variable?
a) if-else
b) switch
c) while
d) foreach
✅ Answer: b) switch
Q11. Which loop executes at least once, even if condition is false?
a) for
b) while
c) do-while
d) foreach
✅ Answer: c) do-while
Q12. Which statement outputs data in PHP?
a) print
b) echo
c) both a & b
d) printf only
✅ Answer: c) both a & b
Q13. Which of the following is used for logical AND in PHP?
a) &
b) AND
c) &&
d) Both b and c
✅ Answer: d) Both b and c
Q14. $x = 10; $y = "10"; if($x === $y) → Result?
a) True
b) False
✅ Answer: b) False (because === checks type and value)
Q15. Which of the following is used to execute code repeatedly for each element of an array?
a) while
b) do-while
c) foreach
d) switch
✅ Answer: c) foreach
Part B: 3–4 Marks Questions with Answers
Q1. Explain PHP file structure with an example.
Answer:
-
A PHP file can contain both HTML and PHP code.
-
PHP code is written inside
<?php ... ?>tags. -
Example:
<!DOCTYPE html> <html> <body> <?php echo "Hello, PHP!"; ?> </body> </html> -
The above code sends “Hello, PHP!” to the browser.
Q2. Write steps to install and test a PHP web server using XAMPP.
Answer:
-
Download XAMPP from apachefriends.org.
-
Install it and start Apache server.
-
Save PHP files inside
C:/xampp/htdocs/. -
Create
test.phpfile with code:<?php phpinfo(); ?> -
Open
http://localhost/test.phpin browser. -
If PHP configuration page opens → Server is working.
Q3. Explain the working of PHP with a diagram.
Answer:
-
Browser requests a PHP page (
index.php). -
Apache server receives the request.
-
Server forwards it to the PHP interpreter.
-
PHP executes code (e.g., DB queries, calculations).
-
Server sends the final HTML output to the browser.
Diagram:
Browser → Apache → PHP Engine → Output (HTML) → Browser
Q4. State steps to configure Apache to use PHP manually.
Answer:
-
Install Apache and PHP separately.
-
Open Apache config file (
httpd.conf). -
Add lines:
LoadModule php_module "c:/php/php8apache2_4.dll" AddType application/x-httpd-php .php PHPIniDir "C:/php" -
Restart Apache.
-
Test using
phpinfo().
Q5. List PHP data types with examples.
Answer:
-
String →
$name = "Jay"; -
Integer →
$age = 21; -
Float →
$pi = 3.14; -
Boolean →
$isTrue = true; -
Array →
$colors = array("red","blue","green"); -
Object →
$obj = new ClassName(); -
NULL →
$val = NULL; -
Resource → DB connection, file handle.
Q6. Describe PHP operators with suitable examples.
Answer:
-
Arithmetic:
+ - * / %→$a+$b -
Assignment:
= += -= *=→$a+=5 -
Comparison:
== === != > <→$a==$b -
Logical:
&& || !→$a && $b -
String:
..=→$str1.$str2 -
Increment/Decrement:
++ --→$a++
Q7. Differentiate between while, do-while, and for loop in PHP.
Answer:
-
while: condition checked before loop body.
-
do-while: executes at least once, condition checked after body.
-
for: used when number of iterations is known.
Example:
while($i<5) { ... }
do { ... } while($i<5);
for($i=0; $i<5; $i++) { ... }
Q8. Write a PHP program using switch statement to print day of week.
Answer:
<?php
$day = 3;
switch($day){
case 1: echo "Monday"; break;
case 2: echo "Tuesday"; break;
case 3: echo "Wednesday"; break;
default: echo "Other day";
}
?>
- Get link
- X
- Other Apps
Comments
Post a Comment