登录
首页 >  文章 >  php教程

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

来源:dev.to

时间:2024-07-15 11:09:41 235浏览 收藏

欢迎各位小伙伴来到golang学习网,相聚于此都是缘哈哈哈!今天我给大家带来《PHP 备忘单涵盖基本语法和函数》,这篇文章主要讲到等等知识,如果你对文章相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!

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

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于文章的相关知识,也可关注golang学习网公众号。

声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>