Summary of basic knowledge of php learning

不言
Release: 2023-03-22 16:22:02
Original
3396 people have browsed it

This article mainly shares with you the basic knowledge of PHP learning. The content is very simple. I hope it will be helpful to friends who are just starting to learn PHP.

I have been studying PHP for more than a year, and I have accumulated a lot of notes, which are also quite complicated. Let me write an article to sort them out.

php basic part

##PHP<span style="font-family:新宋体"></span> Basic instructions for outputting text:echo<span style="font-family:新宋体"></span> and print<span style="font-family:新宋体"></span>.

The difference between echo and print

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

echo<span style="font-family:新宋体"></span> Output one or more strings.
print<span style="font-family:新宋体"></span> can only print out the value of simple type variables (such as int, string)
print_r<span style="font-family:新宋体"></span> can print out Values ​​of complex type variables (such as arrays, objects)

The difference between var_dump and print_r

var_dump<span style="font-family:新宋体"></span>Returns the type and value of the expression, while print_r<span style="font-family:新宋体"></span>Only returns the result, which is easier to read than using var_dump<span style="font-family:新宋体"></span> 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 the function have the

Global<span style="font-family:新宋体"></span> scope, which can only be outside the function for a visit.

Variables declared inside a function have

LOCAL<span style="font-family:新宋体"></span> scope and can only be accessed inside the function.

global<span style="font-family:新宋体"></span> keyword is used to access global variables within a function.

PHP static keyword

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><?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

php类型:**PHP 支持八种原始类型。**
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 decimals into integers.

<code><?php<br/>    var_dump((int)(26/3));//int(8)<br/>?></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><?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 through the

array[key]<span style="max-width:90%"></span> syntax. Note: This does not mean always quoting key names. There is no need to put quotes around the key names of constants or variables, otherwise
PHP<span style="font-family:新宋体"></span> will not be able to parse them.

Array operators

例子名称结果$a + $b 联合 $a 和 $b 的联合$a == $b 相等 如果 $a 和 $b 具有相同的键/值对则为 TRUE$a === $b 全等 如果 $a 和 $b 具有相同的键/值对并且顺序和类型都相同则为 TRUE$a != $b 不等 如果 $a 不等于 $b 则为 TRUE$a <> $b 不等 如果 $a 不等于 $b 则为 TRUE$a !== $b 不全等 如果 $a 不全等于 $b 则为 TRUE

<span style="font-family:新宋体">+</span> 运算符把右边的数组元素附加到左边的数组后面,两个数组中都有的键名,则只用左边数组中的,右边的被忽略。

对象

要初始化一个对象,用 new 语句将对象实例到一个变量中。

常用函数

strlen() 函数用于计算字符串的长度。
strpos() 函数用于在字符串内检索一段字符串或一个字符。

常量

可以用 define() 函数来定义常量。一个常量一旦被定义,就不能再改变或者取消定义。
常用的魔术常量:

定义常量例子:

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

php字符串运算符

在 PHP 中,只有一个字符串运算符。
并置运算符 <span style="max-width:90%">(.)</span> 用于把两个字符串值连接起来。如:echo "a= ".$a."<br>";
左边将字符串文字"a="与变量$a的值连接,第二处是与换行符<span style="font-family:新宋体">"<br>"</span>连接

php函数

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

<code><?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/>?></code>
Copy after login

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

流程控制

在这里,只讲下<span style="font-family:新宋体">foreach</span>语句。

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

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

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

<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

两个重要的魔术方法

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

php中方法不区分大小写

require(dirname(__FILE__).&#39;/global.php&#39;); //引入全局文件
require(dirname(__FILE__).&#39;/config.ini.php&#39;); //引入基本配置文件
Copy after login

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

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

=>和->

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

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

单引号和双引号

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区别

cookie的内容主要包括:名字,值,过期时间,路径和域。路径与域一起构成cookie的作用范围。若不设置过期时间,则表示这
个cookie的生命期为浏览器会话期间,关闭浏览器窗口,cookie就消失。这种生命期为浏览器会话期的cookie被称为会话cookie。
会话cookie一般不存储在硬盘上而是保存在内存里,当然这种行为并不是规范规定的。若设置了过期时间,浏览器就会把cookie
保存到硬盘上,关闭后再次打开浏览器,这些cookie仍然有效直到超过设定的过期时间。

当程序需要为某个客户端的请求创建一个session时,服务器首先检查这个客户端的请求里是否已包含了一个session标识
(称为session id),如果已包含则说明以前已经为此客户端创建过session,服务器就按照session id把这个session检索出来
使用(检索不到,会新建一个),如果客户端请求不包含session id,则为此客户端创建一个session并且生成一个与此session相
关联的session id,session id的值应该是一个既不会重复,又不容易被找到规律以仿造的字符串,这个session id将被在本次响应
中返回给客户端保存。保存这个session id的方式可以采用cookie,这样在交互过程中浏览器可以自动的按照规则把这个标识发送给
服务器。
1、cookie数据存放在客户的浏览器上,session数据放在服务器上。
2、cookie不是很安全,别人可以分析存放在本地的COOKIE并进行COOKIE欺骗
考虑到安全应当使用session。
3、session会在一定时间内保存在服务器上。当访问增多,会比较占用你服务器的性能
考虑到减轻服务器性能方面,应当使用COOKIE。
4、单个cookie保存的数据不能超过4K,很多浏览器都限制一个站点最多保存20个cookie。
5、所以个人建议:
将登陆信息等重要信息存放为SESSION
其他信息如果需要保留,可以放在COOKIE中

php代码规范

1. Variable assignments must maintain equal spacing and arrangement

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

3. Make sure that the file naming and calling case are consistent. 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 access to 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 not allowed to contain Underline.

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

9. Use lowercase letters for function names and underscores 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), single quotes (') must always be used as the delimiter

12. Use the array type When declaring 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

Related recommendations:

Detailed explanation of four basic PHP algorithms

Detailed explanation of PHP basic knowledge

Detailed explanation of PHP basic function examples

The above is the detailed content of Summary of basic knowledge of php learning. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!