BCA-64T-211: PHP Programming (Detailed Notes with Definitions & Examples)
UNIT-I: Introduction to PHP
1. What is PHP?
Definition: PHP (Hypertext Preprocessor) is a widely-used open-source server-side scripting language
designed for web development.
Example:
<?php
echo "Welcome to PHP!";
?>
2. Installation of PHP and MySQL
Definition: Installing PHP with MySQL typically involves setting up a package like XAMPP or WAMP.
Example Steps:
• Download XAMPP
• Install and start Apache, MySQL
• Place .php files in htdocs
3. PHP Configuration (IIS & Apache)
Definition: Configuration involves setting directives in php.ini and web server settings to execute PHP
scripts.
4. Features of PHP
• Open-source
• Platform Independent
• Supports multiple databases
• Embedded within HTML
5. Writing PHP Code
<?php
echo "Hello, PHP World!";
?>
1
6. Embedding PHP in HTML
<!DOCTYPE html>
<html>
<body>
<?php echo "Embedded PHP!"; ?>
</body>
</html>
UNIT-II: Control Structures
1. Data Types
Definition: Data types define the type of data a variable can hold.
Types & Examples:
• String: $name = "Yuvraj";
• Integer: $age = 21;
• Float: $price = 99.99;
• Boolean: $isOn = true;
• Array: $arr = array(1, 2, 3);
• NULL: $x = NULL;
2. Operators
Definition: Operators are symbols used to perform operations on variables and values.
Types:
• Arithmetic: + , - , * , / , %
$x = 10; $y = 3; echo $x % $y;
• Comparison: == , != , < , >
if($x > $y) echo "x is greater";
• Logical: && , || , !
• Assignment: = , += , -=
2
3. Variables
Definition: Variables store data for use in scripts. Start with $ .
$name = "PHP";
echo $name;
4. Static and Global Variables
Static:
function test() {
static $a = 0;
$a++;
echo $a;
}
Global:
$x = 5;
function show() {
global $x;
echo $x;
}
5. Comments
• Single-line: // , #
• Multi-line: /* */
6. Condition Statements
If-Else Example:
$age = 20;
if ($age >= 18) echo "Adult";
else echo "Minor";
7. Switch
$day = "Mon";
switch($day) {
3
case "Mon": echo "Monday"; break;
default: echo "Another day";
}
8. Ternary Operator
$x = 10;
echo ($x > 5) ? "Greater" : "Smaller";
9. Loops
• For:
for($i=0; $i<5; $i++) echo $i;
• While:
$i=0;
while($i<5) { echo $i; $i++; }
• Do While:
$i=0;
do { echo $i; $i++; } while($i<5);
• Foreach:
$arr = array(10, 20);
foreach($arr as $val) echo $val;
10. Exit, Die, Return
exit("Stopped execution");
11. Arrays
• Numeric Array:
4
$arr = array(1, 2, 3);
echo $arr[0];
• Associative Array:
$arr = array("name" => "Yuvraj");
echo $arr["name"];
• Multidimensional Array:
$arr = array(array(1, 2), array(3, 4));
echo $arr[0][1];
UNIT-III: Strings, Functions, Form Data Handling
1. Strings
Definition: A string is a series of characters.
Examples:
$str = "Hello World";
echo strlen($str);
echo str_replace("World", "PHP", $str);
2. Pattern Matching
preg_match("/PHP/", "Learn PHP", $match);
print_r($match);
3. Explode & Implode
$str = "red,green,blue";
$arr = explode(",", $str);
echo implode("-", $arr);
4. Functions
Definition: Reusable blocks of code that perform specific tasks.
5
function greet($name) {
return "Hello $name";
}
echo greet("Yuvraj");
5. Form Handling
<form method="POST">
<input name="name">
<input type="submit">
</form>
<?php echo $_POST['name']; ?>
6. Cookies and Sessions
setcookie("user", "Yuvraj", time()+3600);
$_SESSION['user'] = "Yuvraj";
UNIT-IV: Exception, File, and Database Handling
1. Exception Handling
Definition: Used to handle runtime errors.
try {
throw new Exception("An error occurred");
} catch (Exception $e) {
echo $e->getMessage();
}
2. File Handling
• Open/Write:
$file = fopen("test.txt", "w");
fwrite($file, "PHP File Handling");
fclose($file);
• Read:
6
$file = fopen("test.txt", "r");
echo fread($file, filesize("test.txt"));
3. Database Handling
Definition: PHP connects to databases using MySQLi or PDO.
$conn = mysqli_connect("localhost", "root", "", "testdb");
$sql = "INSERT INTO users (name) VALUES ('Yuvraj')";
mysqli_query($conn, $sql);
• Other Queries: UPDATE, DELETE, SELECT
• Truncate: TRUNCATE TABLE users;
• Alias: SELECT name AS username FROM users;
• Order By: SELECT * FROM users ORDER BY name ASC;
Let me know if you want PDF, practice questions, or diagram-based revision notes!
CREATE PDF