PHP Lecture Notes

Jul 26, 2024

PHP Lecture Notes

Introduction to PHP

  • PHP: Server-side scripting language.
  • Used to build dynamic web pages.
  • Runs on a server, not in the user’s web browser.
  • Despite claims it's "dead", PHP is still used by 70% of websites.
  • Preferred for its speed, simplicity, and flexibility.
  • Often used by freelancers and small businesses.

History of PHP

  • Released in 1995.
  • Originally "Personal Home Page"; evolved to "PHP: Hypertext Preprocessor".
  • Was not originally intended as a full programming language.
  • Mascot: An elephant named "Ella".

Basics of PHP

  • How PHP Works:
    1. Browser sends request to server.
    2. PHP processes request and accesses any databases.
    3. Server sends back HTML to the browser.
  • Commonly used with MySQL, Postgres, Oracle.
  • Strongly recommended to have a good foundation in HTML.

Getting Started with PHP

  • Recommended setup: XAMPP (Apache + MySQL + PHP)
  • Text Editor: Visual Studio Code (VS Code) is popular.

Installation of XAMPP & VS Code

Setting up Your First PHP File

  • Create a folder under XAMPP's htdocs called website.
  • Create index.php as the home page. Code structure:
    <?php
    echo "I like pizza";
    ?>
    
  • Run using localhost in the browser.

PHP Code Syntax

  • PHP code structure: <?php ... ?>
  • Display text using echo "Your message";
  • Comments: // for single line, /* ... */ for multi-line.

HTML Integration

  • PHP files can contain HTML, CSS, and JavaScript.
  • To generate HTML, type ! + tab in VS Code.

Variables in PHP

  • Variables are containers for data: e.g. $name = 'SpongeBob';
  • Data types include strings, integers, floats, booleans.
  • When declaring a variable, always start with a $.

Basic Operations

  • Arithmetic operations: +, -, *, /, % (modulus).
  • Increment: ++, Decrement: --.

Arrays in PHP

  • Arrays store multiple values:
    $fruits = array("apple", "banana", "cherry");
    
  • Accessing array elements: $fruits[0] returns "apple".
  • Associative arrays use key-value pairs:
    $colors = array("red" => "#FF0000", "green" => "#00FF00");
    

Functions in PHP

  • Functions are reusable code blocks:
    function add($a, $b) {
        return $a + $b;
    }
    
  • Functions can take parameters and return values.

Including Files

  • Use include to add code from external files. This promotes reusability:
    include 'header.php';
    include 'footer.php';
    

Sessions and Cookies

  • Cookies: Store user information in the browser.
  • Sessions: Store user data on the server for use across multiple pages.
  • Use session_start() to begin a session.

Database Connection & MySQL

  • Use PHP to connect to MySQL using mysqli_connect().
  • Perform SQL operations inside PHP scripts.
  • Handle exceptions with try and catch blocks.

SQL Queries

  • Inserting and retrieving data from database:
    $sql = "INSERT INTO users (username, password) VALUES ('$username', '$hash')";
    
  • Use methods like mysqli_query() to execute SQL queries.

Practical Exercises and Conclusion

  • Create a registration form to insert user data in a database.
  • Validate and sanitize user input to avoid security risks.

Summary

  • PHP is a powerful tool for web development.
  • Strong understanding of variables, functions, arrays, and SQL is essential for creating dynamic web applications.

Next Steps

  • Continue practicing PHP and SQL integration through hands-on exercises.