首頁  >  文章  >  後端開發  >  PHP 備忘單涵蓋基本語法和函數

PHP 備忘單涵蓋基本語法和函數

WBOY
WBOY原創
2024-07-17 09:17:40439瀏覽

PHP cheat sheet covering essential syntax and functions

這是一個全面的 PHP 備忘單,涵蓋基本語法和函數:

基礎知識

<?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";
?>

資料類型

  • 字串:“你好,世界!”
  • 整數:123
  • 浮動:12.34
  • 布林值:真或假
  • 陣列:[“蘋果”,“香蕉”,“櫻桃”]
  • 對象
  • NULL

弦樂

<?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
?>

陣列

<?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
?>

控制結構

如果-否則

<?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
}
?>

轉變

<?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
}
?>

循環

<?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
}
?>

功能

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

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

超全域變數

  • $_GET – 透過 URL 參數發送的變數
  • $_POST – 透過 HTTP POST 傳送的變數
  • $_REQUEST – 透過 GET 和 POST 發送的變數
  • $_SERVER – 伺服器和執行環境資訊
  • $_SESSION – 會話變數
  • $_COOKIE – HTTP Cookie

文件處理

<?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);
?>

錯誤處理

<?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
}
?>

資料庫(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();
?>

會話管理

<?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();
?>

包含和要求

<?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
?>

本備忘單涵蓋了 PHP 中的基本概念和常用功能。如果您需要有關任何特定主題的更多詳細信息,請告訴我!

以上是PHP 備忘單涵蓋基本語法和函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:水瓶下一篇:水瓶