# PHP Cheat Sheet ## Table of Contents 1. [Variables](#variables) 2. [Data Types](#data-types) 3. [Operators](#operators) 4. [Control Structures](#control-structures) 5. [Functions](#functions) 6. [Arrays](#arrays) 7. [Strings](#strings) 8. [File Handling](#file-handling) 9. [Error Handling](#error-handling) 10. [Classes and Objects](#classes-and-objects) 11. [Sessions and Cookies](#sessions-and-cookies) 12. [Database Connectivity](#database-connectivity) 13. [Regular Expressions](#regular-expressions) ## Variables ### Declaration and Assignment ```php ``` ### Variable Types - String: `$string_variable = "Hello";` - Integer: `$int_variable = 42;` - Float: `$float_variable = 3.14;` - Boolean: `$bool_variable = true;` ## Data Types ### Scalar Types - String - Integer - Float (double) - Boolean ### Compound Types - Array - Object ### Special Types - Resource - NULL ## Operators ### Arithmetic Operators ```php +, -, *, /, %, ** (exponentiation) ``` ### Comparison Operators ```php ==, ===, !=, !==, <, >, <=, >= ``` ### Logical Operators ```php && (and), || (or), ! (not) ``` ### Assignment Operators ```php =, +=, -=, *=, /=, %= ``` ## Control Structures ### If Statement ```php if (condition) { // code to be executed if the condition is true } elseif (another_condition) { // code to be executed if the previous condition was false and this one is true } else { // code to be executed if all conditions are false } ``` ### Switch Statement ```php switch (variable) { case value1: // code to be executed if variable equals value1 break; case value2: // code to be executed if variable equals value2 break; default: // code to be executed if variable doesn't match any case } ``` ### Loops #### For Loop ```php for ($i = 0; $i < 5; $i++) { // code to be executed in each iteration } ``` #### While Loop ```php while (condition) { // code to be executed as long as the condition is true } ``` #### Do-While Loop ```php do { // code to be executed at least once, then repeated as long as the condition is true } while (condition); ``` ## Functions ### Function Declaration ```php function functionName($param1, $param2) { // code to be executed return $result; // optional } ``` ### Function Call ```php $result = functionName($arg1, $arg2); ``` ## Arrays ### Indexed Array ```php $indexed_array = array("value1", "value2", "value3"); ``` ### Associative Array ```php $assoc_array = array("key1" => "value1", "key2" => "value2"); ``` ### Multidimensional Array ```php $multi_array = array( array("value1", "value2"), array("value3", "value4") ); ``` ## Strings ### Concatenation ```php $string1 = "Hello"; $string2 = "World"; $concatenated = $string1 . " " . $string2; ``` ### String Functions ```php strlen($string); // length of the string strpos($string, "find"); // position of the first occurrence of "find" substr($string, start, length); // extract a portion of the string ``` ## File Handling ### Reading from a File ```php $file_content = file_get_contents("filename.txt"); ``` ### Writing to a File ```php file_put_contents("filename.txt", "Hello, World!"); ``` ## Error Handling ### Try-Catch Block ```php try { // code that may throw an exception } catch (Exception $e) { // handle the exception echo "Error: " . $e->getMessage(); } ``` ## Classes and Objects ### Class Declaration ```php class MyClass { // properties and methods } ``` ### Object Instantiation ```php $object = new MyClass(); ``` ## Sessions and Cookies ### Sessions ```php session_start(); $_SESSION['username'] = 'John'; ``` ### Cookies ```php setcookie('user', 'Jane', time() + 3600, '/'); ``` ## Database Connectivity ### MySQLi Connection ```php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "database"; $conn = new mysqli($servername, $username, $password, $dbname); ``` ### MySQLi Query ```php $sql = "SELECT * FROM table"; $result = $conn->query($sql); ``` ## Regular Expressions ### Pattern Matching ```php $pattern = "/[0-9]+/"; $string = "There are 123 apples"; preg_match($pattern, $string, $matches); ``` ``` # PHP Extended Examples ## Variables ### Variable Scope ```php ``` ## Control Structures ### Foreach Loop with Arrays ```php ``` ### Switch Statement with Fall-Through ```php ``` ## Functions ### Default Parameter Values ```php ``` ### Variable Number of Arguments ```php ``` ## Arrays ### Array Functions ```php 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 ) ?> ``` ## Strings ### Regular Expression Match and Replace ```php ``` ## File Handling ### Reading and Writing to Files ```php ``` ## Classes and Objects ### Class Inheritance ```php name = $name; } public function makeSound() { echo "Generic animal sound\n"; } } class Dog extends Animal { public function makeSound() { echo "Woof!\n"; } } $dog = new Dog("Buddy"); echo $dog->name; // Outputs: Buddy $dog->makeSound(); // Outputs: Woof! ?> ``` ## Database Connectivity ### MySQLi Prepared Statements ```php connect_error) { die("Connection failed: " . $conn->connect_error); } // Prepared statement $sql = "SELECT * FROM users WHERE username = ?"; $stmt = $conn->prepare($sql); $username = "john_doe"; $stmt->bind_param("s", $username); $stmt->execute(); // Fetch result $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { print_r($row); } // Close connection $stmt->close(); $conn->close(); ?> ``` ```