PHP from scratch - study notes (c) Advanced

Foreword

(This preface a bit long) although advanced, but belongs to the basic part has not yet finished, and that part is circulating loop nests, which are related to language, like C, I did not write, because there is no new knowledge, now understand the C language teacher said:. "learn the C language, linguistics other programming time will be a lot of effort," of course, did not skip the time to learn, or learn step by step again, and then discovered a never seen before loop: foreach循环the following describes this single cycle:

foreach statement

In PHP the foreach loop, commonly used in the loop through the array (described below, see where the main use of this statement), generally used in two ways: without removing the scale, remove the standard.
(1) value only, without removing the standard

<?php
 foreach (数组 as){
//执行的任务
}
?>

(2) simultaneously and remove the standard value

<?php
foreach (数组 as 下标 =>){
 //执行的任务
}
?>

Write an example:

<?php
$books = array(
'2010'=>'鬼谷子',
'2011'=>'人性的弱点',
'2012'=>'狼道',
'2013'=>'莫非定律',
'2014'=>'山海经',
'2015'=>'红楼梦',
'2016'=>'西游记',
'2017'=>'水浒传',
'2018'=>'三国演义',
'2019'=>'金瓶梅嘿嘿',
);//10本书的编号和书名,用数组存储

//使用循环结构遍历数组,获取编号和书名  
foreach($books as  $v)//foreach($students as $key => $v)只需在这里把代码改成这样即可
{ 
    echo $v;//输出(打印)书名
	echo "<br />";
}
?>

As for the array, introduced later. Look at the last few lines of code. Augenstern
Remove the subject of self-realization.


Note: I used to use a browser to compile the code, now for the compiler software, so I run from now to the back of the school will be the source, PHP inside the browser compiled and source code is not the same, give chestnut: which compiler browser , line breaks <br />, and the source code inside the wrap is \nthe C language is somewhat similar.

Advanced

Array

Array brief introduction:
define an array: array ()
can use the array () to create a new array that accepts any number of comma-separated (key) => (value)

array( key => value
……
)
//key可以是一个整数int,或字符串string
//value可以是任意类型的值。

Finally, a cell array comma omitted
look at an example of an array of definitions:

<?php
$array = array(
    "lxj" => "lalala",
    "wkx" => "xixixi",
);
//两种方法
$array = [
    "lxj" => "lalala",
    "wkx" => "xixix",
];
?>

The second method I specifically went to check a bit, the official PHP manual explains that starting from PHP 5.4 can use this method, short array definition method.

About key

1. contains valid integer string is converted to an integer, such as "8", are stored as 8. But "08" will not be cast. Because he is not a valid decimal number.
2. The float will be converted to an integer. Behind the decimal point will be discarded. Note that rounding, not rounded, but not all throw (ha ha, really a waste).
3. Boolean will be converted into an integer, true is stored as 1 and false is stored as of 0. The
4.NULL will be converted to an empty string, null is actually stored as "."
Then said array:
if an array defining a plurality of cells use the same key, it will use only the last one will be overwritten before, see code:

<?php//这个代码同时反映类型强制和覆盖
$array = array(
    1 => "mary",
    "1" => "jack",
    1.5 => "rose",
    true => "lxj"
);
var_dump($array);
?>

The result, there will only be LXJ
Augenstern
PHP arrays can also be mixed key, that is, both integer array of variables, there are strings. I do not write the code. Nothing promoter can be exemplified.

Access array

By Syntax: array name [key]
did not explain what the code is unclear. Code:

<?php
$array = array(
    "lxj" => "Augenstern",
    0258 => 8520
);
var_dump($array["lxj"]);
var_dump($array[0258]);
?>

This output is
Augenstern
tip: The brackets and braces can be used interchangeably here to achieve the same effect. Hurriedly try to go.

Add, modify, delete array of values

Here with a code that will be able to see how to play:

<?php
$arr = array(
    "lxj" => "Augenstern",
    0258 => 8520,
);
$arr["lxj"] = "zxcv0221";//修改数组值
$arr["wkx"] = "wangkaixin";//增加一个数组值
unset($arr["wkx"]);//删除wkx这个key和其对应的值
unset($arr);//删除整个数组
?>

Clear. An array of learning being here.

function

User-defined functions

How function definition: to define a function, in which first PHP plus function keyword
follows is an example of pseudo-code function (not run):

<?php//这是一段伪代码,为了直观的介绍函数的简单定义而已。
function lxj($var_1, $var_2, $var_3,/*,,,,,,*/)
{
	echo "伪代码\n";
	return ...;
}//此代码不能运行
?>

Function name their own definition, but it is not casual, naming rules: A valid function name starts with a letter or an underscore, followed by letters, numbers or underscores.
Any valid PHP code may appear inside a function, even other functions and contains the definition of the class. 1. The function need not be defined before calling, unless the following function is conditional is defined. 2. When a function is conditionally defined, it must be defined before calling the function. Look at an example:

<?php
$makelizi = true;
lizi();//lizi函数还不能调用,因为它还不存在
lxj();//但是bar函数可以调用,因为bar函数不是有条件的函数。
if($makelizi){
	function lizi()
	{
		echo "lalala\n";
	}
}
//可以调用lizi函数了,因为$makelizi的值为真了。
if($makelizi) lizi();
function lxj()
{
	echo "xixixi\n";
}
?>

This code can be run, run their own what contrast is more obvious, to understand more clearly.
Again a function code for the function sleeve

<?php
function lxj(){
	function wkx(){
		echo "nice,xiongdei\n";
	}
}
//这里不能直接调用wkx函数,因为他还不存在。可以理解为没有被定义。
lxj();
//现在就可以调用wkx函数了,因为lxj函数被执行了,使得wkx函数被定义。
wkx();
?>

Introduces all the comments in the code. All functions in PHP have the global scope, and can be defined outside a function call within a function of. The opposite is also possible. PHP does not support function overloading, it is impossible to cancel the function definition, or redefinition of already declared.
Note: The function name is unrelated to the size of the case, but try to write the same, develop a good habit.
Recursive function:
PHP can also call a recursive function, or to look at the code

<?php
function lxj($f=1){
	if($f < 20){
		echo "lalala\n";
		lxj($f+1);
	}
}
lxj();
?>

Recursive function calls should be noted that to avoid calling more than 100-200 layer, because the stack may collapse so that the termination of the current script. Infinite recursion can be considered a programming error.

Function parameters

Function parameters can not fill, make it the default parameters work, you can also pass parameters.
Look at a way to pass parameters by reference code:

<?php
function lxj(&$string)
{
	$string .= "或者其他的";
}
$str = "这是一个字符串,";
lxj($str);
echo $str;//输出结果:这是一个字符串,或者其他的[Finished in 0.1s]
?>

Or directly pass parameters, overriding the default

<?php
function lxj($a=10,$b=20)
{
	echo $a+$b;
}
lxj(20,25);
//运行结果:45[Finished in 0.1s]
?>

These are simple to use arrays, functions, plus school C language, a little foundation, the school will not be too difficult. Remind everyone what is usually overlooked, is the official handbook, like PHP, Python and other programming languages ​​will have its official handbook, at least compared to the related books on the market, it's free (emphasis ha ha).

Published 26 original articles · won praise 12 · views 3218

Guess you like

Origin blog.csdn.net/qq_45836474/article/details/105001575