4

PHP (Hypertext Preprocessor) – Notes

1. Introduction

  • PHP is a server-side scripting language.

  • Mainly used for web development to create dynamic web pages.

  • Can interact with databases (like MySQL).

  • Free and open-source.


2. Features of PHP

  1. Server-side scripting – Code runs on server.

  2. Cross-platform – Works on Windows, Linux, Mac.

  3. Database Support – MySQL, PostgreSQL, Oracle, SQLite.

  4. Easy to learn – Syntax similar to C, Java, Perl.

  5. Open-source – Free to use and distribute.

  6. Fast execution – Efficient for web apps.


3. PHP Syntax

  • PHP code is written inside <?php ... ?> tags.

 
<?php echo "Hello World"; ?>
  • echo or print is used to display output.


4. PHP Variables

  • Variables start with $ symbol.

  • Case-sensitive.

  • Example:

 
$name = "Dinesh"; $age = 25;
  • Data types:

    • String: "Hello"

    • Integer: 10

    • Float: 10.5

    • Boolean: true / false

    • Array: array(1,2,3)

    • Object


5. PHP Constants

  • Value cannot change once defined.

 
define("PI", 3.14); echo PI;

6. PHP Operators

  1. Arithmetic: + - * / % **

  2. Assignment: = += -= *= /= %=

  3. Comparison: == != === !== > < >= <=

  4. Logical: && || !

  5. Increment/Decrement: ++ --

  6. String: . (concatenation)


7. PHP Strings

  • Concatenation:

 
$first = "Dinesh"; $last = "Pagare"; echo $first . " " . $last;
  • String functions: strlen(), strtoupper(), strtolower(), substr()


8. PHP Arrays

  1. Indexed Array

 
$fruits = array("Apple","Banana","Orange"); echo $fruits[0]; // Apple
  1. Associative Array

 
$age = array("Dinesh"=>25,"Ravi"=>30); echo $age["Dinesh"];
  1. Multidimensional Array

 
$students = array( array("Dinesh",25), array("Ravi",30) ); echo $students[0][0]; // Dinesh
  • Array functions: count(), array_push(), array_pop(), sort(), array_merge()


9. PHP Conditional Statements

 
if($age > 18) { echo "Adult"; } else if($age > 12) { echo "Teenager"; } else { echo "Child"; } switch($day) { case "Mon": echo "Monday"; break; case "Tue": echo "Tuesday"; break; default: echo "Other day"; }

10. PHP Loops

  1. while loop

 
$i = 0; while($i < 5){ echo $i; $i++; }
  1. do-while loop

 
$i = 0; do { echo $i; $i++; } while($i < 5);
  1. for loop

 
for($i=0; $i<5; $i++){ echo $i; }
  1. foreach loop (for arrays)

 
$fruits = array("Apple","Banana","Orange"); foreach($fruits as $fruit){ echo $fruit; }

11. PHP Functions

  • Reusable block of code.

 
function greet($name){ return "Hello " . $name; } echo greet("Dinesh");
  • Default parameters:

 
function greet($name="Guest"){ return "Hello ".$name; }

12. PHP Superglobals

  • Predefined variables available in all scopes:

  1. $_GET – Data sent via URL

  2. $_POST – Data sent via form

  3. $_REQUEST – Combination of GET and POST

  4. $_SESSION – Session variables

  5. $_COOKIE – Cookies

  6. $_SERVER – Server info

  7. $_FILES – File upload info


13. PHP Forms

  • HTML Form

 
<form method="post" action="process.php"> Name: <input type="text" name="name"> <input type="submit" value="Submit"> </form>
  • Process in PHP

 
$name = $_POST['name']; echo "Hello ".$name;

14. PHP File Handling

  • Open file

 
$file = fopen("test.txt","r");
  • Read file

 
echo fread($file, filesize("test.txt"));
  • Write file

 
$file = fopen("test.txt","w"); fwrite($file,"Hello World"); fclose($file);

15. PHP Sessions & Cookies

  1. Session

 
session_start(); $_SESSION['user']="Dinesh";
  1. Cookie

 
setcookie("user","Dinesh",time()+3600); // 1 hour

16. PHP Object-Oriented Programming (OOP)

  • Class & Object

 
class Car { public $color; function setColor($c){ $this->color=$c; } function getColor(){ return $this->color; } } $myCar = new Car(); $myCar->setColor("Red"); echo $myCar->getColor();
  • Inheritance

 
class Vehicle { } class Car extends Vehicle { }
  • Constructor & Destructor

 
class Car { function __construct(){ echo "Car created"; } function __destruct(){ echo "Car destroyed"; } }

17. PHP Database (MySQL)

  • Connect to MySQL

 
$conn = mysqli_connect("localhost","root","","mydb"); if(!$conn) { die("Connection failed: ".mysqli_connect_error()); }
  • Query

 
$sql = "SELECT * FROM users"; $result = mysqli_query($conn,$sql); while($row = mysqli_fetch_assoc($result)){ echo $row['name']; }

18. Important Notes

  • PHP files must have .php extension

  • Code executes on server, not client

  • Case-sensitive for variables & functions

  • Comments:

 
// single line /* multi-line */