PHP – PERSONAL HOME PAGES
By,
                       B Tejasree( 23d3067 )
                     Chandana KR( 23d3070 )
                  Finamol Willson ( 23d3081 )
                    Madhumitha M (23d3092 )
                                  WHAT IS PHP?
•   It is a popular open source, server-side scripting language used primarily for creating dynamic
    web content and web applications. It is designed to work seamlessly with databases and can
    be embedded into HTML code to create interactive elements on web pages.
•   Originally, PHP stood for Personal Home Page when it was first created by Rasmus Lerdorf in
    1994.But over time, as it evolved into a full-fledged programming language, the official meaning
    was changed to PHP :Hypertext Preprocessor.
                                  HOW PHP WORKS
PHP works by processing code on the server to generate HTML, which is then sent to the user’s browser. It
helps create dynamic websites that can respond to user actions.
Steps involved are :
Step 1 :A user requests a PHP web page from the server.
E.g., The browser sends a request like www.example.com/page.php.
Step 2 :Server receives the request and passes it to the PHP interpreter.
E.g., Apache or Nginx sends the PHP file to be processed.
Step 3 :PHP interpreter reads and executes the code.
E.g., It starts running the PHP code inside the file.
Step 4 :The code may connect to a database or other resources.
E.g., fetch data from MySQL to display.
Step 5 :Interpreter generates dynamic HTML output.
E.g., The result of PHP code is HTML.
Step 6 :Server sends the HTML output to the user’s browser.
E.g., Only the final HTML is seen by the user, not PHP code.
Step 7 :Browser displays the final web page.
E.g., User sees the content on the screen.
     PHP SYNTAX – RULES AND CONVENTIONS
1.   Statements are terminated by semicolons.
     Every PHP instruction ends with ;
     Example: echo "Hi";
2.   Space Insensitivity
      Extra spaces and tabs are ignored by PHP.
     Example: $x=5; $y = 10;
3.   Case Insensitivity (for keywords and functions)
     PHP treats keywords and functions without case sensitivity.
      Example: ECHO "Hello"; echo "Hi";
4.   Variable names are case sensitive.
      $Name and $name are treated as different variables.
      Example: $name = "Shadow"; echo $Name; // Error
5.   Comments are used to explain code.
     Use //, #, or /* */ to write comments.
     Example: // This is a comment
6.   Code blocks are enclosed in curly braces {}.
     Used to group multiple lines in conditions, loops, etc.
     Example:
      VARIABLES IN PHP                                    CONSTANTS IN PHP
1.   Variable Declaration                              1. Defining Constants
     Declaring a variable begins with $ sign.             Use define() to set a constant that doesn't
     Example: $age;                                       change.
2.   Naming Conventions                                    Example: define("PI", 3.14);
      Must start with a letter or _, and contain no
     spaces or symbols.                                2. Accessing Constants
      Example: $user_name = "Shadow";                     Use the constant name directly (no $).
                                                          Example: echo PI;
3.   Variable Assignment
     Use = to assign values to variables.              3. Naming Conventions
     Example: $x = 25;                                    Usually written in uppercase, with
4.   Data Type Conversion                                 underscores       for clarity.
      PHP automatically converts types based on           Example: define("MAX_VALUE", 100);
     usage.
      Example: $a = "5"; $sum = $a + 2; // Output: 7
5.   Variable Reuse
     Variables can be updated or reused later.
     Example: $x = 10; $x = $x + 5; // $x becomes 15
                          DataTypes in PHP
PHP supports several data types, which can be categorized into scalar,
compound, special, and pseudo types. Here's a breakdown of the main
types:
Scalar Types:
• Integer
   Whole numbers (e.g., 1, -42, 0)
• Float (Double)
   Decimal numbers (e.g., 3.14, -0.5, 2.0E+3)
• String
   Text values (e.g., "Hello, world!", 'PHP')
• Boolean
   Only true or false
Compound Types
Ø Array
  A collection of values (e.g., ["apple", "banana", "cherry"] or associative arrays like ["name" =>
"John"])
Ø Object
   Instances of user-defined classes
Special Types
Ø NULL
 Represents a variable with no value
Ø Resource
    A special variable holding a reference to an external resource (like a database connection or
file handle)
                                         Strings in PHP
1. Creating Strings
                                                     3. String Length
Ø Single-quoted strings                                Using strlen()
  It is denoted by using (‘ ‘) quotes.                 EX::$str = "PHP is fun";
                                                            echo strlen($str); // Output: 10
 EX::$name = 'John';
     echo 'Hello, $name'; // Output: Hello, $name
Ø Double-quoted strings
  It is denoted by using (“ “) quotes.
  EX::$name = "John";
       echo "Hello, $name"; // Output: Hello, John
2. String Concatenation                              4. String Manipulation
                                                      EX:: echo strtoupper("php"); // Output: PHP
  Uses the dot (.) operator to concatenate strings
                                                            echo strtolower("PHP"); // Output: php
  EX::$first = "Hello";
      $second = "World";
      $message = $first . " " . $second;
echo $message; // Output: Hello World
                                     Arrays in PHP
1. Indexed Arrays (Numerical Index)                   3. Multidimensional Arrays
 These arrays use numeric indexes starting from 0.     These are arrays within arrays.
                                                       EX::$contacts = array(
  EX::$fruits = array("Apple", "Banana", "Cherry");         array("name" => "Alice", "phone" => "1234"),
       echo $fruits[0]; // Output: Apple                    array("name" => "Peter", "phone" => "5678")
                                                            );
                                                            echo $contacts[1]["phone"]; // Output: 5678
2. Associative Arrays (Key-Value Pairs)
  These arrays use named keys instead of numbers.
  EX::$person = array(
     "name" => "John",
     "age" => 30,
     "city" => "New York"
     );
     echo $person["name"]; // Output: John
                              FUNCTIONS IN PHP
• A function in PHP is a self-contained block of code that performs a specific task. It can accept inputs
  (parameters), execute a set of statements, and optionally return a value.
• PHP functions allow code reusability by encapsulating a block of code to perform specific tasks.
• Functions can accept parameters and return values, enabling dynamic behavior based on inputs.
• PHP supports both built-in functions and user-defined functions, enhancing flexibility and modularity
  in code.
• Example
function greet($name) {
    return "Hello, $name!";
}
echo greet("Alice"); // Output: Hello, Alice!
                       Control Structures in PHP
• PHP provides several constructs for decision-making, including if, else, elseif, and switch. These
  control structures can be used to make logical decisions in a program.
If ,else,elif Statements
if: Executes a block of code only if a specified condition is true.
else: Executes a block of code if the if condition is false.
elseif: Checks another condition if the previous if or elseif condition was false.
Example
$age = 20;
if ($age >= 18) {
    echo "Adult";
} elseif ($age >= 13) {
    echo "Teenager";
} else {
    echo "Child";
}
} else {
    echo "Child";
}
Loops
• Used for multiple conditions based on a single value.
Example
$day = "Monday";
switch ($day) {
    case "Monday":
       echo "Start of the week";
       break;
    case "Friday":
       echo "Almost weekend!";
       break;
    default:
       echo "Another day";
}
while Loop                      Control Keywords
$i = 1;                         break: Exits a loop or switch
while ($i <= 5) {               continue: Skips to the next loop iteration
   echo $i;                     return: Exits a function and optionally returns a value
   $i++;
}                               Example
 do...while Loop                for ($i = 1; $i <= 10; $i++) {
$i = 1;                            if ($i == 3) continue; // skip 3
do {                               if ($i == 7) break; // stop at 7
   echo $i;                        echo $i;
   $i++;                        }
} while ($i <= 5);
for Loop
for ($i = 1; $i <= 5; $i++) {
   echo $i;
}
            PERSONAL HOME PAGES-OPERATORS
1. Arithmetic Operators:                       2. Assignment Operators:
Arithmetic operators are used to perform       Assignment operators are used to assign
basic mathematical operations like addition,   values to variables and can also perform
subtraction, multiplication, division, etc.    arithmetic operations while assigning.
                                                Operator   Description      Example
Operator   Description      Example
                                                =          Assign value     $x = 10
+          Addition         10 + 5              +=         Add and assign   $x += 5
-          Subtraction      10 - 3              -=         Subtract and     $x -= 3
                                                           assign
*          Multiplication   4*2
                                                *=         Multiply and     $x *= 2
/          Division         10 / 2                         assign
                                                /=         Divide and       $x /= 4
%          Modulus          10 % 3                         assign
**         Exponentiation 2 ** 3                %=         Modulus and      $x %= 3
                                                           assign
3. Comparison Operators:                    4. Logical Operators:
Comparison operators are used to compare    Logical operators are used to combine
two values and return true or false.        conditional statements.
 Operator   Description           Example   Operator   Description   Example
 =          Assign value          $x = 10   &&         Logical AND   (5 > 3 && 2 < 4)
 +=         Add and assign        $x += 5   ||         Logical OR    (5 > 3 || 2 > 4)
 -=         Subtract and assign   $x -= 3   !          Logical NOT   !(5 < 3)
 *=         Multiply and assign   $x *= 2
 /=         Divide and assign     $x /= 4
 %=         Modulus and assign    $x %= 3
    5. String Operators:                             6. Array Operators:
    String operators are used to manipulate and      Array operators are used to compare or
    combine string values in PHP. These operators    combine arrays in PHP. They help in
    allow you to concatenate strings or append       operations like union, equality checks,
    one string to another                            and identity checks between arrays.
Operator      Description       Example              Operator    Description     Example
                                                     +           Union           $a + $b
.             Concatenation     "Hello " . "World"
                                                     ==          Equal           $a == $b
.=            Concatenate and   $a .= "World";
              assign                                 ===         Identical       $a === $b
                                                     !=          Not equal       $a != $b
                                                     <>          Not equal       $a <> $b
                                                     !==         Not identical   $a !== $b
                   Example code:
<?php
$a = 10; $b = 5;
                                                              Output:
// Arithmetic
echo "Add: " . ($a + $b) . "<br>";
// Assignment                                            Add: 15
$a += 2;                                                 After += : 12
echo "After += : $a<br>";
                                                         Equal: No
// Comparison
echo "Equal: " . ($a == $b ? "Yes" : "No") . "<br>";     After ++ : 6
// Increment                                             Logical AND: True
$b++;                                                    String: Hi PHP
echo "After ++ : $b<br>";
// Logical
echo "Logical AND: " . (($a > 5 && $b < 10) ? "True" :
"False") . "<br>";
// String
$x = "Hi "; $y = "PHP";
echo "String: " . $x . $y . "<br>";
              Advantages of PHP:                                    Disadvantages of PHP:
Free and Open Source:                                   Security Vulnerabilities:
PHP is available for free and has strong community      If not coded properly, PHP applications are prone to
support, making it accessible and well-documented.      common web attacks like SQL injection or XSS.
Easy to Learn and Use:                                  Not Suitable for Large Projects:
PHP has a simple syntax similar to C, which makes it    Maintaining structure and scalability in large
easy for beginners to learn and start building web      applications is difficult without a strict framework or
applications.                                           coding discipline.
Cross-Platform Support:                                 Performance Limitations:
PHP runs on different operating systems like Windows,   Compared to newer technologies like Node.js, PHP
Linux, and macOS, and works with most web servers.      can be slower, especially in handling real-time
                                                        applications.
Strong Database Integration:
PHP easily connects with databases like MySQL and       Inconsistent Function Naming:
PostgreSQL, making it ideal for dynamic websites.       PHP’s built-in functions lack a consistent naming
                                                        pattern, which can confuse developers and reduce
                                                        code readability.
THANK YOU