PHP (Hypertext Preprocessor Notes)

 

PHP Programming Notes


1. Introduction to PHP

1.1 What is PHP?
  • PHP stands for Hypertext Preprocessor, which is a popular server-side scripting language used to create dynamic web pages.
  • It is embedded into HTML to produce dynamic content, like interacting with databases, handling forms, and performing server-side operations.
  • PHP is open-source and runs on most servers, like Apache and Nginx.
1.2 Syntax of PHP

A PHP script starts with <?php and ends with ?>. PHP code can be embedded into an HTML file.

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

2. PHP Variables

2.1 Declaring Variables

Variables in PHP are declared using a dollar sign ($) followed by the variable name.

<?php
$name = "John"; $age = 25; echo $name; ?>
2.2 Data Types in PHP
  • String: A sequence of characters.
  • Integer: A whole number.
  • Float: A decimal number.
  • Boolean: Represents true or false.
  • Array: An ordered collection of values.
  • Object: An instance of a class.
<?php
$name = "John"; // String $age = 25; // Integer $price = 19.99; // Float $is_student = true; // Boolean ?>

3. PHP Operators

3.1 Arithmetic Operators

These operators perform basic mathematical operations.

<?php
$sum = 10 + 5; // Addition $diff = 10 - 5; // Subtraction $prod = 10 * 5; // Multiplication $quotient = 10 / 5; // Division $remainder = 10 % 3; // Modulus ?>
3.2 Relational Operators

Used to compare two values.

<?php
$isEqual = (10 == 5); // Equal to $isNotEqual = (10 != 5); // Not equal to $isGreaterThan = (10 > 5); // Greater than $isLessThan = (10 < 5); // Less than ?>
3.3 Logical Operators

Used for logical operations.

<?php
$and = (true && false); // AND $or = (true || false); // OR $not = !true; // NOT ?>

4. PHP Control Structures

4.1 If-Else Statement

The if-else statement is used to execute code conditionally.

<?php
$age = 18; if ($age >= 18) { echo "You are eligible to vote."; } else { echo "You are not eligible to vote."; } ?>
4.2 Switch-Case Statement

The switch-case statement is used for multi-way branching.

<?php
$day = 3; switch ($day) { case 1: echo "Monday"; break; case 2: echo "Tuesday"; break; case 3: echo "Wednesday"; break; default: echo "Invalid day"; } ?>
4.3 Loops in PHP
  • For loop: Executes a block of code a fixed number of times.
<?php
for ($i = 0; $i < 5; $i++) { echo $i; } ?>
  • While loop: Executes as long as the condition is true.
<?php
$i = 0; while ($i < 5) { echo $i; $i++; } ?>
  • Do-While loop: Executes at least once and then checks the condition.
<?php
$i = 0; do { echo $i; $i++; } while ($i < 5); ?>

5. PHP Functions

5.1 Defining Functions

A function is a block of code that can be called to perform a specific task.

<?php
function greet() { echo "Hello, welcome to PHP!"; } greet(); // Calling the function ?>
5.2 Functions with Parameters

Functions can accept parameters to make them more dynamic.

<?php
function greet($name) { echo "Hello, " . $name . "!"; } greet("John"); // Output: Hello, John! ?>

6. Arrays in PHP

6.1 Indexed Arrays

An indexed array stores values with numeric indices.

<?php
$fruits = array("Apple", "Banana", "Cherry"); echo $fruits[0]; // Output: Apple ?>
6.2 Associative Arrays

An associative array stores values with named keys.

<?php
$person = array("name" => "John", "age" => 25); echo $person["name"]; // Output: John ?>
6.3 Multidimensional Arrays

Arrays containing other arrays.

<?php
$students = array( array("John", 25), array("Jane", 22) ); echo $students[0][0]; // Output: John ?>

7. PHP Forms Handling

7.1 Getting Form Data Using $_GET

When a form is submitted with method GET, the data is sent in the URL.

<form method="GET" action="process.php">
Name: <input type="text" name="name"> <input type="submit"> </form> <?php // process.php echo $_GET['name']; ?>
7.2 Getting Form Data Using $_POST

When a form is submitted with method POST, the data is sent in the HTTP body.

<form method="POST" action="process.php">
Name: <input type="text" name="name"> <input type="submit"> </form> <?php // process.php echo $_POST['name']; ?>

8. File Handling in PHP

8.1 Writing to a File
<?php
$file = fopen("file.txt", "w"); fwrite($file, "Hello, PHP file handling!"); fclose($file); ?>
8.2 Reading from a File
<?php
$file = fopen("file.txt", "r"); $content = fread($file, filesize("file.txt")); echo $content; fclose($file); ?>

9. Object-Oriented Programming (OOP) in PHP

9.1 Classes and Objects

A class is a blueprint for objects.

<?php
class Car { public $model; public $color; function __construct($model, $color) { $this->model = $model; $this->color = $color; } function displayDetails() { echo "Model: " . $this->model . "<br>"; echo "Color: " . $this->color; } } $car1 = new Car("Toyota", "Red"); $car1->displayDetails(); ?>
9.2 Inheritance

Inheritance allows a class to inherit properties and methods from another class.

<?php
class Animal { public $name; function __construct($name) { $this->name = $name; } function speak() { echo $this->name . " makes a sound.<br>"; } } class Dog extends Animal { function speak() { echo $this->name . " barks.<br>"; } } $dog = new Dog("Buddy"); $dog->speak(); // Output: Buddy barks. ?>

10. Exception Handling in PHP

10.1 Try-Catch Block

Exception handling is done using try-catch blocks.

<?php
try { $result = 10 / 0; } catch (Exception $e) { echo "Error: " . $e->getMessage(); } ?>

Assessment: Regular Tests, Assignments, and Final Project Evaluation

Certification: Certificate of Completion from Disha Institute

Number of Days Depends on your practice and feedback. The more you practice and review your work, the faster you will complete the course.

For Batch time and Fess contact to Below Address and Number.

Zamanat Sir       (MCA / Bsc. I.T / ‘A’ / ‘O’ / CCC / Govt. Certified  Domain Skill Trainer Disha Institute 153 Vijay Nagar Opp. Rg Pg College W.K Road Meerut     (9411617329 , 9458516690) 

Courses                Reviews &Location                  Download

For More Details such as Courses , Job updation , Notes , Videos ,Links  Click Here

(MCA / Bsc. I.T / ‘A’ / ‘O’ / CCC / Govt. Certified  Domain Skill Trainer )

   Disha Institute 153 Vijay Nagar Opp. Rg Pg College W.K Road Meerut 

(9411617329 , 9458516690) 

Comments

Popular posts from this blog

VBA Modules for Excel Automation (Advance Excel)

Java Programming

Excel Shortcuts