S Q L

Based on example

SQL

/* * note 1: In principle, SQL keywords should be capitalized, but lowercase is ok */ /* Note 2: INSERT into 'values' (' 1','2', '3'); INSERT into' values' (' 1','2', '3'); / * * / modify data UPDATE SET ` table name ` ` field 1 ` = 'new value, ` field 2 ` =' new value where filter conditions; /* DELETE FROM 'table_name' where table_name = 1; /* SELECT * FROM 'table_name' WHERE table_name = 1 ORDER BY table_name DESC;Copy the code

Where filter criteria

Following the filter criteria behind where allows you to exclude irrelevant data and then match (modify, delete, query) SQL statements to achieve your purpose.

SQL

*/ DELETE FROM table where age<18 and city=' Beijing '; */ SELECT * FROM table WHERE age<18 or age>12Copy the code

Take PDO operating MySQL as an example

PHP

$DSN = 'mysql:dbname=php10-7; The host = 127.0.0.1 '; $pdo = new pdo ($DSN,' database ', 'password '); / * * * * * * * writing data (increase, deletion is write) to delete for example * * * * * * * * * * * / # predefined SQL $STH = $pdo - > prepare (" delete from table "); $STH ->execute(); / * * * * * * * read data * * * * * * * * * * * / # predefined SQL $STH = $pdo - > prepare (" select * from table "); $STH ->execute(); $rows $rows = $STH ->fetchAll(); Var_dump ($rows);Copy the code

As you can see, you only need to do it once; FetchAll ($STH ->fetchAll()).

Mathematical operations

Let’s start with the easiest thing to read: addition, subtraction, multiplication, division, remainder.

PHP

echo 1+1; / / add echo 1-1; / / echo 2 * 2; / / by echo 2/1; / / in addition to echo 2% 2; Echo 2**2; // Exponentiate (after php5.6)Copy the code

Comparison operations

This is easy to understand. Compare two values and return true or false

PHP

Var_dump (1<2, 1>2, 1<>2); Var_dump ($a == $b); === var_dump($a === = $b);Copy the code

Looking at a few you might not have noticed

PHP

$a = 1; $a = 1; $a = 'a'. 'b'; $a = 'a'. $a+ =1; $a+ =1; // Append 1 to the end of $a (as a string);Copy the code

A very common logical operation

The and and && operators return true if both sides are true.

PHP

$is = (1>2 and true); Var_dump ($is); $is = (1>2 && true); $is = (1>2 && true);Copy the code

Around the or and | | operators, any one is true, it returns true.

PHP

$is = (1>2 or true);
//此时输出 true,右侧满足true的条件,所以该表达式永远返回 true
var_dump($is);

// || 和 or 运算符是一个意思,不过||优先级更高
$is = (1>2 || true);
Copy the code

If /switch Executes the specified condition

PHP

Echo 'a'; echo 'a'; echo 'a'; echo 'a'; }else{ echo 'b'; }Copy the code

Switch functions like if in that it can read documents by itself.

The for/foreach/while loop executes specific code

Let’s start with the for loop, which is often used to specify a number of loops, such as 10

PHP

$I = 0; $I = 0; $I = 0; for($i=0; $i<10; $I ++){// Execute this code repeatedly and print different values of $I echo $I; }Copy the code

Look at the foreach loop, which iterates through a set of key-value pairs and the attributes of an object

PHP

$arr = [1, 2, 3, 4, 5]; $value = $key; $value = $key; $value; Foreach ($arr as $key=>$value){var_dump($key, $value); }Copy the code

The *foreach* syntax structure provides a simple way to iterate over groups of numbers. * Foreach * can only be applied to arrays and objects, and attempts to apply to variables of other data types, or uninitialized variables, will raise an error message. There are two types of syntax:

PHP

foreach (array_expression as $value)
 statement
foreach (array_expression as $key => $value)
 statement
Copy the code

The first format iterates over a given array_expression array. Each time through the loop, the value of the current cell is assigned to $value and the pointer inside the array moves forward one step (so the next cell will be returned in the next loop).

The second format does the same thing, except that the key name of the current cell is also assigned to the variable $key each time through the loop.

Other loop statements can read the document by themselves.

Break /continue loop control statements

  • Break the loop
  • Cuntinue skips this loop and continues to the next one

PHP

For ($I =0; $I =0; $i<10; $i++){ if($i == 5){ cuntinue; } echo $i; }Copy the code

Include code contains statements

In practice, we will put frequently used common code in other.php files and include them to reduce code duplication.

Let’s start with a function library file

PHP

// functions.php function debug(){echo 'output debugging information '; }Copy the code

Include function libraries in your program and call related functions.

PHP

// include functions.php include('functions.php'); // Then you can call the debug function. debug();Copy the code

PHP actually provides 4 include statements with similar functionality. See:

  • Include (file name)
    • Include specified files
  • Require (filename)
    • Contains the specified file. If this fails, the program stops running
  • include_once()
    • The same file is included only once. Other functions are the same as include
  • require_once();
    • The same file contains only once, and other functions are the same as require

Introduction of function

The point of a function is to “encapsulate” commonly used code for reuse.

The term

  • define
    • Is to write functions
  • call
    • It’s using a function that you’ve written.
  • Line and
    • A list of arguments to fill in when defining a function, such asxy($a,$b)
  • The arguments
    • Parameter values to fill in when calling a function, such as xy(1,2)
  • The return value
    • The function outputs the result of its execution via return
  • The default value
    • When defining functions, define default values for parametersxy($a,$b=2)
    • You can omit this parameter when calling a functionxy(1), the second parameter defaults to 2
  • A local variable
    • Variables defined inside a function (not available outside the function)
  • The global variable
    • Variables defined outside the function (not available inside the function)

Let’s start with an example

PHP

Function xy($a, $b,$c =3){if($a,$b,$c =3){if($a,$b,$c =3){if($a,$b,$c =3); is_numeric($a) || ! is_numeric($b) || ! is_numeric($b) ){ return NULL; $x = $a+$b+$c; return $x; $r1 = xy(1,2); $r1 = xy(1,2); $r3 = xy(8,10,5); $r4 = xy('a','b'); $r4 = xy('a','b');Copy the code

Example syntax decomposition

PHP

function xy($a, $b, $c=3){ $x = $a+$b+$c; return $x; } $r1 = y (1, 2);Copy the code

One explanation

  • The function keyword, which means you want to define functions

  • Xy is the unique name of the function and is used when calling it

  • ($a,$b,$c=3) <! 17 -- -- > -- --  ` $r1 ` variables used to receive the function return value - ` xy () - 1, 2 ` ` ` the function respectively corresponding to the function of ` $a `, ` $b ` parametersCopy the code

Local variables vs global variables

A variable defined inside a function is a local variable (invisible to the outside). A variable defined outside a function is a global variable and invisible to the inside of the function.

Want to each other? What problem do you think function argument passing and return value are used to solve?