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

王林
发布: 2024-07-15 12:10:23
转载
728人浏览过

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

这是一份全面的 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 中的基本概念和常用功能。如果您需要有关任何特定主题的更多详细信息,请告诉我!

法语写作助手
法语写作助手

法语助手旗下的AI智能写作平台,支持语法、拼写自动纠错,一键改写、润色你的法语作文。

法语写作助手 31
查看详情 法语写作助手

以上就是PHP 备忘单涵盖基本语法和函数的详细内容,更多请关注php中文网其它相关文章!

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号