0% found this document useful (0 votes)
2 views5 pages

PHP Course Notes With Code Examples

This document provides comprehensive PHP course notes covering essential topics such as PHP syntax, variables, form handling, control structures, loops, arrays, string manipulation, input validation, cookies, sessions, password hashing, and database connection using MySQLi. Each section includes code examples to illustrate the concepts discussed. The notes serve as a foundational guide for understanding and using PHP effectively in web development.

Uploaded by

sammunroe2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

PHP Course Notes With Code Examples

This document provides comprehensive PHP course notes covering essential topics such as PHP syntax, variables, form handling, control structures, loops, arrays, string manipulation, input validation, cookies, sessions, password hashing, and database connection using MySQLi. Each section includes code examples to illustrate the concepts discussed. The notes serve as a foundational guide for understanding and using PHP effectively in web development.

Uploaded by

sammunroe2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PHP Course Notes with Code Examples

1. Introduction to PHP

 PHP is a popular server-side scripting language used to create dynamic web pages.
 It runs on the server, processes requests, and interacts with databases like MySQL.
 Widely supported and powers about 70% of websites worldwide.
 Commonly used with development tools like XAMPP and editors such as Visual Studio
Code.

2. Basic Syntax & Variables

 PHP code is embedded within <?php ... ?> tags.


 Variables start with $ and store data like strings, integers, floats, and booleans.
 PHP supports comments using // for single-line and /* ... */ for multi-line.

php
CopyEdit
<?php
// Single line comment
/*
Multi-line comment
*/

$name = "Alice"; // String


$age = 25; // Integer
$price = 19.99; // Float
$is_active = true; // Boolean
?>

3. Form Handling (GET vs POST)

 Forms send user input to PHP scripts via GET or POST methods.
 GET appends data to URL (less secure, limited data size).
 POST sends data in the request body (secure for sensitive data like passwords).

html
CopyEdit
<form method="POST" action="submit.php">
Username: <input type="text" name="username" />
Password: <input type="password" name="password" />
<input type="submit" value="Login" />
</form>
4. Control Structures (if, else, switch)

 Use if and else for conditional logic based on true/false conditions.


 switch handles multiple conditions more cleanly than many if-else.

php
CopyEdit
<?php
$age = 20;

if ($age >= 18) {


echo "Adult";
} else {
echo "Minor";
}

$day = "Monday";
switch ($day) {
case "Monday":
echo "Start of the week";
break;
case "Friday":
echo "Weekend is near";
break;
default:
echo "Another day";
}
?>

5. Loops (for, while)

 for loop: used when the number of iterations is known.


 while loop: used when the number of iterations depends on a condition.

php
CopyEdit
<?php
// for loop
for ($i = 0; $i < 5; $i++) {
echo "Count: $i<br>";
}

// while loop
$count = 0;
while ($count < 5) {
echo "Count: $count<br>";
$count++;
}
?>

6. Arrays & Associative Arrays


 Arrays store lists of values.
 Associative arrays use key-value pairs.

php
CopyEdit
<?php
$fruits = ["Apple", "Banana", "Cherry"];
echo $fruits[1]; // Outputs Banana

$capitals = [
"USA" => "Washington D.C.",
"France" => "Paris",
"Japan" => "Tokyo"
];
echo $capitals["France"]; // Outputs Paris
?>

7. String Manipulation

 Useful functions: strtolower(), strtoupper(), str_replace(), substr(), strlen().

php
CopyEdit
<?php
$text = "Hello World!";
echo strtolower($text); // hello world!
echo strtoupper($text); // HELLO WORLD!
echo str_replace("World", "PHP", $text); // Hello PHP!
echo substr($text, 0, 5); // Hello
echo strlen($text); // 12
?>

8. Input Validation & Sanitization

 Always sanitize and validate user input to prevent security risks.


 Use PHP filters like FILTER_SANITIZE_EMAIL and FILTER_VALIDATE_EMAIL.

php
CopyEdit
<?php
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Invalid email address.";
} else {
echo "Valid email: $email";
}
?>
9. Cookies & Sessions

 Cookies store small data on the client-side; expire after a set time.
 Sessions store data server-side and persist across pages during a user visit.

php
CopyEdit
<?php
// Set a cookie for 1 hour
setcookie("user", "Alice", time() + 3600);

// Start a session
session_start();
$_SESSION['username'] = "Alice";
?>

10. Password Hashing & Authentication

 Never store plain passwords.


 Use password_hash() for hashing and password_verify() for checking.

php
CopyEdit
<?php
$password = "mypassword";

// Hash password
$hashed_password = password_hash($password, PASSWORD_DEFAULT);

// Verify password
if (password_verify("mypassword", $hashed_password)) {
echo "Password is valid!";
} else {
echo "Invalid password!";
}
?>

11. Database Connection and Queries (MySQLi)

 Use MySQLi to connect to a MySQL database and perform queries.

php
CopyEdit
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydatabase";

// Connect
$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// Insert user data


$sql = "INSERT INTO users (username, password) VALUES ('Alice',
'$hashed_password')";

if (mysqli_query($conn, $sql)) {
echo "New record created successfully.";
} else {
echo "Error: " . mysqli_error($conn);
}

mysqli_close($conn);
?>

You might also like