Rumah  >  Artikel  >  pembangunan bahagian belakang  >  Lembaran cheat PHP yang merangkumi sintaks dan fungsi penting

Lembaran cheat PHP yang merangkumi sintaks dan fungsi penting

WBOY
WBOYasal
2024-07-17 09:17:40439semak imbas

PHP cheat sheet covering essential syntax and functions

Berikut ialah helaian cheat PHP komprehensif yang merangkumi sintaks dan fungsi penting:

Asas

<?php
// Single-line comment

/*
  Multi-line comment
*/

// Variables
$variable_name = "Value";  // String
$number = 123;             // Integer
$float = 12.34;            // Float
$boolean = true;           // Boolean
$array = [1, 2, 3];        // Array

// Constants
define("CONSTANT_NAME", "Value");
const ANOTHER_CONSTANT = "Value";
?>

Jenis Data

  • String: "Hello, World!"
  • Integer: 123
  • Apung: 12.34
  • Boolean: benar atau salah
  • Susun: ["epal", "pisang", "ceri"]
  • Objek
  • NULL

rentetan

<?php
$str = "Hello";
$str2 = 'World';
$combined = $str . " " . $str2; // Concatenation

// String functions
strlen($str);            // Length of a string
strpos($str, "e");       // Position of first occurrence
str_replace("e", "a", $str); // Replace all occurrences
?>

Tatasusunan

<?php
$array = [1, 2, 3];
$assoc_array = ["key1" => "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
?>

Struktur Kawalan

Jika-Lain

<?php
if ($condition) {
    // code to execute if true
} elseif ($another_condition) {
    // code to execute if another condition is true
} else {
    // code to execute if all conditions are false
}
?>

Tukar

<?php
switch ($variable) {
    case "value1":
        // code to execute if variable equals value1
        break;
    case "value2":
        // code to execute if variable equals value2
        break;
    default:
        // code to execute if no case matches
}
?>

gelung

<?php
// For loop
for ($i = 0; $i < 10; $i++) {
    // code to execute
}

// While loop
while ($condition) {
    // code to execute
}

// Do-While loop
do {
    // code to execute
} while ($condition);

// Foreach loop
foreach ($array as $value) {
    // code to execute
}
?>

Fungsi

<?php
function functionName($param1, $param2) {
    // code to execute
    return $result;
}

$result = functionName($arg1, $arg2);
?>

Superglobals

  • $_GET – Pembolehubah dihantar melalui parameter URL
  • $_POST – Pembolehubah dihantar melalui HTTP POST
  • $_REQUEST – Pembolehubah dihantar melalui GET dan POST
  • $_SERVER – Pelayan dan maklumat persekitaran pelaksanaan
  • $_SESSION – Pembolehubah sesi
  • $_COOKIE – Kuki HTTP

Pengendalian Fail

<?php
// Reading a file
$file = fopen("filename.txt", "r");
$content = fread($file, filesize("filename.txt"));
fclose($file);

// Writing to a file
$file = fopen("filename.txt", "w");
fwrite($file, "Hello, World!");
fclose($file);
?>

Pengendalian Ralat

<?php
try {
    // Code that may throw an exception
    if ($condition) {
        throw new Exception("Error message");
    }
} catch (Exception $e) {
    // Code to handle the exception
    echo "Caught exception: " . $e->getMessage();
} finally {
    // Code to always execute
}
?>

Pangkalan Data (MySQLi)

<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->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"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>

Pengurusan Sesi

<?php
// Start session
session_start();

// Set session variables
$_SESSION["username"] = "JohnDoe";
$_SESSION["email"] = "john@example.com";

// Get session variables
echo $_SESSION["username"];

// Destroy session
session_destroy();
?>

Sertakan & Perlukan

<?php
include 'filename.php';  // Includes file, gives a warning if not found
require 'filename.php';  // Includes file, gives a fatal error if not found

include_once 'filename.php';  // Includes file once, checks if already included
require_once 'filename.php';  // Requires file once, checks if already included
?>

Helaian cheat ini merangkumi konsep asas dan ciri yang biasa digunakan dalam PHP. Beritahu saya jika anda memerlukan butiran lanjut tentang mana-mana topik tertentu!

Atas ialah kandungan terperinci Lembaran cheat PHP yang merangkumi sintaks dan fungsi penting. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:Botol AirArtikel seterusnya:Botol Air