Basic knowledge of php study notes, php study notes_PHP tutorial

WBOY
Release: 2016-07-13 10:14:45
Original
837 people have browsed it

Basic knowledge of php study notes, php study notes

I have been studying PHP for more than a year now. I have accumulated a lot of notes and they are quite complicated. I will write an article to sort them out.

PHP basic part

<font face="新宋体">PHP</font> Basic commands for outputting text: <font face="新宋体">echo</font> and <font face="新宋体">print</font>.

The difference between echo and print

<font face="新宋体">echo</font> is a PHP statement, <font face="新宋体">print</font> and <font face="新宋体">print_r</font> are functions. Statements have no return value, but functions can have return values ​​(even if they are useless)

<font face="新宋体">echo</font> Output one or more strings.
<font face="新宋体">print</font> Only the values ​​of simple type variables (such as int, string) can be printed out
<font face="新宋体">print_r</font> You can print out the values ​​of complex type variables (such as arrays, objects)

The difference between var_dump and print_r

<font face="新宋体">var_dump</font> returns the type and value of the expression, while <font face="新宋体">print_r</font> only returns the result, which is easier to read than using <font face="新宋体">var_dump</font> for debugging code.

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:

<code>local(局部)<br>global(全局)<br>static(静态)</code>
Copy after login
Variables declared outside

functions have <font face="新宋体">Global</font> scope and can only be accessed outside the function.

Variables declared inside a function have <font face="新宋体">LOCAL</font> scope and can only be accessed inside the function.

<font face="新宋体">global</font> keyword is used to access global variables within a function.

PHP static keywords

Normally all variables are deleted when the function completes/executes. 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:

<code><&#63;php    <br />function myTest() {<br />   static $x=-1;<br />   echo $x;<br />   $x--;<br />}<br />myTest();//-1<br />echo "<br>";<br>myTest();//-2<br>echo "<br>";<br>myTest();//-3<br>?></code>
Copy after login

php type

<code>php类型:**PHP 支持八种原始类型。** </code>
Copy after login

Boolean

To specify a Boolean value, use the keywords TRUE or FALSE. Both are case-insensitive.

integer type

We can use (int) to cast the decimal to an integer.

<code><&#63;php<br />    var_dump((int)(26/3));//int(8)<br />&#63;></code>
Copy after login

Array

There are three types of arrays in php:

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

The subscript is either an integer or a string.

<code><&#63;php<br />$array = array(<br />    "foo" => "bar",<br>    "bar" => "foo",<br>);<br>// 自 PHP 5.4 起<br>$array = [<br>    "foo" => "bar",<br>    "bar" => "foo",<br>];<br>?></code>
Copy after login

Array cells can be accessed via the <font face="新宋体">array[key]</font> syntax.
Note: This does not mean always quoting key names. There is no need to quote the key names of constants or variables, otherwise <font face="新宋体">PHP</font> will not be able to parse them.

Array operator

example name result $a + $b union of $a and $b $a == $b equal TRUE if $a and $b have the same key/value pair $a === $b congruent if $a and TRUE if $b has the same key/value pairs in the same order and type $a != $b is not equal TRUE if $a is not equal to $b $a <> $b is not equal if $a is not TRUE if $a is equal to $b !== $b is not equal if $a is not equal to $b The

<font face="新宋体">+</font> operator appends the array elements on the right to the array on the left. If the key names are in both arrays, 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.

Common functions

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

Constant

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

Example of defining constants:

<code><&#63;php<br />define("poems" , "Homeric epic");<br />echo poems ;//outputs "Homeric epic" <br />&#63;></code>
Copy after login

php string operators

In PHP, there is only one string operator.
The concatenation operator <font face="新宋体">(.)</font> is used to concatenate two string values. Such as: echo "a= ".$a."<br>";
The left side connects the string literal "a=" to the value of the variable $a, and the second place connects it to the newline character <code><font face="新宋体">"<br>"</font>"
"

php function

函数只有在被调用时才会被执行,这点和js是一样的,同样,函数定义也是以function关键字开头的。

<code><&#63;php<br />    function sum($x,$y){<br />        $z=$x + $y;<br />        return $z;<br />    }<br />    echo "-2+10= ".sum(-2,10);//outputs "-2+10=8"<br />&#63;></code>
Copy after login

当没有<font face="新宋体">return</font>语句时,以上将会变成"-2+10=";

流程控制

在这里,只讲下<font face="新宋体">foreach</font>语句。

<font face="新宋体">foreach</font>语句遍历输出数组:
语法:

<code>foreach (array_expression as $value){ statement};
foreach (array_expression as $key => $value){ statement};</code>
Copy after login

参数<font face="新宋体">array_expression</font>是指定要遍历的数组,<font face="新宋体">$value</font>是数组的值

<code><?php<br>     $actors [0] ="Marry";<br>     $actors [1] ="Lorry";<br>     $actors [2] = "mike";<br>     foreach ($actors as $values){<br>     echo "Name:$values<br>"; <br>}<br>?></code>
Copy after login

以上代码将输出:
Name:Marry
Name:Lorry
Name:mike

两个重要的魔术方法

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

php中方法不区分大小写

<code>require(dirname(__FILE__).'/global.php'); //引入全局文件
require(dirname(__FILE__).'/config.ini.php'); //引入基本配置文件</code>
Copy after login

对象运算符和双冒号运算符

在类的成员方法里面,可以用 ->(对象运算符):<font face="新宋体">$this->property</font>(其中 property 是该属性名)这种方式来访问非静态属性。
静态属性则是用 <font face="新宋体">::</font>(双冒号):<font face="新宋体">self::$property</font> 来访问。

=>和->

<font face="新宋体">=></font>数组成员访问符号,<font face="新宋体">-></font>对象成员访问符号;
<font face="新宋体">$this</font>-<font face="新宋体">>$name=$value</font>:将当前类的<font face="新宋体">name</font>变量的值设为<font face="新宋体">$value</font>;
<font face="新宋体">$this</font>代表了类本身,<font face="新宋体">-></font>是访问其类成员的操作符
双冒号运算符(<font face="新宋体">::</font>)类名<font face="新宋体">::</font>静态属性/方法
<font face="新宋体">::</font>”用来调用类中静态的属性和方法

<font face="新宋体">include()</font>:包含外部文件,语法格式为 include(string filename);
<font face="新宋体">require()</font>:会输出错误信息,终止脚本
<font face="新宋体">include_once()</font>:多次调用相同文件时,程序只会调用一次
<font face="新宋体">require_once()</font>:先检查文件是否在其他地方被调用过
<font face="新宋体">array_pop()</font>:获取并返回数组中的最后一个元素
<font face="新宋体">count()</font>:统计数组中的元素个数
<font face="新宋体">array_search()</font>:获取数组中元素的键名
<font face="新宋体">$array_keys()</font>:获取数组中重复元素的所有键名

单引号和双引号

PHP把单引号中的数据视为普通字符串,不再处理。而双引号还要对其中的字符串进行处理

get和post

$_GET[ ]和$_POST[ ]全局数组:分别用来接收GET和POST方法传递到当前页面的数据。“[ ]”里面是name.

php参数传递常用的方法有3种:$_POST[ ]、$_GET[ ]、$_SESSION[ ],分别用于获取表单、URL与Session变量的值。

表单提交中get和post方式的区别归纳如下几点:

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

在FORM提交的时候,如果不指定Method,则默认为 GET请求(.net默认是POST),Form中提交的数据将会附加在url之后,以?分开与url分开。字母数字字符原样发送,但空格转换为“+” 号,其它符号转换为%XX,其中XX为该符号以16进制表示的ASCII(或ISO Latin-1)值。GET请求请提交的数据放置在HTTP请求协议头中,而POST提交的数据则放在实体数据中;GET方式提交的数据最多只能有2048字节,而POST则没有此限制。POST传递的参数在doc里,也就http协议所传递的文本,接受时再解析参数部分。获得参数。一般用POST比较好。POST提交数据是隐式的,GET是通过在url里面传递的,用来传递一些不需要保密的数据,GET是通过在URL里传递参数,POST不是。

1.GET请求的数据会附在URL之后(就是把数据放置在HTTP协议头中),以?分割URL和传输数据,参数之间以&相连

2.GET方式提交的数据最多只能是1024字节,理论上POST没有限制,可传较大量的数据,IIS4中最大为80KB,IIS5中为100KB

HTTP状态码

cookie和session区别

The content of the cookie mainly includes: 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 this
The lifetime of a cookie is during the browser session. When the browser window is closed, the cookie disappears. 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 the expiration time is set, the browser will save the cookie
Save to your hard drive, close and reopen the browser, these cookies will remain valid until the set expiration time expires.

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 is included, it means that a session has been created for this client before, and the server will retrieve this session according to the session id
Use (cannot be retrieved, a new one will be created), if the client request does not contain a session id, create a session for this client and generate a session corresponding to this session
The associated session id. The value of the session id should be a string that is neither repetitive nor easy to find patterns to counterfeit. This session id will be used in this response
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
according to the rules. server.
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 cookies stored locally and deceive them
Session should be used for security reasons.
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
Taking into account the reduction of 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 my personal suggestion:
Store 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. Make sure the file naming and calling case are consistent because Unix-like systems are case-sensitive

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

5. Attribute names are only allowed to consist of letters, underscores are not allowed⋯⋯

6. For accessing object members, we must always use the "get" and "set" methods

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 always Underscores are not allowed.

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

9. Use lowercase letters and underscores in the name of the function, and it should be able to clearly describe the function of the function.

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. When using the array type to declare an associative array, it should be divided into multiple rows to ensure that the keys and values ​​of each row 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 parameter brackets

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/908173.htmlTechArticleBasic knowledge of PHP learning notes, PHP learning notes. I have been studying PHP for more than a year, and I have accumulated a lot of notes. It’s quite complicated, so I’ll write an article to sort it out. PHP basic part PHP output text...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template