12th Computer Applications PHP Guide: Functions, Syntax and Loops in One Place
Six of the eighteen chapters in Class 12 Computer Applications are about PHP, so this block carries a large share of the paper. The questions are consistent: they ask about syntax rules, operator names, loop types, file functions and the mysqli functions with their parameter counts. This guide puts all of that on one page with small examples.
PHP basics (Unit 4)
PHP stands for PHP: Hypertext Preprocessor. It is a server-side scripting language, and its files are saved with the .php extension.
<?php
$name = "Tamil"; // every variable starts with $
$marks = 95.5; // float: a decimal number
echo $name;
?>
- Eight data types: integer, float, string, boolean, array, object, resource and NULL.
- Case sensitivity:
$Nameand$nameare different variables, but keywords and function names are not case-sensitive. - Operators:
=assigns, comparison operators compare two values, and===is called identical because it checks both value and type.
Functions and arrays (Unit 5)
<?php
function greet() { // user-defined function
echo "Welcome";
}
greet(); // calling the function
$subjects = array("Tamil", "English", "Maths");
echo $subjects[2]; // Maths
?>
- A function is a block of code that performs a specific task. Pre-defined functions are called built-in functions.
- A user-defined function starts with the keyword
function. - PHP has three types of arrays: indexed, associative and multidimensional.
- The first index is 0, so in an array of five elements the third element is at index 2.
- An indexed array can be created with
array()or with square brackets, and elements are read with the index in square brackets.
Conditional statements (Unit 6)
<?php
switch ($day) {
case 1: echo "Monday"; break;
case 2: echo "Tuesday"; break;
default: echo "Other day";
}
?>
- The simplest conditional statement is
if. If its condition is false, the block is skipped. elseifadds further conditions to an if...else chain.- In a switch,
casegives each option,defaultruns when no case matches, andbreakends the switch. whileis not a conditional statement. It is a loop, and it is the answer to the "which is not a conditional statement" question.
Loops (Unit 7)
| Loop | Type | When the condition is checked |
|---|---|---|
for | Entry-check | Before each iteration |
while | Entry-check | Before each iteration |
do...while | Exit-check | After each iteration, so the body runs at least once |
foreach | Array loop | Once for every element of an array |
<?php
for ($i = 1; $i <= 3; $i++) { // initialization; condition; increment
echo $i;
}
$array = array(1, 2, 3, 4, 5);
foreach ($array as $value) {
echo $value; // prints each element in order
}
?>
The three parts of a for loop are initialization, condition and increment. Initialization runs only once, at the start. The increment part updates the loop variable after every pass.
Forms and files (Unit 8)
- HTML forms collect input from users and are created with the
formtag. - Checkboxes allow several selections; radio buttons allow only one at a time.
- Validation checks the data a user submits. There are two types: client-side and server-side.
<?php
$file = fopen("notes.txt", "r"); // open
$text = fread($file, filesize("notes.txt")); // read
fclose($file); // close
?>
Connecting PHP and MySQL (Unit 9)
The parameter counts are the most frequently tested detail in this chapter, so learn this table exactly as the textbook presents it.
| Function | Purpose | Parameters |
|---|---|---|
mysqli_connect() | Open a connection | 4: server name, user name, password, database name |
mysqli_query() | Run an SQL query | 2: connection object, SQL query |
mysqli_close() | Close the connection | 1: connection object |
mysqli_num_rows() | Count rows in a result | 1: result |
<?php
$conn = mysqli_connect("localhost", "root", "", "school");
$result = mysqli_query($conn, "SELECT * FROM students");
echo mysqli_num_rows($result);
mysqli_close($conn);
?>
Two more facts from this chapter: MySQLi functions are supported from PHP version 5, and mysqli_select_data() is not a real function. The correct one is mysqli_select_db().
Practise the PHP block
Work through the chapters in order, because each one uses the previous one: PHP basics, functions and arrays, conditional statements, loops, forms and files and connecting PHP and MySQL. Typing and running the small examples above on your own computer is the fastest way to make the syntax stick.