0% found this document useful (0 votes)
3 views8 pages

PHP Basics Handout

This document provides an overview of PHP, a server-side scripting language used for web development, covering its key features, setup, syntax, variables, data types, operators, control structures, arrays, functions, form handling, and database interaction. It also outlines next steps for further learning, including object-oriented PHP and frameworks. The document serves as a foundational guide for beginners to understand and start using PHP effectively.

Uploaded by

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

PHP Basics Handout

This document provides an overview of PHP, a server-side scripting language used for web development, covering its key features, setup, syntax, variables, data types, operators, control structures, arrays, functions, form handling, and database interaction. It also outlines next steps for further learning, including object-oriented PHP and frameworks. The document serves as a foundational guide for beginners to understand and start using PHP effectively.

Uploaded by

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

PHP Basics Handout

Learn the fundamentals of PHP for server-side web development

1. Introduction to PHP
PHP (Hypertext Preprocessor) is a server-side scripting language used for:

 Building dynamic websites


 Handling forms, sessions, and databases
 Creating APIs and web applications

Key Features:

 Open-source & cross-platform


 Works with HTML, MySQL, and JavaScript
 Runs on the server (unlike JavaScript, which runs in the browser)

2. Setting Up PHP
To run PHP, you need:

 A server (e.g., XAMPP, WAMP, or MAMP)


 A .php file (processed by the server)

Basic PHP Syntax


php

Copy
Download
<?php
// PHP code goes here
echo "Hello, World!";
?>

 PHP code is embedded inside <?php ... ?>


 echo outputs text to the browser

3. Variables & Data Types


PHP is loosely typed (no need to declare variable types).

Variables
php

Copy
Download
<?php
$name = "John"; // String
$age = 25; // Integer
$price = 9.99; // Float
$isStudent = true; // Boolean
?>

 Variables start with $


 Case-sensitive ($name ≠ $Name)

Common Data Types

Type Example

String "Hello"

Integer 42

Float 3.14

Boolean true / false


Type Example

Array $colors = ["red", "green"]

Null $var = null;

4. Operators
Arithmetic Operators
php

Copy
Download
$a = 10; $b = 5;
echo $a + $b; // 15 (Addition)
echo $a - $b; // 5 (Subtraction)
echo $a * $b; // 50 (Multiplication)
echo $a / $b; // 2 (Division)
echo $a % $b; // 0 (Modulus)

Comparison Operators
php

Copy
Download
$a == $b // Equal (value)
$a === $b // Identical (value & type)
$a != $b // Not equal
$a > $b // Greater than

Logical Operators
php

Copy
Download
if ($age > 18 && $isStudent) { ... } // AND
if ($age < 18 || $hasTicket) { ... } // OR
if (!true) { ... } // NOT
5. Control Structures
If-Else Statements
php

Copy
Download
<?php
$age = 20;
if ($age >= 18) {
echo "You are an adult.";
} else {
echo "You are a minor.";
}
?>

Loops

1. while Loop
php

Copy
Download
$i = 1;
while ($i <= 5) {
echo $i;
$i++;
}

2. for Loop
php

Copy
Download
for ($i = 1; $i <= 5; $i++) {
echo $i;
}

3. foreach Loop (for arrays)


php

Copy
Download
$colors = ["red", "green", "blue"];
foreach ($colors as $color) {
echo $color;
}

6. Arrays
Indexed Arrays
php

Copy
Download
$fruits = ["Apple", "Banana", "Orange"];
echo $fruits[0]; // "Apple"

Associative Arrays (Key-Value Pairs)


php

Copy
Download
$person = [
"name" => "John",
"age" => 25,
"city" => "New York"
];
echo $person["name"]; // "John"

7. Functions
Basic Function
php

Copy
Download
function greet($name) {
return "Hello, " . $name;
}
echo greet("Alice"); // "Hello, Alice"

Built-in PHP Functions

Function Description

strlen($str) String length

strtoupper($str) Convert to uppercase

date("Y-m-d") Current date (e.g., 2025-06-09)

count($array) Number of elements in an array

8. Handling Forms (GET & POST)


HTML Form (index.html)
html

Copy
Download
Run
<form action="process.php" method="POST">
<input type="text" name="username">
<input type="submit" value="Submit">
</form>

PHP Form Handling (process.php)


php

Copy
Download
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
echo "Hello, " . $username;
}
?>

 $_GET – Data from URL (e.g., ?id=1)

 $_POST – Data from form submissions

9. Working with Databases (MySQL)


Connecting to MySQL
php

Copy
Download
$conn = mysqli_connect("localhost", "username", "password", "dbname");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

Querying Data
php

Copy
Download
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo $row["username"];
}
mysqli_close($conn);

10. Next Steps


 Learn Object-Oriented PHP (OOP)
 Explore PHP frameworks (Laravel, Symfony)
 Build a CRUD app (Create, Read, Update, Delete)

You might also like