search
HomeBackend DevelopmentPHP TutorialPHP常见知识点梳理小结

PHP常见知识点梳理总结

PHP方面:
1,echo print print_r的区别
Echo是PHP语句,print与print_r是函数语句没有返回值,函数可以有返回值
Print只能打印简单类型变量的值。
Print_r可以打印复杂变量值(数组或对象)
Echo 输出一个或者多个字符串。

2,mysql_fetch_array() 与mysql_fetch_row()区别
mysql_fetch_array()返回根据从结果集取得的行生成的数组,如果没有更多行,返回false,除了将数据以字段做索引外,还可以用字段名作为索引。
mysql_fetch_row() 返回从结果集中取得一行作为枚举数组,返回一个数字索引的数组,偏移量从0开始。
mysql_fetch_array() 是mysql_fetch_row()的扩展版本。

3,PHP面向对象中 _set() 与 _construct的作用
_set() ——- 用于为属性设置值, _get() 获取属性的值
_construct ——- 在一个类中只能声明一个构造方法,只有在每次创建对象的时候回去调用一次构造方法,不能主动的调用这个方法,所以通常用它执行一些有用的初始化任务。

4,PHP中session与cookie的区别
Cookie是保存在客户端的信息,是一种在远程浏览器储存数据并以此来跟踪和识别用户的机制。PHP的http协议的头信息里发送cookie,因此setcookie()函数必须在其他信息输出前调用,和header()函数的限制类似。
Session是保存在服务器端的信息,从这个角度,session比cookie更安全。当会话创建时,服务器返回客户端一个加密的session_id 以标识用户身份,浏览器关闭时,session会销毁,从而session存的值就没有了。

5,如何设置cookie并指定有效的时间
Bool setcookie(string name,string value,int expire,string path,string domain,bool secure,bool httponly)
Name : cookie变量名
Value : cookie 变量值
Expire: 有效期结束的时间
Path: 有效目录
Domain: 有效域名,顶级或唯一
Secure: 如果值为1,则cookie只能在https连接上有效,如果为默认值0,http和https都可以

Php设置cookie
$value = ‘something from somewhere’;

setcookie(“TestCookie”, $value); /* 简单cookie设置 */
setcookie(“TestCookie”, $value, time()+3600); /* 有效期1个小时 */
setcookie(“TestCookie”, $value, time()+3600, “/~rasmus/”, “.example.com”, 1); /* 有效目录 /~rasmus,有效域名example.com及其所有子域名 */
?>
使用header()设置cookie;
header(“Set-Cookie: name=$value[;path=$path[;domain=xxx.com[; ]]”);
后面的参数和上面列出setcookie函数的参数一样.

Cookie的机制原理:
a) 服务器通过随着响应发送一个http的Set-Cookie头,在客户机中设置一个cookie(多个cookie要多个头).
b) 客户端自动向服务器端发送一个http的cookie头,服务器接收读取.
HTTP/1.x 200 OK
X-Powered-By: PHP/5.2.1
Set-Cookie: TestCookie=something from somewhere; path=/
Expires: Thu, 19 Nov 2007 18:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-type: text/html

这一行实现了cookie功能,收到这行后
Set-Cookie: TestCookie=something from somewhere; path=/
浏览器将在客户端的磁盘上创建一个cookie文件,并在里面写入:
TestCookie=something from somewhere;
/
这一行就是我们用setcookie(‘TestCookie’,’something from somewhere’,’/’);的结果.也就是用header(‘Set-Cookie: TestCookie=something from somewhere; path=/’);的结果.
6,PHP面向对象中的访问控制
Public 表示全局,类内部外部子类都可以访问
Private 表示私有,只有本类内部可以调用
Protected 表示受保护的,只有本类或子类或父类可以访问

7,什么是PHP的MVC,MVC的作用及原理
MVC是一个设计模式,它强制性的使应用程序的输入,处理,输出分开,使用MVC应用程序本分成了三个核心部分:模型,视图,控制器,它们各自处理自己的任务。

MVC的原理:首先控制器接受用户的请求,并决定应该调用用哪个模型来进行处理,然后模型用业务逻辑来处理用户的请求并返回数据,然后控制器用相应的视图格式化模型返回的数据,并通过表示层呈现给用户。
基本原理:表现层(V)的请求发送到控制器(C),控制器根据请求类型调用业务层(M),最终调用表现层显示。

8,PHP中include require 的区别
这两种结构在处理失败之外有不同,include产生一个警告,而require则导致一个致命的错误,换句话说,如果你想在遇到丢失文件时停止处理页面就用require。Include就不是这样,脚本会继续执行。
Require的使用方法如require(“Myfile.php”); 这个表式通常放到PHP程式的最前面,PHP执行前,就会读入require所指定引入的档案,使它变成PHP程式网页的一部分。
Include使用方法和上面相同,这个程式一般放到流程控制的处理区段,php程式网页在读到include的档案时,才将它读进来,这种方式,可以把程式执行时的流程简单化。

当页面执行到require()时,如果require是一个PHP、HTML页面的话,这时就会马上转到去执行该页面了。而include,一般是用于include进来一些inc文件。比如说可以将你的网站的页头和页眉作为一个inc文件,这样在每个PHP文件里面再include进来。include实际只是将你要include的文件嵌入到当前页面当中。而require则是马上去执行你所请求的页面。
——————————————————————————–
incluce在用到时加载
require在一开始就加载
_once后缀表示已加载的不加载
php系统在加载php程序时有一个伪编译过程,可使程序运行速度加快。但incluce的文档仍为解释执行
include的文件中出错了,主程序继续往下执行
require的文件出错了,主程序也停了
所以包含的文件出错对系统影响不大的话(如界面文件)就用include,否则用require
include_once()函数,require_once()函数会先检查目标档案的内容是不是在之前就已经导入过了,如果是的话,便不会再次重复导入同样的内容

10,计算两个时间差 2009.5.12 2009.5.20
$regist1 = “05/12/2006″;
$regist2 = “10/05/2007″;
list($month1,$day1,$year1)= explode(“/”,$regist1);
list($month2,$day2,$year2)= explode(“/”,$regist2);
$regist1 = mktime(0,0,0,$month1,$day1,$year1);
$regist2 = mktime(0,0,0,$month2,$day2,$year2);
$time_difference = $regist2-$regist1;

11,你了解的协议有哪些?HTTP协议的错误提示代表什么?
SMTP(Simple Mail Transfer Protocal)称为简单邮件传输协议,目标是向用户提供高效、可靠的邮件传输。
POP的全称是 Post Office Protocol ,即邮局协议,用于电子邮件的接收,它使用TCP的110端口,现在常用的是第三版,所以简称为 POP3
IMAP是Internet Message Access Protocol的缩写,顾名思义,主要提供的是通过Internet获取信息的一种协议。
HTTP(HyperTextTransferProtocol)是超文本传输协议的缩写,它用于传送WWW方式的数据,关于HTTP协议的详细内容请参考RFC2616。
IE提示 HTTP 403 – 禁止访问
IE提示 HTTP 403.9 – 禁止访问:连接的用户过多
IE提示 HTTP 404 – 无法找到文件
IE提示 HTTP 500 – 内部服务器错误

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
PHP: An Introduction to the Server-Side Scripting LanguagePHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP and the Web: Exploring its Long-Term ImpactPHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

Why Use PHP? Advantages and Benefits ExplainedWhy Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

Debunking the Myths: Is PHP Really a Dead Language?Debunking the Myths: Is PHP Really a Dead Language?Apr 16, 2025 am 12:15 AM

PHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.

The PHP vs. Python Debate: Which is Better?The PHP vs. Python Debate: Which is Better?Apr 16, 2025 am 12:03 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.

PHP's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.