PHP

PHP is a server-side scripting language used to generate dynamic HTML on the server before it reaches the browser. It is commonly embedded into HTML pages using special tags and is useful for handling form input, performing arithmetic, working with arrays, and producing HTML that reflects server-side logic. This chapter shows readable, correct examples: printing output, variables, conditionals, loops, and a working subtraction calculator (final example). The code blocks below are valid and syntactically correct.

Embedding PHP & Hello

PHP blocks are placed between <?php and ?>. Anything inside these tags runs on the server. Use echo or print to send text into the generated HTML.

<?php
echo "Hello, world!";
?>

Variables & Strings

Variables begin with $. Concatenate strings with .. PHP is loosely typed — it converts types when necessary, but explicit casts or careful usage prevent surprises.

<?php
$name = "Asha";
echo "Name: " . $name . "<br>";
?>

Conditionals

Use if, else, and elseif to branch logic. Keep conditions simple and readable; prefer parentheses and clear comparisons.

<?php
$age = 12;
if ($age >= 18) {
  echo "Adult";
} else {
  echo "Child";
}
?>

Loops & Arrays

Arrays are common: iterate with foreach. Keep loops side-effect-free where possible and build output strings rather than echoing in many places for performance.

<?php
$colors = ["red", "green", "blue"];
foreach ($colors as $c) {
  echo $c . "<br>";
}
?>

Final example — calculator for subtraction

The final example below is a working subtraction calculator implemented in one PHP file. Save it as php.php and run it through a local server (XAMPP, WAMP, or similar) to see the generated HTML output. The PHP code checks input, casts to numeric type, performs subtraction, and prints the result inside the page.

php.php

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Subtract Calculator</title>
</head>
<body>
  <h1>Subtract Calculator</h1>
  <form method="GET">
    <label>First number: <input name="minuend" type="number" /></label><br />
    <label>Second number: <input name="subtrahend" type="number" /></label><br />
    <button type="submit">Calculate</button>
  </form>
  <?php
  if (isset($_GET['minuend']) && isset($_GET['subtrahend'])) {
    $minuend = (float) $_GET['minuend'];
    $subtrahend = (float) $_GET['subtrahend'];
    $difference = $minuend - $subtrahend;
    echo "<p>Difference: " . $difference . "</p>";
  }
?>
</body>
</html>

MCQs

1. What marks the start of a PHP code block in a file served by PHP?

(a) <php>

(b) <?php

(c) <script type="php">

(d) <!-- php -->

► (b) <?php

2. Which symbol begins a variable name in PHP?

(a) #

(b) $

(c) %

(d) @

► (b) $

3. Which of these prints text to the output stream in PHP?

(a) console.log()

(b) echo

(c) printf()

(d) Both (b) and (c)

► (d) Both (b) and (c)

4. What will this code output?
<?php $a = 5; $b = 2; echo $a - $b; ?>

(a) 52

(b) 3

(c) $a - $b

(d) Error

► (b) 3

5. Which operator concatenates strings in PHP?

(a) +

(b) . (dot)

(c) &

(d) concat()

► (b) . (dot)

6. Which superglobal contains GET parameters?

(a) $_POST

(b) $_GET

(c) $_REQUEST

(d) $_PARAM

► (b) $_GET

7. How do you start an if statement in PHP?

(a) if $x > 0 { ... }

(b) if ($x > 0) { ... }

(c) if ($x > 0): ... endif;

(d) Both (b) and (c)

► (d) Both (b) and (c)

8. Which function returns the number of elements in an array?

(a) len()

(b) count()

(c) size()

(d) length()

► (b) count()

9. What is the correct file extension for PHP files?

(a) .html

(b) .php

(c) .ph

(d) .phtml

► (b) .php

10. Which type cast is appropriate to ensure numeric subtraction uses decimals?

(a) (int)

(b) (float)

(c) strval()

(d) (string)

► (b) (float)

11. How do you loop over each value in an array $arr?

(a) for ($i = 0; $i < count($arr); $i++) { ... }

(b) foreach ($arr as $val) { ... }

(c) while (list(...) = each($arr)) { ... }

(d) Any of the above depending on use-case

► (d) Any of the above depending on use-case

12. What happens if you miss the semicolon after a PHP statement?

(a) The statement ends implicitly

(b) A parse error occurs

(c) It becomes a comment

(d) The server ignores it

► (b) A parse error occurs

13. Which superglobal can be used to safely check both GET and POST inputs (but may include cookies)?

(a) $_INPUT

(b) $_REQUEST

(c) $_DATA

(d) $_ALL

► (b) $_REQUEST

14. Which statement best describes PHP execution?

(a) PHP runs in the browser and manipulates the DOM

(b) PHP runs on the server and outputs HTML for the browser

(c) PHP runs inside CSS files

(d) PHP is interpreted by the client's JavaScript engine

► (b) PHP runs on the server and outputs HTML for the browser

15. Which of the following is a valid way to include one PHP file in another?

(a) include 'file.php';

(b) require 'file.php';

(c) include_once 'file.php';

(d) All of the above

► (d) All of the above

16. Which function converts a string to a float?

(a) floatval()

(b) toFloat()

(c) (float) cast

(d) Both (a) and (c)

► (d) Both (a) and (c)

17. Which practice helps avoid syntax parse errors when showing code inside HTML?

(a) Insert raw PHP tags directly without escaping

(b) Escape special characters as HTML entities (e.g., &lt;?php)

(c) Put <br /> inside PHP tags

(d) Mix templates without a parser

► (b) Escape special characters as HTML entities (e.g., &lt;?php)

18. When showing PHP examples inside an HTML preview, which is the correct way to close the PHP block so formatters don't break?

(a) <?php ... <br ?>

(b) <?php ... ?>

(c) <? php ... ? >

(d) <!-- ?-->

► (b) <?php ... ?>