The seventh step of php basic tutorial array supplement and loop basics

Key-value pair

In the previous section, we briefly understood the definition, value and storage of the array. This section supplements the content of the array in the previous section.
In the previous section, we know that the index is used to mark the position of the value, and the value of the current position can be obtained through the index. The relationship between an index and a value is a mapping relationship, called a key-value pair. The key refers to the index, and the value refers to the specific value. In some programming languages, this relational array is called a dictionary.

Defining this kind of key-value pair relationship in php can be created by the following code:

$a=array("name"=>"Xiaoming","age"=>"37","sex"=>"nan");

In the above code, an array variable a is defined, and the definition of the number is different from before. We look at the first key-value pair "name"=>"Xiaoming", where name is the key, and the value corresponding to this key is Xiaoming. The second "age"=>"37"key is age and the value is 37. We can use the following methods to obtain values:

$a ["age"]

The output is as follows:

echo $a ["age"];

The complete code is:

<?php
	$a=array("name"=>"Xiaoming","age"=>"37","sex"=>"nan");
	echo $a ["age"];
?>

The results are as follows:

Insert picture description here
The setting method is the same as the setting method introduced in the previous section, as follows:

$a ["age"]=18;

Complete code:

<?php
	$a=array("name"=>"Xiaoming","age"=>"37","sex"=>"nan");
	$a ["age"]=18;
	echo $a ["age"];
?>

The results are as follows:
Insert picture description here

cycle

Repeating a sentence of code or a statement of a code block during program execution during looping can save us the amount of code writing and enhance the readability of the program.

for loop

The for loop is a way of looping in a php program. The syntax is as follows:

for (初始值; 条件; 增量)
{
    
    
    要重复的代码或代码块;
}

When we are in the project development, if you need to repeat the output ten times, hello, you may think that I can directly echo ten times, but what about 100 times or 1000 times? It was embarrassing at this time. You can easily solve this problem by using loops. Check out the following example:

<?php
	for($i=0;$i<10;$i++){
    
    
		echo 'hello ';
	}
?>

The above code uses a for loop. In the parentheses of the for loop, a variable i is defined at the first initial value, and the initial value is 0; at the second condition value, the given condition is that the variable i is less than 10. Here, the variable i is less than 10 means "when the variable i is less than 10, this loop will always execute"; the last increment value is filled with $i++, where ++ means that the variable is 0 at the beginning , And then add it every time the loop is executed.

The above explanation may be confusing for readers with zero knowledge. I explained above, each execution time refers to the first performance of this cycle will execute echo 'hello ';this code, but this time will not be out of the loop. Look at the following code:

<?php
	for($i=0;$i<10;$i++){
    
    
		echo 'hello ';
	}
	echo ' 程序执行完了';
?>

In the first program is executed echo 'hello ';, the execution will not echo ' 程序执行完了';, because the loop, the value of the variable $ i not greater than 10, so this loop will continue (in the conditions of the loop, the variable i is set less than 10, Under no special circumstances, the loop will always be executed, and the code in curly braces will be executed repeatedly).
When the echo'hello'; in the loop is executed once, the variable i will increase by 1, and change from 0 to 1. Take the condition to judge, the condition is satisfied and continue the loop, know that i is not less than 10, the condition is not satisfied, the loop Will jump out.
The result is as follows:
Insert picture description here
If you want to see the change of variable i intuitively, the program code can be written as follows:

<?php
	for($i=0;$i<10;$i++){
    
    
		echo 'hello <br/>';
		echo $i.'<br/>';
	}
?>

What appears in the above code is a <br/>line break in the html code, that is, output from the first line to the next line, which is convenient for observation.
The results are as follows: The
Insert picture description here
above code lists the changes in the value of variable i in detail.

Loop through the output array values

Array value We can use print_r to output all the values ​​of the current array. You can also output all the values ​​in the array by looping.
Look at the following example:

<?php
	$a=array('abc','cba',1,22,'as');
	echo '数组 a 的长度是'.count($a).'<br/>';
	for($i=0;$i<count($a);$i++){
    
    
		echo $a[$i].'<br/>';
	}
?>

In the above code, the count function is used to calculate the length of the array a. The variable i starts from 0 and cannot exceed the length of the array calculated by count, which is 5. The results are as follows: The
Insert picture description here
php tutorial is continuously updated, welcome to follow, like, and favorite

Guess you like

Origin blog.csdn.net/A757291228/article/details/107326046