php

前提须知

  1. php是弱类型的脚本语言[1]

  2. 虽然在html里面使用php但是前端通过开发者工具是看不到的

    image-20231011115354493

  3. 使用echo输出

    1
    echo "<h1>标题</h1>"

变量

由于是弱语言所以我们只需要$进行操作就行了

  1. 使用16进制

    1
    $hex=0x16
  2. 使用判断

    ===

字符串类型

  1. 拼接符号

    1
    2
    3
    $c="hhh";
    $d="dada";
    echo $c.$d
  2. 部分字符串处理的函数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    //计算字符串的长度
    echo "<br>";
    echo strlen($c.$d);
    //反转字符串
    echo "<br>";
    echo strrev($c.$d);
    //从查找strpos()
    echo strpos($c,"h");
    //替换
    echo "<br>";
    echo str_replace("h","e",$c)
  3. 其他的函数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    //计算字符串的长度
    echo "<br>";
    echo strlen($c.$d);
    //反转字符串
    echo "<br>";
    echo strrev($c.$d);
    //从查找strpos()
    echo strpos($c,"h");
    //替换
    echo "<br>";
    echo str_replace("h","e",$c)."<br>";
    //ceil 向上取整
    echo ceil(3.4)."<br>";
    //向下取整
    echo floor(4.2)."<br>";
    //取随机整数
    echo rand(2,10);
  4. 从前端获取输入

    我们需要使用htmlform表单形式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <form action="" method="get">
    <label for="id:"></label>
    <input type="number" name="id"><br>

    <label for="passwd:"></label>
    <input type="number" name="passwd"><br>

    <input type="submit" name="submit">
    </form>
    <?php
    echo $_GET['id']."<br>";
    echo $_GET['passwd'];
    ?>

    使用_GET方法获取,因为表单的提交方法是get

    计算机:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <!-- 计算器 -->
    <form action="" method="get">
    input num1:
    <input type="number" name="num1"><br>

    input num1:
    <input type="number" name="num2"><br>

    <input type="submit">
    </form>

    <?php
    echo "result is:";
    echo $_GET['num1']+$_GET['num2'];
    ?>

数组

创建数组&输出特定的元素

1
2
3
4
5
6
7
8
9
//第一种方式
$arr=[1,2,3.4,"chg"];
echo $arr."<br>";
echo $arr[3]."<br>";


//第二种方式
$arr2=array(1,3.4,"zjy");
echo $arr2;

当直接输出数组的名称的时候,就会输出类型

增加元素

1
$arr[]="zjy";

使用数组接受

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<form action="" method="get">
<select multiple="multiple" name="fruits[]">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<input type="submit" name="submit">
</form>



<?php
$fruits=$_GET['fruits'];
print_r($fruits);
?>

文件包含

  1. 语法

    1
    2
    include 'filename';
    require 'filename';

类与对象

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
<?php

class test_class{
var $test1;
function set_test1($test1_d){
$this->test1=$test1_d;
}
function get_test1(){
return $this->test1;
}
function echo_test1(){
echo $this->test1;
}
}
if(TRUE){
echo "testTrue";
}

$class_object=new test_class;

$class_object->set_test1('1');

echo $class_object->get_test1();

$class_object->echo_test1();
?>

  1. 解释语言,不需要编译

php
https://tsy244.github.io/2023/10/11/PHP/php/
Author
August Rosenberg
Posted on
October 11, 2023
Licensed under