PHP 备忘单涵盖基本语法和函数
来源:dev.to
时间:2024-07-15 11:09:41 235浏览 收藏
欢迎各位小伙伴来到golang学习网,相聚于此都是缘哈哈哈!今天我给大家带来《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删除
相关阅读
更多>
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
136 收藏
-
393 收藏
-
448 收藏
-
250 收藏
-
243 收藏
-
407 收藏
-
321 收藏
-
341 收藏
-
428 收藏
-
121 收藏
-
256 收藏
-
358 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习