
这是一份全面的 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";
?>
<?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);
?>
<?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
}
?>
<?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中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号