PHP Study Notes

foreword

Previous articlePHP Study Notes (Go Forward)
Welcome to the second article of PHP Learning (Cai Guan Er You): Continuous learning and accumulation, today's me is better than yesterday's me. The following PHP articles will continue to update relevant study notes, and look forward to learning and communicating with you!
insert image description here

Judge sentences

When writing code, we need to perform different actions for different judgments. This is where you can use conditional statements in your code to accomplish this task. In PHP, the following conditional statements are provided:
if statement - execute code if the condition is true
if...else statement - execute a block of code if the condition is true, and execute another block of code if the condition is not true
if...else if...else statement - execute if several conditions Executes a block of code when one of several conditions is true
switch statement - executes a block of code when one of several conditions is true

if statement

//语法:
if (条件)
{
    
    
    条件成立时要执行的代码;
}

Single condition of if statement:
insert image description here

If the expression after if is judged to be true, statement 1 after if will be executed; if it is judged to be false, statement 1 will be bypassed and the following statement will be executed.

the case

//当前分钟大于或等于30时,返回hello world。
$time=date("i");
if($time>=30){
    
    
echo "hello world";
}

if...else statement

//语法
if (条件)
{
    
    
条件成立时执行的代码;
}
else
{
    
    
条件不成立时执行的代码;
} 

the case

//如果分钟大于等于30就返回hello world;反之返回你好,世界
$time=date("i");
if($time>=30){
    
    
echo "hello world";
}else {
    
    
echo "你好,世界";
}

the case

//if else 语句的嵌套
<form action="if3.php" method="post"> 
        <h3>请选择今天的天气</h3>
        <select name="select" size="1">
        <option selected value="sunny">sunny</option>
        <option value="rain">rain</option>
        <option value="cloudy">cloudy</option>
        <option value="foggy">foggy</option>
        <input type="submit" >
</form>
$select=isset($_POST['select'])?$_POST['select']:"";//进行post传参
if ($select=="sunny"){
    
    
echo "可以出去玩!";
}else{
    
    
	if($select=="rain"){
    
    
		echo "外面下雨了";
	}else if($select=="cloudy"){
    
    
		echo "外面刮风了";
	}else{
    
    
		echo "外面起雾了";
	}
	}

insert image description here

Supplement: 三元运算符
The function of the ternary operator is consistent with the "if...else" process statement. It is written in one line. Appropriate use of the ternary operator will make our code more concise and more efficient.
Ternary operator syntax: Condition? Result 1 : Result 2 Explanation: The position in front of the question mark is the condition for judgment, if the condition is met, the result is 1, and if the condition is not met, the result is 2.

$a = 10;
$b = 7;
$a % 2 == 0 ? print '$a 是偶数!<br>' : '';//输出 $a 是偶数!
$b % 2 == 0 ? '' : print '$b 是奇数!';//输出 $b 是奇数!
echo "<br>";

The ternary operator can be extended and used. When the set condition is true or not, the execution statement can be more than one sentence:
(expr1) ? (expr2).(expr3) : (expr4).(expr5);

$a = 10;
$b = 6;
$c = 12;
$x = $a>$b ? ($a<$c ? $c-$a : $a-$c) : ($b<$c ? $c-$b : $b-$c);
echo 'x的值是:'.$x;
//输出 x的值是:2

if...else if...else statement

//语法
if (条件)
{
    
    
    if 条件成立时执行的代码;
}
else if (条件)
{
    
    
    else if 条件成立时执行的代码;
}
else
{
    
    
    条件不成立时执行的代码;
}

the case

insert image description here

html page:

<form action="scoredo.php" method="post" >
        <h3>成绩测试器</h3>
        <!-- 。placeholder可以用来描述输入字段预期值的简短的提示信息。 -->
        <input type="text" name="score1" placeholder="请输入成绩">
        <button type="submit" value="查询">提交
    </form>

php page:

<?php
$score1=isset($_POST['score1'])?$_POST['score1']:"";
if(!is_numeric($score1)){
    
    
    echo "请输入正确成绩类型";
}else{
    
    
    if($score1<0||$score1>100){
    
    
        echo "错误成绩";
    }else if($score1>=90){
    
    
        echo "您的成绩为优秀";
    }else if($score1>=70){
    
    
        echo "您的成绩为良好";
    }else if($score1>=60){
    
    
        echo "您的成绩为及格";
    }else{
    
    
        echo "您的成绩不合格";
    }
}
?>

Effect:
insert image description here

Note:
When there are too many conditions, you can use the form of matching if;
if the conditions are relatively single, you can use the matching method of elseif;
when the conditions are inconsistent, you can use the if else nested syntax. If there are too many conditions, it is not recommended to use the nested syntax , will affect the aesthetics of the code, it is recommended to use the following switch...break... statement

switch...break... statement

//语法
switch (n)
{
    
    
case term1:
    如果 n=term1,此处代码将执行;
    break;//执行结束
case term2:
    如果 n=term2,此处代码将执行;
    break;
default:
    如果 n 既不等于 term1 也不等于 term2,此处代码将执行;
}

A simple expression n (usually a variable) is first evaluated once. Compare the value of the expression with the value of each case in the structure. If there is a match, the code associated with the case is executed. After the code is executed, use break to prevent the code from jumping into the next case to continue execution. The default statement is used to execute when there is no match (ie no case is true).

the case

//根据用户输入放入的数字来判断显示类型
<?php
$i=2;
switch($i){
    
    
    case 1:echo "<input type='text' value='{
      
      $i}'>";
    break;
    case 2:echo "<input type='button' value='{
      
      $i}'>";
    break;
    case 3:echo "<input type='checkbox'>";
    break;
    case 4:echo "<input type='redio'>";
    break;
    default:echo "错误";
}
//i=2,输出一个按钮
?>

the case

Calculate the result according to the operation specified by the user

<!-- placeholder可以用来描述输入字段预期值的简短的提示信息 -->
    <form method="post" action="switchcomputer.php">
        <input type="text" name="num1" placeholder="num1">
        <select name="op" id="">
            <option value="+">+</option>
            <option value="-">-</option>
            <option value="*">*</option>
            <option value="/">/</option>
        </select>
        <input type="text" name="num2" placeholder="num2">
        <input type="submit" value="计算">
    </form>
$num1=$_POST['num1'];
$op=isset($_POST['op'])?$_POST['op']:"";//判断op是否被设置,如果设置就是op,没有被设置就是空
$num2=$_POST['num2'];
switch($op){
    
    
    case "+":$result=$num1+$num2;
    echo $result;
    break;
    case "-":$result=$num1-$num2;
    echo $result;
    break;
    case "*":$result=$num1*$num2;
    echo $result;
    break;
    //除数不能是0,如果是0就返回错误
    case "/":if($num2!=0) 
    {
    
    
        $result=$num1/$num2;
        echo $result;
        break;
    }else{
    
    
        echo "错误";
    }
}

Effect:
insert image description here

Brief summary:
The if statement can do all the branch structures
The switch statement is used to deal with the branch structure with more conditions, one single, and fixed value matching

loop statement

Loop statement in PHP
1. for loop, syntax "for (initial value; condition; added value) {loop body}";
2. do while loop, syntax "do{loop body}while (condition)";
3. while Loop, syntax "while (condition) {loop body}";
4. foreach loop.

for loop

//语法
for(初始化计数初值;判断条件;增加计数值){
    
    

循环体;

}
//当for循环语句中的循环条件没有时,会造成死循环,要避免这种情况出现

the case

for($i=1; $i<=10 ;$i++){
    
    
 if($i==5)break;//表示打破,输出1 2 3 4 5 
// if($i==5)continue;//表示继续,输出1,2,3,4,5,6,7,8,9,10
echo $i."<br>";
}

the case

//打印一个等腰三角形
$n=4;
for($i=0;$i<$n;$i++){
    
    
    for($j=1;$j<($n-$i);$j++){
    
    
        echo "&nbsp";//第一行是三个空格一个 *
    }
    for($k=0;$k<(2*$i+1);$k++){
    
    
        echo "*";
    }
    echo "<br>";
}
//输出:
   *
  ***
 *****
*******

the case

// 九九乘法表
echo "<table border=1px>";
for($i=1; $i<=9;$i++){
    
    
    echo "<tr>";
    for($j=1;$j<=$i;$j++){
    
    
        echo "<td>"."{
      
      $i}".'*'."{
      
      $j}".'='.$i*$j."</td>";
    }
    for($k=0;$k<=(9-$j);$k++){
    
    
        echo "<td></td>";
    }
    echo "<tr>";
}
echo "</table>";

Effect:
insert image description here

while loop

//语法
while(判断条件){
    
    

循环体;

}
//while循环先进行循环条件的判断,后进行循环;如果条件不满足,就跳出循环

the case

When the conditions are known (usually regular data), we can use the for loop;
if the conditions are not fixed, we need to use the conditional judgment. At this time, we can use the while loop, which is a more flexible loop statement

do while loop

//语法
do{
    
    

循环体;

}while(判断条件);
//do while 循环先进入循环,再进行条件的饿判断;因此至少会进行一次循环

the case

//打印出1-10的奇数和偶数
$i=1;
do{
    
    
    if($i % 2 ==0){
    
    
      echo $i."是偶数 "." ";
    }else{
    
    
      echo  $i."是奇数 "."  ";
    }
    //i进行自加
    $i++;
}while($i<=10);

insert image description here

foreach loop

//语法
foreach($array as $value){
    
    

执行代码;

}
或者是;
foreach($array as $key => $value ){
    
    

执行代码;

}
//$key  数组键名   $value  数组键值
//foreach循环属于数组的遍历循环,每进行一次循环,当前的值就会赋给变量$value,并且数组指针是逐一移动的,直到最后一个数组元素

the case

//一维数组的遍历
$stu1=array(
    '1001'=> '小明',
    '1002'=> '小红',
    '1003'=> '晓东'
);
foreach($stu1 as $key=>$value){
    
    
    echo $key.$value."<br>";
}
//输出
1001小明
1002小红
1003晓东

the case

//二维数组的遍历
$stu2=array(
    "人事部"=>array('a','b','c'),
    "研发部"=>array('d','e','f'),
);
//打印出人事部有abc,研发部有def;
foreach($stu2 as $key=>$value){
    
    
    echo $key.":";
foreach($value as $keyin=>$valuein){
    
    
    echo $valuein." ";
}
echo "<br>";
}
//输出
人事部:a b c
研发部:d e f

the case

//将一个二维数组遍历到表格
$arr1=array(
    array('name'=>'lili','sex'=>'famale','age'=>12),
    array('name'=>'tom','sex'=>'male','age'=>15),
    array('name'=>'join','sex'=>'famale','age'=>10)
);
echo" <table border='1'>";
echo "<tr><th>id</th><th>name</th><th>sex</th><th>age</th></tr>";
foreach($arr1 as $key=>$value){
    
    
    echo "<tr>";
    echo "<td>$key</td>";
foreach($value as $keyin=>$valuein){
    
    
    echo "<td>$valuein</td>";
}
    echo "</tr>";
}
echo "</table>";
echo "<br>";

insert image description here

The above is a brief summary of the judgment statement and process statement in php. The next article will continue to update the study notes and look forward to communicating with you!
insert image description here

If there are any deficiencies, thanks for pointing it out!

Guess you like

Origin blog.csdn.net/weixin_64122448/article/details/127117310