PHP 备忘单涵盖基本语法和函数
 admin 阅读:196 2024-07-17 
                    
这是一份全面的 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 中的基本概念和常用功能。如果您需要有关任何特定主题的更多详细信息,请告诉我!
声明
                        
                        1、部分文章来源于网络,仅作为参考。 2、如果网站中图片和文字侵犯了您的版权,请联系1943759704@qq.com处理!



