PHP Basics

I Operators and Expressions

  • Arithmetic operator: + - * /%
  • Assignment operator: + = - = * = / =% ==
    Example: $a+= b his real Just yes b is actually b is actually a = $a + $b
  • Self addition and self subtraction operation: + + –
  • Operator: < = >! = = = >! = = = = >! = = = = = >! = = = = = = >! = = = = >! = = = = = >! = = = = = >! = = = = = = = >! = = = = = >! ====
    $a < $b

    =: assignment

    : judge whether the values are equal

    != Judge whether the values are not equal

    ===The judgment value and the type corresponding to the value must be equal

    ! Only one of the judgment value or the type corresponding to the value is not equal
  • Logical operators & &|| and or !
 	$a = 10;
    $b = 20;
    var_dump($a + $b);//30
    var_dump($a - $b);//-10
    var_dump($a * $b);//200
    var_dump($a / $b);//0.5
    var_dump($a % $b);//10
    var_dump($a . $b);//1020
    echo '<hr>';
    var_dump($a += $b); //$a=$a+$b; $a=30
    var_dump($a -= $b); //$a=30-$b; $a=10
    var_dump($a *= $b); //$a=200
    var_dump($a /= $b); //$a=10
    var_dump($a %= $b); //10%20=10
    var_dump($a .= $b); //1020

II Process control

  • Execution process: when the program executes the if statement, judge the expression. If it is true, execute the statement block, If false, do not execute

  • [note] in actual development, there are always some second-class goods, and the braces after if will be omitted If omitted, the first output statement block is the content in braces in if by default Other cases are the contents in else braces
    Bidirectional: form
    if (expression){
    Statement block 1;
    } else {
    Statement block 2;
    }

  • Execution process: when the program executes the if statement, judge the expression. If it is true, execute statement block 1. If it is false, execute statement block 2

  • [note] statement block 1 and statement block 2 will not be executed at the same time, nor will they not be executed at the same time. One of them will certainly be executed
    Multidirectional: form
    if (expression){
    Statement block 1;
    }else if (expression 2){
    Statement block 2;
    }else if (expression 3){
    Statement block 3;
    } else {
    Statement block 4;
    }

  • Execution process: when the program executes the if statement, first judge the expression 1. If it is true, execute the statement block 1 and directly jump out of the whole structure If false, continue to judge expression 2. If true, execute statement block 2 and jump out of the whole structure If it is false, continue with this judgment Finally, if both are false, the last else statement block is executed

  • [note] theoretically, there can be multiple if, and the last else can not

  • Homework: write a program to generate random numbers if the values are 1,3,5 Output "Lang Bo drinking". If the value is 246, output "Lao Wang drinking opposite Lang Bo"
    Nesting:
    if () {
    if () {

      		} else {
      		
      		}
      	}else {
      	
      	}
      [notes] When nested,Generally, two or three layers are nested.,
      switch-case:
      	Syntax format:
      		switch (expression) {
      			case Value 1:
      				Statement block 1;
      				break;
      			case Value 2:
      				Statement block 2;
      				break;
      			case Value 3:
      				Statement block 3;
      				break;
      			case Value 4:
      				Statement block 4;
      				break;
      			default:
      				Statement block n;
      				break;
      		}
      	[matters needing attention]
      	1.Default each case The back can be an integer,character string,floating-point
      	2.Default each case There's one in the back break,encounter break Just jump out of the whole structure
      	3.case Not in the back break,without,The program continues,Until you meet break,Just jump out of the whole structure
      	4.In theory, we can have more than one case,To output the same content
      	5.default Can have,Or not
    

    Cycle structure:
    while:
    Syntax structure:
    while (expression){
    Circulatory body;
    }
    Execution process: when the program is executed to the while structure, first judge whether the expression is true or false. If it is true, execute the loop body. If it is false, jump out of the loop
    for:
    Syntax format:
    for (expression 1; expression 2; expression 3){
    Circulatory body;
    }
    Execution process: first execute expression 1, then judge expression 2. If true, execute the loop body, then execute expression 3, and then judge expression 2. If true, continue to execute the loop body, then execute expression 3, and then judge expression 2. When the condition of expression 2 is false, jump out of the loop
    Question: 1 Write a program: calculate the sum of 1-100. The sum of 1-100 is 5050
    2. Write a program to output all odd numbers between 1-100
    3. Write a program, randomly generate 1-12 months, and output the number of days in each month (calculated by 28 in February)
    The difference between break and continue
    break: jump out of the whole loop structure and do not execute no matter how many statements are left behind
    continue: end this cycle and the following contents will not be executed Then execute the next cycle
    [note] using continue in a while loop may cause an endless loop

III Basic syntax of function

Basic format
	function Function name()
	{
		Function body;
	}
	[notes]
		1.The function is function start
		2.function Followed by a space,Space followed by function name,Write after function name().
		3.stay()Write below{}
		4.Function must be called to execute
		5.How to write when calling: Function name();
	Function name:
		1.Same format as variable
		2.Function names should be meaningful
		3.The function name follows the hump method or underscore   my_func()  myFunc()
		4.Function names are not case sensitive,(But we must be case sensitive)
No parameter, no return value
	parameter:Is the input of the function
	Return value:Is the output of the function
 There are parameters and no return value
	1.When defining,Parentheses after function name,The parameters in the parentheses are formal parameters.   A parameter is a variable
	2.When called,Parentheses after function name,The parameters in parentheses are actual parameters, and the actual parameters here are in the actual value   

There are parameters and return values
	1.return Refers to the return value
	2.A function name can only be defined once,Can be called multiple times
	3.When calling a function,Once encountered return,Return immediately,No matter how much code there is, it will no longer be executed
	4.return You can return a value,You can also return multiple values,This needs to return an array.
Default parameters
	1.Normally,There is no default value. The parameter is placed at the top,Parameters with default values are placed behind
	2.In parameter form,Write up to 5 parameters,

IV Scope

The execution valid range of a variable,Or the life cycle of a variable.
Internal variable
	/* 
	Internal variable
	1.After the function call, the variables in the function body will be released (destroyed)
	 */
	/* function huang()
	{
		$a = 250;
		echo $a;
	}
	huang();
	echo $a; */
External variable
	/* 
	External variable
	1.Variables outside the function cannot be called inside the function, and variables inside the function cannot be called outside the function
	 */
	$b = 500;
	function huang()
	{
		$a = 250;
		echo $a.'<br />';
		echo 'This is an internal call to an external variable,Value is'. $b;
	}
	huang();
	echo 'This is an external variable called externally,Value is'. $b;
Super global variable
	1.Method 1: global $b  When calling an external variable inside a function,use global
	2.Method 2: $GLOBALS  $_GET   $_POST  $_FILES  $_SESSION $_COOKIE $_SERVER
	3.use $GLOBALS Changing the value of a variable inside a function,At the same time, the value of the external variable of the function will also change
 Static variable
	1.use static keyword,Declared variables are static variables
	2.Once a static variable is declared,It's already worth it,Static variables will not be destroyed after the function call is executed,No matter how many times you execute.The value of the static variable is always accumulated

V Reference pass parameter

Reference pass parameter
	1.When referencing and transferring parameters&Must be written before the formal parameter
	2.When passing parameters by reference,When the formal parameters change, the values of the corresponding external actual parameters will also change together
 Parameters can not be defined outside
	Only when referencing parameters,You can define no actual parameters outside(variable),When transferring values,In fact, it is equivalent to defining a parameter outside(variable)
	/* $a = 250;  */
	function duhaitao(&$num)
	{
		$num = 300;
		echo $num.'<br />';
	}
	duhaitao($a);
	echo $a.'<br />';

Vi load file

	include
	require
	include_once
	require_once
	
	/* 
	include The difference between and require
	include:Include file. When the include file fails, a warning will appear, but the program will continue to execute
	require:Include file. When the include file fails, a fatal error will appear and the program will not be executed
	*/

	/* 
	Difference between with and without once:
	With once: when executing the program, first check whether the file is loaded. If it is not loaded, we will load it directly If already loaded No more loading
	[Conclusion] the execution efficiency without once is higher than that with once
	 */
	 
	 Grammatical structure:
		include '';
		include();

VII array

1. Basic definition
	array: array Keyword a set of data
	element:Each individual in the array is an element
	Key value pair: English Chinese Dictionary    hello===> Hello    world====>world

two. Array classification and declaration
	Index array
		read
			By finding the subscript of the array,Enclose subscripts in square brackets,You can find the corresponding value
			//echo $arr1[3];
		
		add to
			$arr1[7] = 8;
			$arr1[] = 10;//Find the value with the largest subscript in the array, and then add 1 to the subscript, which is the subscript corresponding to the current value of 10
		modify
			$arr1[8] = 30;//The value of the existing subscript is modified,
		delete
			unset($arr1[1]);
			var_dump($arr1);
	Associative array
		read
			/* echo $arr['222']; */
		add to
			$arr['aaa'] = 'Xu Haojie';
			$arr[] = 'Xiao Jie';//In this way, find the largest (number) or (number string) in the array, and then add 1 as the value key
		modify
			$arr['ccc'] = 'Minions';
		delete
			unset($arr['ccc']);
	Index association mixing
	Multidimensional array

VIII Array traversal

	for
		[notes]for Only index arrays can be traversed,And the subscripts are continuous from 0
	foreach
		foreach ($arr as $key => $value) { 
			/* echo $key .'<br />';
			echo $value.'<br />'; */
			echo $key.'=>'.$value.'<br />';
		}

IX Super global array

$_GET  $_POST
[notes]adopt form The data submitted by the form is through get perhaps post Submitted.

$_REQUEST: receive get and post Data submitted in any way

$_SERVER:Is a super global array[Dictation]
	SERVER_ADDR:Gets the name of the server ip address
	REMOTE_ADDR: Get client's ip address
	HTTP_REFERER:Where did you come from

Tags: PHP

Posted by merkinmuffley on Sat, 14 May 2022 08:24:41 +0300