PHP 笔记(一)
数据库项目作业要团队开发 web,选择了尝试后端,又向做出自己的网站这个目标踏出了一步。
1. 简单使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <!DOCTYPE html> <html> <body>
<h1>h1</h1>
<?php echo "Hello World!"; ?>
<h1>h1</h1>
</body> </html>
|
在服务器下运行才有效果
2. 变量
1 2 3 4 5 6
| <?php $x = 1; $y = 1; $z = $x + $y; echo $z; ?>
|
2.1 变量作用域
PHP 有四种作用域
- local
- global
- static
- parameter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <?php $x = 5; function fn() { $y = 10; echo "<p>函数内: </p>"; echo "<br>"; echo "变量y: $y"; }
fn(); echo "<p>函数外: </p>"; echo "变量x: $x";
?>
|
global 关键字:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <?php $x = 1; $y = 1; function test() { global $x, $y; $y = $x + $y; }
test(); echo "变量y: $y"; ?>
<?php $x = 1; $y = 2; function test() { $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y']; }
test(); echo "变量y: $y"; ?>
|
static 关键字:
当一个函数完成时,它的所有变量通常都会被删除,在第一次声明变量时使用static 关键字可以实现让特定变量不被删除。然后,每次调用该函数时,该变量都会保留函数前一次被调用时的值。该变量还是函数的局部变量
1 2 3 4 5 6 7 8 9 10 11
| <?php function test() { static $x = 0; echo $x; $x++; echo "<br>"; } test(); test(); test(); ?>
|
3. 定界符 EOF
1 2 3 4 5 6 7 8 9 10
| <?php $name = "clz"; echo <<<EOF <h1>测试</h1> // html格式会被解析 "abg\n"$name // 双引号也会被解析,双引号内会保留转义符的转义效果 "123" EOF; echo "Hello World!"; ?>
|
4. 数据类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <?php $x = 123; var_dump($x); $x = -234; var_dump($x);
$x = 017; var_dump($x);
$x = 0xaa; var_dump($x);
$x = "test"; var_dump($x);
$x = 1.234; var_dump($x); $x = 2.4e3; var_dump($x);
$x = true; var_dump($x); ?>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <?php $arr = array(1, "Hello", true); var_dump($arr); var_dump($arr[0]);
class Car { var $color; function __construct($color="green") { $this->color = $color; } function what_color() { return $this->color; } }
$c = new Car("white"); var_dump($c);
$d = null; var_dump($d); ?>
|
5. 类型比较
和 JavaScript 一样,有松散比较和严格比较两种形式
- 松散比较(等于):”==”, 只比较值,不比较类型
- 严格比较(绝对等于):”===”, 既比较值,也比较类型
6. 常量
设置常量需要使用 define()函数
1
| bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )
|
- name: 必选参数,常量名称
- value: 必选参数,常量的值
- case_insensitive: 可选参数,如果是 true,则常量对大小写不敏感。默认是大小写敏感。
1 2 3 4 5 6 7 8 9 10 11
| <?php define("HW", "Hello World!"); echo HW; echo "<br>";
function test() { echo HW; }
test(); ?>
|
7. 字符串
7.1 并置运算符(.)
用于把两个字符串连接起来
1 2 3 4 5
| <?php $h = "Hello "; $w = "World"; echo $h . $w; ?>
|
7.2 strlen()函数
strlen()函数返回字符串的长度(字节数), echo strlen("中文");
会输出 6,因为一个中文占 3 个字节
1 2 3 4 5
| <?php $h = "Hello "; $w = "World"; echo strlen($h . $w); ?>
|
7.3 strpos()函数
strpos()函数用于在字符串中查找字符串,如果找到匹配,则返回第一个匹配的字符位置,如果找不到,则返回 false
1 2 3 4 5 6 7 8 9
| <?php echo strpos("Hello World!", "!"); echo "<br>";
echo strpos("Hello World!", "World"); echo "<br>";
var_dump(strpos("Hello World!", "Worll")); ?>
|
8. 关联数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?php $age = array("A" => 17, "B" => 33, "C" => 21);
echo "A is " . $age['A'] . " years old."; echo "<br>";
foreach($age as $k => $v) { echo "Key = " . $k . ", Value = " .$v; echo "<br>"; } ?>
|
9. 支持可变参数的函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <?php echo "<pre>"; function test(...$args) { $num = count($args); echo "函数调用参数个数: " . $num . PHP_EOL;
echo "函数参数详情: ";
foreach($args as $args) { echo $args . " "; } echo PHP_EOL;
} test("a"); test("a", "b"); test("a", "b", "c"); ?>
|
10. 数据库
10.1 连接数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?php $servername = "localhost"; $username = "root"; $password = "密码";
$conn = new mysqli($servername, $username, $password);
if($conn->connect_error) { die("连接失败: " . $conn->connect_error); }
echo "连接成功";
$conn->close(); ?>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?php $servername = "localhost"; $username = "root"; $password = "root1234";
$conn = mysqli_connect($servername, $username, $password);
if(!$conn) { die("连接失败: " . mysqli_connect_error()); } echo "连接成功";
mysqli_close($conn); ?>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <?php $servername = "localhost"; $username = "root"; $password = "root1234";
try { $conn = new PDO("mysql:host=$servername;", $username, $password); echo "连接成功"; } catch(PDOException $e) { echo $e->getMessage(); }
$conn = null; ?>
|
10.2 创建数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <?php $servername = "localhost"; $username = "root"; $password = "root1234";
$conn = new mysqli($servername, $username, $password); if($conn->connect_error) { die("连接失败: " . $conn->connect_error); }
$sql = "create database php_db"; if($conn->query($sql) === true) { echo "数据库创建成功"; } else { echo "数据库创建失败: " . $conn->error; }
$conn->close(); ?>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <?php $servername = "localhost"; $username = "root"; $password = "root1234";
$conn = mysqli_connect($servername, $username, $password); if(!$conn) { die("连接失败: " . mysqli_connect_error()); }
$sql = "create database php_db_1"; if(mysqli_query($conn, $sql)) { echo "数据库创建成功"; } else { echo "数据库创建失败: " . mysqli_error($conn); }
mysqli_close($conn); ?>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <?php $servername = "localhost"; $username = "root"; $password = "root1234";
try { $conn = new PDO("mysql:host=$servername", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "create database php_db_2";
$conn->exec($sql); echo "数据库创建成功"; } catch(PDOException $e) { echo $sql . " " . $e->getMessage(); }
$conn = null; ?>
|
10.3 创建表
MySQLi 面向对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <?php $servername = "localhost"; $username = "root"; $password = "root1234"; $dbname = "php_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error) { die("连接失败: " . $conn->connect_error); }
$sql = "create table guests_1( id int(6) unsigned auto_increment primary key, firname varchar(30) not null, lastname varchar(30) not null, email varchar(50), reg_date timestamp )";
if($conn->query($sql) === true) { echo "创建表成功"; } else { echo "创建表失败: " . $conn->error; }
$conn->close(); ?>
|
其他操作,基本类似,具体实现可查PHP 教程 | 菜鸟教程 (runoob.com)
参考:PHP 教程 | 菜鸟教程 (runoob.com)