Home>Article>php教程> Basic knowledge of php study notes

Basic knowledge of php study notes

WBOY
WBOY Original
2017-07-18 09:26:21 2161browse

i have been studying php for more than a year, and i have accumulated a lot of notes, which are quite complicated. let me write an article to sort them out.

php basic part

PHPbasic commands for outputting text:echoandprint.

the difference between echo and print

echois a php statement,printandprint_rare functions. statements have no return value, and functions can have return values (even if not use )

echoto output one or more strings.
printcan only print out the values of simple type variables (such as int, string)
print_rcan print out the values of complex type variables (such as arrays, objects)

var_dump and print_r difference

var_dumpreturns the type and value of the expression, whileprint_ronly returns the result. compared with debugging code, usingvar_dumpis easier to read.

variables

variables are used to store values, such as numbers, text strings, or arrays. all variables in php start with a $ symbol.
php variable names are case-sensitive!

php has three different variable scopes:

local(局部)
global(全局)
static(静态)

variables declared outside a function haveGlobalscope and can only be accessed outside the function.

variables declared inside a function haveLOCALscope and can only be accessed inside the function.

theglobalkeyword is used to access global variables within a function.

php static keyword

normally, when a function completes/executes, all variables are deleted. however, sometimes i need to not delete a local variable. achieving this will require further work.

to accomplish this, use the static keyword when you first declare the variable:

function mytest() {
static $x=-1;
echo $x;
$x--;
}
mytest();//-1
echo "
";
mytest();//-2
echo "
";
mytest();//-3
?>

php type

php类型:**php 支持八种原始类型。**

boolean

to specify a boolean value, use the keywords true or false. both are case-insensitive.

integer type

we can use (int) to cast a decimal into an integer.

 var_dump((int)(26/3));//int(8)
?>

arrays

there are three types of arrays in php:

索引数组:就是下标是顺序整数作为作为索引(比如第几排第几列)$class[5]
关联数组:就是下标是字符串作为索引(比如名字)$class2["zhangsan"]
多维数组 - 包含一个或多个数组的数组

the subscript is either an integer or a string.

$array = array(
"foo" => "bar",
"bar" => "foo",
);
// 自 php 5.4 起
$array = [
"foo" => "bar",
"bar" => "foo",
];
?>

array cells can be accessed through thearray[key]syntax.
note: this does not mean always quoting key names. there is no need to quote the key names of constants or variables, otherwisePHPwill not be able to parse them.

array operators

example name result $a $b union union of $a and $b $a == $b equality if $a and $b have the same key/ value pairs true $a === $b congruent true if $a and $b have the same key/value pair and are of the same order and type $a != $b unequal if $a is not equal to $ b is true $a $b is not equal. if $a is not equal to $b, it is true$a !== $b is not equal. if $a is not equal to $b, it is true

tr>



the+operator appends the array elements on the right to the array on the left. if the keys in both arrays are present, only the ones in the left array are used, and the ones on the right are ignored.

object

to initialize an object, use the new statement to instantiate the object into a variable.

commonly used functions

the strlen() function is used to calculate the length of a string.
the strpos() function is used to retrieve a string or a character within a string.

constants

constants can be defined using the define() function. once a constant is defined, it cannot be changed or undefined.
commonly used magic constants:

definition of constant examples:

define("poems" , "homeric epic");
echo poems ;//outputs "homeric epic"
?>

php string operatorsp>

in php, there is only one string operator.
the concatenation operator(.)is used to concatenate two string values. for example:echo "a= ".$a."
";

the left side of the string literal "a=" is connected to the value of the variable $a, and the second place is connected to the newline character"
"

php function

the function will only be executed when it is called. this is the same as js. similarly, the function definition also uses the function key beginning with the word .

 function sum($x,$y){
$z=$x + $y;
return $z;
}
echo "-2+10= ".sum(-2,10);//outputs "-2+10=8"
?>

when there is noreturnstatement, the above will become "-2 10=";

process control

in here, only theforeachstatement will be discussed.

theforeachstatement traverses the output array:
syntax:

foreach (array_expression as $value){ statement}; foreach (array_expression as $key => $value){ statement};

the parameterarray_expressionspecifies the array to be traversed, and$valueis the value of the array

 $actors [0] ="marry";
$actors [1] ="lorry";
$actors [2] = "mike";
foreach ($actors as $values){
echo "name:$values
";
}
?>

above the code will output:
name:marry
name:lorry
name:mike

two important magic methods

1. __set( )方法:这个方法用来为私有成员属性设置值的,有两个参数,第一个参数为你 要为设置值的属性名,第二个参数是要给属性设置的值,没有返回值。 2. __get()方法:这个方法用来获取私有成员属性值的,有一个参数,参数传入你要获取的成员属性的名称,返回获取的属性值,这个方法不用我们手工的去调用

the methods in php are not case sensitive

require(dirname(__file__).'/global.php'); //引入全局文件 require(dirname(__file__).'/config.ini.php'); //引入基本配置文件

object operator and double colon operator

in the member method of the class, you can use -> (object operator):$this->property(where property is the attribute name) to access non-static properties.
static properties are accessed using::(double colon):self::$property.

=> and ->

=>array members access symbols,->bject members access symbols;
$this->$name=$value: set the value of thenamevariable of the current class to$value;
$thisrepresents the class itself,->is the operator for accessing its class members
double colon operator (::) class name::static properties/methods
"::" is used to call the class static properties and methods

include(): includes external files, the syntax format is include (string filename);
require(): will output an error message and terminate the script
include_once(): call the same thing multiple times file, the program will only call it once
require_once(): first check whether the file has been called elsewhere
array_pop(): get and return the last element in the array
count(): count the elements in the array number
array_search(): get the key names of the elements in the array
$array_keys(): get all the key names of the repeated elements in the array

single quotes and double quotes

php treats the data in single quotes as an ordinary string and does not process it anymore. the double quotes also need to process the strings

get and post

$_get[ ] and $_post[ ] global arrays: used to receive the data passed to the current page by the get and post methods respectively. "[ ]" contains name.

there are three commonly used methods for passing parameters in php: $_post[ ], $_get[ ], $_session[ ], which are used to obtain form, url and session variables respectively. value.

the differences between get and post methods in form submission are summarized as follows:

GET是从服务器上获取数据,POST是向服务器传送数据。
GET 是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到。POST是通过HTTP POST机制,将表单内各个字段与其内容放置在HTML HEADER内一起传送到ACTION属性所指的URL地址。用户看不到这个过程。
对于GET方式,服务器端用Request.QueryString获取变量的值,对于POST方式,服务器端用Request.Form获取提交的数据。
GET传送的数据量较小,不能大于2KB(这主要是因为受URL长度限制)。POST传送的数据量较大,一般被默认为不受限制。但理论上,限制取决于服务器的处理能力。
GET 安全性较低,POST安全性较高。因为GET在传输过程,数据被放在请求的URL中,而如今现有的很多服务器、代理服务器或者用户代理都会将请求URL记 录到日志文件中,然后放在某个地方,这样就可能会有一些隐私的信息被第三方看到。另外,用户也可以在浏览器上直接看到提交的数据,一些系统内部消息将会一 同显示在用户面前。POST的所有操作对用户来说都是不可见的。

when submitting a form, if the method is not specified, the default is get request (.net default is post), the data submitted in the form will be appended to the url and separated from the url with ?. alphanumeric characters are sent as they are, but spaces are converted to " " signs and other symbols are converted to %xx, where xx is the ascii (or iso latin-1) value of the symbol in hexadecimal. the data submitted by the get request is placed in the http request protocol header, while the data submitted by post is placed in the entity data; the data submitted by get can only be up to 2048 bytes, while post does not have this limit. the parameters passed by post are in the doc, which is the text passed by the http protocol. the parameter part will be parsed when accepting. get parameters. generally it is better to use post. post submits data implicitly, get is passed in the url, and is used to pass some data that does not need to be kept confidential. get is passed in parameters in the url, and post is not.

1. the data requested by get will be appended to the url (that is, the data is placed in the http protocol header) to split the url and transmit the data. the parameters are connected with &

2. the data submitted by get method can only be up to 1024 bytes. in theory, post has no limit and can transfer a larger amount of data. the maximum is 80kb in iis4 and 100kb in iis5

http status code

the difference between cookies and sessions

the contents of cookies mainly include: name, value, expiration time, path and domain. the path and domain together form the scope of the cookie. if the expiration time is not set, it means that the lifetime of this cookie is during the browser session. if the browser window is closed, the cookie will disappear. this type of cookie that lasts for the duration of the browser session is called a session cookie.
session cookies are generally not stored on the hard disk but in memory. of course, this behavior is not specified by the specification. if an expiration time is set, the browser will save the cookies to the hard disk. if you close and open the browser again, these cookies will still be valid until the set expiration time is exceeded.

when the program needs to create a session for a client's request, the server first checks whether the client's request already contains a session identifier
(called session id). if it does, it means it has been previously if a session has been created for this client, the server will retrieve this session according to the session id
and use it (if it cannot be retrieved, a new one will be created). if the client request does not include the session id, a session will be created for this client. and generate a session id associated with this session
the value of the session id should be a string that is neither repeated nor easy to find patterns to counterfeit. this session id will be used in this response is returned to the client for storage. the method of saving this session id can use cookies, so that during the interaction process, the browser can automatically send this id to the server according to the rules.
1. the cookie data is stored on the client's browser, and the session data is stored on the server.
2. cookies are not very safe. others can analyze the cookie stored locally and conduct cookie deception
considering security, session should be used.
3. the session will be saved on the server for a certain period of time. when access increases, it will take up more of your server's performance
in order to reduce server performance, cookie should be used.
4. the data saved by a single cookie cannot exceed 4k. many browsers limit a site to save up to 20 cookies.
5. so personal suggestions:
save important information such as login information as session
if other information needs to be retained, it can be placed in cookie

php code specifications

1. variable assignments must maintain equal spacing and arrangement

2. no extra spaces are allowed at the end of each line

3. ensure the naming of the file and the calling case are consistent because unix-like systems are case-sensitive

4. method names are only allowed to be composed of letters, underscores are not allowed, and the first letter must be lowercase. the first letter of each subsequent word must be capitalized

5. attribute names are only allowed to consist of letters, and underscores are not allowed...

6. for access to object members, we the "get" and "set" methods must always be used

7. when a class member method is declared as private, it must start with a double underscore "__"; when it is declared as protected, it must start with a single underscore" _"; member attributes declared as public are not allowed to contain underscores at any time.

8. if we need to define some frequently used methods as global functions, they should be defined in the class in static form

9. naming of functions using lowercase letters and underscores should clearly describe what the function does.

10.boolean values and null values are both lowercase.

11. when a string is composed of plain text (that is, it does not contain variables), it must always use single quotes (') as the delimiter

12. use when the array type declares an associative array, it should be divided into multiple lines to ensure that the keys and values of each line are aligned

13. all code in the class must be indented with four spaces

14. it is not allowed to use var to declare variables. class member variables must be declared as private, protected and public. usually get and set methods are used to access class members.

15. methods must always use private, protected or public to declare their scope

16. no extra spaces are allowed between the function or method name and the parameter brackets

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn