Certainly! Here’s a basic PHP cheat sheet that covers some common syntax and functions:

Basic Syntax:

<?php
// PHP code goes here
?>

Variables

$name = "John";
$age = 25;
$isStudent = true;

Data Types

  • String: "Hello"
  • Integer: 123
  • Float: 3.14
  • Boolean: true or false
  • Array: $colors = array("red", "green", "blue");
  • Null: $variable = null;

Operators:

  • Arithmetic: +, -, *, /, %
  • Comparison: ==, !=, <, >, <=, >=
  • Logical: &&, ||, !

Conditional Statements:

if ($condition) {
    // Code to execute if the condition is true
} elseif ($anotherCondition) {
    // Code to execute if the first condition is false and this one is true
} else {
    // Code to execute if all conditions are false
}

Loops:

For Loop:

for ($i = 0; $i < 5; $i++) {
    // Code to repeat
}

While Loop:

while ($condition) {
    // Code to repeat as long as the condition is true
}

Foreach Loop:

$colors = array("red", "green", "blue");
foreach ($colors as $color) {
    // Code to repeat for each element in the array
}

Functions

function greet($name) {
    return "Hello, $name!";
}

echo greet("John"); // Output: Hello, John!

Arrays

$fruits = array("apple", "orange", "banana");
echo $fruits[0]; // Output: apple

// Associative Array
$person = array("name" => "John", "age" => 25);
echo $person["name"]; // Output: John

Super Globals:

$_GET['parameter'];    // Retrieve data from the query string
$_POST['parameter'];   // Retrieve data from a form submission
$_SESSION['variable']; // Access session data

File Handling:

$file = fopen("example.txt", "r");
$content = fread($file, filesize("example.txt"));
fclose($file);

Error Handling:

try {
    // Code that may throw an exception
} catch (Exception $e) {
    // Code to handle the exception
}

This is a basic overview, and PHP has many more features and functions. It’s recommended to refer to the official PHP documentation for more in-depth information and updates.

Tagged in: