首页 >后端开发 >php教程 >PHP 备忘单涵盖基本语法和函数

PHP 备忘单涵盖基本语法和函数

WBOY
WBOY原创
2024-07-17 09:17:40470浏览

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