PHP学习笔记:基础

时间:2022-05-03 00:19:16
1. 基本语法
PHP 的脚本块以 <?php 开始,以 ?> 结束,我们可以把 PHP 的脚本块放置在文档中的任何位置
PHP每个代码都必须以分号结束

2. 变量
在 PHP 中,不需要在设置变量之前声明该变量,不必向 PHP 声明该变量的数据类型,根据变量被设置的方式,PHP 会自动地把变量转换为正确的数据类型。
eg:

$a = 10;
$b = 5;
$b .= $a;
echo $b;

3. 运算符
PHP 支持所有基本运算符:
+    -    *    / %    ++    --
=    +=    -=    *=    /=    .=    %=
==    !=    >    <    >=    <=
&&    ||    !
eg:

$a = 10;
$b = 5;
$b .= $a;
echo $b;
4. 数组

PHP有三种数组类型:
       数值数组:带有数字 ID 键的数组
       关联数组:数组中的每个 ID 键关联一个值
       多维数组:包含一个或多个数组的数组

数字数组:
数值数组存储的每个元素都带有一个数字 ID 键,创建方式以下两种:
$type1 = array(1, 2, 3);

$type2[0] = 1;
$type2[1] = 2;
$type2[2] = 3;

echo "array1[2]:" . $type1[2] . " array2[2]: " . $type2[2];
关联数组:
关联数组,它的每个 ID 键都关联一个值。创建方式以下两种:
$name_age1 = array("zhou"=>20,"huang"=>30,"yan"=>40);

$name_age2["zhou"]=20;
$name_age2["huang"]=30;
$name_age2["yan"]=40;

echo "name_age1['yan']:" . $name_age1['yan'] . " name_age2['yan']: " . $name_age2['yan'];
echo " <br>";  
多维数组:
在多维数组中,主数组中的每个元素也是一个数组。在子数组中的每个元素也可以是数组,以此类推
eg:
$student = array
(
        "zhou"=>array
        (
                1,
                2,
                3
        ),
        "huang"=>array
        (
                1,
                2,
                3
        )
);
echo "student['zhou'][2]:" . $student['zhou'][2];
echo " <br>";
5. if..elseif..else 、switch
PHP 的if、swithch语句和c版的基本类似,上例子:
$var = 2;

if ($var == 1)
  echo "if_test: 1"; 
elseif ($var == 2)
  echo "if_test: 2"; 
else
  echo "if_test: other"; 
echo " <br>";

switch ($var)
{
case 1:
  echo "switch_test: 1";
  break;
case 2:
  echo "switch_test: 2";
  break;
default:
  echo "swith_test: 3";
};
echo " <br>";
6. 循环
while : 只要指定的条件成立,则循环执行代码块
do...while : 首先执行一次代码块,然后在指定的条件成立时重复这个循环
for : 循环执行代码块指定的次数
foreach : 根据数组中每个元素来循环代码块
eg:
$name_age2["zhou"]=20;
$name_age2["huang"]=30;
$name_age2["yan"]=40;

//do...while
$i=0;
do
{
  $i++;
  echo "The number is " . $i . "<br />";
}while ($i<5);

//foreach
foreach ($name_age2 as $value)
{
  echo "Value: " . $value . "<br />";
}
7. 函数
php函数和c函数类似,有参数、返回值
一个函数使用例子:
//function
function add($x,$y)
{
  $total = $x + $y;
  return $total;
}

echo "100 + 10 = " . add(100,10);

完整代码:
test.php
<html>
<body>

<?php
/*
* php note
*/
// php note

//variable
$txt = "hello php";
echo $txt;
echo " <br>";

//operator
$a = 10;
$b = 5;
$b .= $a;
echo $b;
echo " <br>";

//array
$type1 = array(1, 2, 3);
$type2[0] = 1;
$type2[1] = 2;
$type2[2] = 3;
echo "array1[2]:" . $type1[2] . " array2[2]: " . $type2[2];
echo " <br>"; 

//array
$name_age1 = array("zhou"=>20,"huang"=>30,"yan"=>40);
$name_age2["zhou"]=20;
$name_age2["huang"]=30;
$name_age2["yan"]=40;
echo "name_age1['yan']:" . $name_age1['yan'] . " name_age2['yan']: " . $name_age2['yan'];
echo " <br>"; 

//array
$student = array
(
        "zhou"=>array
        (
                1,
                2,
                3
        ),
        "huang"=>array
        (
                1,
                2,
                3
        )
);
echo "student['zhou'][2]:" . $student['zhou'][2];
echo " <br>";

//if
$var = 2;
if ($var == 1)
  echo "if_test: 1"; 
elseif ($var == 2)
  echo "if_test: 2"; 
else
  echo "if_test: other"; 
echo " <br>";

//switch
switch ($var)
{
case 1:
  echo "switch_test: 1";
  break;
case 2:
  echo "switch_test: 2";
  break;
default:
  echo "swith_test: 3";
};
echo " <br>";

//do...while
$i=0;
do
{
  $i++;
  echo "The number is " . $i . "<br />";
}while ($i<5);

//foreach
foreach ($name_age2 as $value)
{
  echo "Value: " . $value . "<br />";
}

//function
function add($x,$y)
{
  $total = $x + $y;
  return $total;
}

echo "100 + 10 = " . add(100,10);

?>

</body>
</html>
测试运行:
浏览器输入:http://192.168.21.133/test.php
输出:
hello php
510
array1[2]:3 array2[2]: 3
name_age1['yan']:40 name_age2['yan']: 40
student['zhou'][2]:3
if_test: 2
switch_test: 2
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
Value: 20
Value: 30
Value: 40
100 + 10 = 110