"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > PHP cheat sheet covering essential syntax and functions

PHP cheat sheet covering essential syntax and functions

Published on 2024-08-07
Browse:615

PHP cheat sheet covering essential syntax and functions

Here's a comprehensive PHP cheat sheet covering essential syntax and functions:

Basics


Data Types

  • String: "Hello, World!"
  • Integer: 123
  • Float: 12.34
  • Boolean: true or false
  • Array: ["apple", "banana", "cherry"]
  • Object
  • NULL

Strings


Arrays

 "value1", "key2" => "value2"];

// Array functions
count($array);                  // Count elements
array_push($array, 4);          // Add an element
array_merge($array, [4, 5]);    // Merge arrays
in_array(2, $array);            // Check if element exists
?>

Control Structures

If-Else


Switch


Loops


Functions


Superglobals

  • $_GET – Variables sent via URL parameters
  • $_POST – Variables sent via HTTP POST
  • $_REQUEST – Variables sent via both GET and POST
  • $_SERVER – Server and execution environment information
  • $_SESSION – Session variables
  • $_COOKIE – HTTP Cookies

File Handling


Error Handling

getMessage();
} finally {
    // Code to always execute
}
?>

Database (MySQLi)

connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Select data
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
"; } } else { echo "0 results"; } $conn->close(); ?>

Session Management


Include & Require


This cheat sheet covers the fundamental concepts and commonly used features in PHP. Let me know if you need more details on any specific topic!

Release Statement This article is reproduced at: https://dev.to/devabdul/php-cheat-sheet-covering-essential-syntax-and-functions-33e1?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3