Short overview on Unit-II

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

  1. Download XAMPP from https://www.apachefriends.org.

  2. Install it → Select Apache + MySQL + PHP components.

  3. Start Apache server from XAMPP control panel.

  4. Place PHP files inside the folder:

    C:\xampp\htdocs\
  5. 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:

  1. Client (browser) requests a PHP pagehttp://localhost/page.php.

  2. Web server (Apache) receives request.

  3. Apache sends the request to the PHP interpreter.

  4. PHP code is processed on server-side (queries DB, calculations, etc.).

  5. 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):

  1. Install Apache Server.

  2. Install PHP (download from php.net).

  3. Edit httpd.conf (Apache configuration file):

    LoadModule php_module "c:/php/php8apache2_4.dll" AddType application/x-httpd-php .php PHPIniDir "C:/php"
  4. Restart Apache.

  5. 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

  1. String"Hello"

  2. Integer10, -50

  3. Float/Double3.14

  4. Booleantrue / false

  5. Array["red","green","blue"]

  6. Object

  7. NULL

  8. 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:

  1. Arithmetic Operators

    • + (add), - (subtract), * (multiply), / (divide), % (modulus)

    $x=10; $y=3; echo $x % $y; // 1
  2. Assignment Operators

    • =, +=, -=, *=, /=, .=

    $a = 5; $a += 3; // 8
  3. Comparison Operators

    • == (equal), === (identical), != (not equal), <, >, <=, >=

    if($x == $y) echo "Equal";
  4. Logical Operators

    • && (AND), || (OR), ! (NOT)

  5. String Operators

    • . (concatenation), .= (append)

    $str1="Hello"; $str2="PHP"; echo $str1.$str2; // HelloPHP
  6. Increment/Decrement

    • ++$x, $x++, --$x, $x--

  7. 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

  1. While Loop

    $i=1; while($i<=5){ echo $i."<br>"; $i++; }
  2. Do-While Loop

    $i=1; do{ echo $i."<br>"; $i++; }while($i<=5);
  3. For Loop

    for($i=1; $i<=5; $i++){ echo $i."<br>"; }
  4. 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:

  1. Download XAMPP from apachefriends.org.

  2. Install it and start Apache server.

  3. Save PHP files inside C:/xampp/htdocs/.

  4. Create test.php file with code:

    <?php phpinfo(); ?>
  5. Open http://localhost/test.php in browser.

  6. If PHP configuration page opens → Server is working.


Q3. Explain the working of PHP with a diagram.
Answer:

  1. Browser requests a PHP page (index.php).

  2. Apache server receives the request.

  3. Server forwards it to the PHP interpreter.

  4. PHP executes code (e.g., DB queries, calculations).

  5. 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:

  1. Install Apache and PHP separately.

  2. Open Apache config file (httpd.conf).

  3. Add lines:

    LoadModule php_module "c:/php/php8apache2_4.dll" AddType application/x-httpd-php .php PHPIniDir "C:/php"
  4. Restart Apache.

  5. Test using phpinfo().


Q5. List PHP data types with examples.
Answer:

  1. String → $name = "Jay";

  2. Integer → $age = 21;

  3. Float → $pi = 3.14;

  4. Boolean → $isTrue = true;

  5. Array → $colors = array("red","blue","green");

  6. Object → $obj = new ClassName();

  7. NULL → $val = NULL;

  8. 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"; } ?>

Comments

Popular posts from this blog

Unit - 1 Question and Answer

Short overview on Unit-III