Home  >  Article  >  Backend Development  >  Summary of some common problems in PHP (collection)

Summary of some common problems in PHP (collection)

不言
不言Original
2018-09-10 14:18:571779browse

The content of this article is a summary (collection) of some common problems in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. What is the difference between single quotes and double quotes when defining a string?

Single quotes load faster than double quotes

2. What are the differences between echo(), print(), and print_r()?

(1) echo is a syntax, Output one or more strings, no return value;
(2) print is a function, cannot output arrays and objects, Outputastring, print
has a return Value;
(3)print_r is a function that can output an array. print_r is an interesting function. It can output string, int, float, array, object, etc. When outputting array, it will be represented by a structure. print_r returns true when the output is successful; and print_r can be passed print_r($str,true), so that print_r Returns the value processed by print_r without outputting it. In addition, for echo and print, echo is basically used because it is more efficient than print.

3. What are the string processing functions according to functional classification? What do these functions do?

A. String output function
(1)echo $a,$b,$c...; is a language structure, not a real
function.
(2)print($a) This function outputs a string. If successful, return 1, if failed, return 0
(3)print_r($a)
(4)var_dump($a); Can output type, length, value
B. Remove spaces from the beginning and end of the string Function: trim ltrim rtrim (alias: chop) Using the second parameter, you can also remove specified characters.
C. Escape string function: addslashes()
D. Get string length function: strlen()
E. Intercept string length function: substr()
F. Retrieve string functions: strstr(), strpos()
G. Replace string functions: str_replace()

4. Please give the correct answers to the following questions?

1).$arr = array('james', 'tom', 'symfony'); Please split the value of the $arr array with ',' and merge it into a string Output?

echo implode(‘,’,$arr);

2).$str = 'jack,james,tom,symfony'; Please split $str with ',' and put the split value in $arr array?

$arr = explode(‘,’,$str);

3).$arr=array(3,7,2,1,'d','abc'); Please sort $arr from large to small sort order and keep their key values ​​unchanged?

arsort($arr);
print_r($arr);

4).$mail = “gaofei@163.com”; Please take out the domain of this mailbox (163.com) and print it to see how many types can be written at most method?

echostrstr($mail,'163');
echosubstr($mail,7);
$arr=explode("@",$mail);echo$arr[1];

5. The characters on the page are garbled, how to solve it?

1. First consider whether the current file has a character set set . Check whether charset is written in the meta tag. If it is a php page, you can also check whether
charset is specified in the header() function;
For example:

<meta http-equiv="Content-Type" content="text/html;
charset=utf-8"/>
header(“content-type:text/html;charset=utf-8”);

2. If the character set (that is, charset) is set, then determine whether the encoding format saved in the current file
is consistent with the character set set on the page.
The two must be consistent;
3. If it involves extracting data from the database, then determine whether the
character set when querying the database is consistent with the character set set on the current page. The two must be unified,
For example:

mysql_query(“set names utf8”)。

6. 正则表达式是什么?php 中有哪些常用的跟正则相关的 函数?请写出一个 email 的正则,中国手机号码和座机号码的正则表达式?

正则表达式是用于描述字符排列模式的一种语法规则。正则表达式也叫做模式表达式。网站开发中正则表达式最常用于表单提交信息前的客户端验证。
比如验证用户名是否输入正确,密码输入是否符合要求, email、手机号码等信息的输入是否合法
在 php 中正则表达式主要用于字符串的分割、匹配、查找和
替换操作。
preg 系列函数可以处理。具体有以下几个:
stringpreg_quote(stringstr[,stringdelimiter])
转义正则表达式字符 正则表达式的特殊字符包括:.\\+*? [^]$(){}=!a8093152e673feb7aba1828c43532094|:。
preg_replace-- 执行正则表达式的搜索和替换
mixed preg_replace ( mixed pattern, mixed replacement, mixed subject[,intlimit]
preg_replace_callback -- 用回调函数执行正则表达式的搜索
和替换
mixed preg_replace_callback ( mixed pattern, callback callback, mixedsubject[,intlimit])
preg_split-- 用正则表达式分割字符串
array preg_split ( string pattern, string subject [, int limit [, int flags]])
常用的正则表达式写法:
中文:/^[\u4E00-\u9FA5]+$/
手机号码:/^(86)?0?1\d{10}$/
EMAIL:
/^[\w-]+[\w-.]?@[\w-]+\.{1}[A-Za-z]{2,5}$/
密码(安全级别中) :
/^(\d+[A-Za-z]\w*|[A-Za-z]+\d\w*)$/
密码(安全级别高) :
/^(\d+[a-zA-Z~!@#$%^&(){}][\w~!@#$%^&(){}]*|[a-zA-Z~! @#$%^&(){}]+\d[\w~!@#$%^&(){}]*)$/

7.preg_replace()和 str_ireplace()两个函数在使用上有什么 不同?preg_split()和 split()函数如何使用?

preg_replace — 执行正则表达式的搜索和替换
str_ireplace — str_replace() 的 忽 略 大 小 写 版 本 str_replace — 子字符串替换
preg_split — 用正则表达式分割字符串
split — 用正则表达式将字符串分割到数组中

8. 获取当前时间戳的函数主要有哪些?用 PHP 打印出今 天的时间,格式是2010-12-10 22:21:21?用 PHP 打印出前一天的时间格式是2010-12-10 22:21:21? 如何把2010-12-25 10:30:25变成 unix 时间戳?

echodate("Y-m-dH:i:s",strtotime(‘-1,days’));
date(&#39;Y-m-dH:i:s&#39;,time());
$unix_time = strtotime("2009-9-2 10:30:25");//变成 unix 时间戳
echodate("Y-m-dH:i:s",$unix_time);//格式化为正常时间格式

9.在 url 中用 get 传值的时候,若中文出现乱码,应该用哪个函数对中文进行编码?

用户在网站表单提交数据的时候,为了防止脚本攻击(比如 用户输入3f1c4e4b6b16bbbd69b2ee476dc4f83aalert(111);2cacc6d41bbb37262a98f745aa00fbf0),php 端接收数据的
时候,应该如何处理?
使用 urlencode()对中文进行编码,使用 urldecode()来解码。
使用 htmlspecialchars($_POST[‘title’])来过滤表单传参就可以避免脚本攻击。

10. 说 说 mysql_fetch_row() 和 mysql_fetch_assoc() 和 mysql_fetch_array 之间有什么区别?

第一个是返回结果集中的一行作为索引数组,第二个是返回
关联数组,而第三个既可以返回索引数组也可以返回关联数 组,取决于它的第二个参数 MYSQL_BOTH MYSQL_NUM MYSQL_ASSOC 默认为 MYSQL_BOTH

$sql=”select*fromtable1”;
$result=mysql_query($sql);
mysql_fetch_array($result,MYSQL_NUM);

11. 请说出目前学过的返回是资源的函数?

答:fopen(打开文件)
imagecreatefromjpeg(png gif) — 从 JPEG
文件新建一图像
imagecreatetruecolor — 新建一个真彩色
图像
imagecopymerge — 拷贝并合并图像的一
部分
imagecopyresized — 拷贝部分图像并调
整大小
mysql_connect — 打开一个到 MySQL MySQL MySQL MySQL
服务器的连接
mysql_query();只有这执行 select 的时候成功,才返回资源, 失败返回 FALSE

12. 文件上传需要注意哪些细节?怎么把文件保存到指定目录?怎么避免上传文件重名问题?

1.首现要在 php.ini 中开启文件上传;
2.在 php.ini 中有一个允许上传的最大值,默认是2MB。必要
的时候可以更改;
3. 上 传 表 单 一 定 要 记 住 在 form 标 签 中 写 上 enctype="multipart/form-data";
4. 提交方式 method 必须是 post;
5. 设定 type="file" 的表单控件;
6.要注意上传文件的大小 MAX_FILE_SIZE、文件类型是否符合要求,上传后存放的路径是否存在。可以通过上传的文件名获取到文件后缀,然后使用时间戳+文件后缀的方式为文件重新命名,这样就避免了重名。可以自己设置上传文件的保存目录,与文件名拼凑形成一个文件 路径,使用 move_uploaded_file(),就可以完成将文件保存到指定目录。

13. $_FILES 是几维数组?第一维和第二维的索引下标分别是什么?批量上传文件的时候需要注意什么?

二维数组。第一维是上传控件的 name,二维下标分别为 name/type/tmp_name/size/error.

14.header()函数主要的功能有哪些?使用过程中注意什么?

答:

header()发送 http 头信息
-header("content-type:text/html; charset=utf-8");-------------------//当前页面输出内容是 html,编 码为 utf-8格式
-<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
-header("content-type:image/png gif jpeg");----------------------------------//当前页面输出内容的格式是图片
-header("refresh:5;url=http://www.1004javag.com/five/string.ph p");--//页面5秒后要跳转到新网址
-header("location:http://1004javag.com/five/string.php");----------//页面重定向

15. 文件下载的时候如果使用 header()函数?

答 : header("content-type: application/octet-stream;charset=UTF-8"); //在这里加 utf-8和
在上面定义有什么区别?、??

header("accept-ranges:bytes");
header("accept-length: ".filesize($filedir.$filename));
header("content-disposition: attachment; filename=".$filedir.$filename);

16. 什么是 ajax?ajax 的原理是什么?ajax 的核心技术是什 么?ajax 的优缺点是什么?

ajax 是 asynchronous JavaScript JavaScript JavaScript JavaScript and xml 的 缩 写 , 是 javascript、xml、css、DOM 等多个技术的组合。'$'是 jQuer jQuer jQuer jQuery y y y 的别名.
页面中用户的请求通过 ajax 引擎异步地与服务器进行通 信,服务器将请求的结果返回给这个 ajax 引擎,
最后由这个ajax引擎来决定将返回的数据显示到页面中的 指定位置。Ajax 最终实现了在一个页面的指定位置可以加载 , 另一个页面所有的输出内容。
这样就实现了一个静态页面也能获取到数据库中的返回数 据信息了。所以 ajax 技术实现了一个静态网页在不刷新整个
页面的情况下与服务器通信,
减少了用户等待时间,同时也从而降低了网络流量,增强了
客户体验的友好程度。
Ajax 的优点是:
1. 减轻了服务器端负担,将一部分以前由服务器负担的工
作转移到客户端执行,利用客户端闲置的资源进行处理;
2. 在只局部刷新的情况下更新页面,增加了页面反应速度,
使用户体验更友好。
Ajax 的缺点是不利于 seo 推广优化,因为搜索引擎无法直接 访问到 ajax 请求的内容。
ajax 的核心技术是 XMLHttpRequest,它是 javascript 中的
一个对象。

17.jquery 是什么?jquery 简化 ajax 后的方法有哪些?

jQuery 是 Javascript 的一种框架。
$.get(),$.post(),$.ajax()。$是 jQuery 对象的别名。
代码如下:

$.post(异 步 访 问 的 url 地 址 , {&#39; 参 数 名&#39; : 参 数 值} , function(msg){
$("#result").html(msg);
});
$.get( 异 步 访 问 的 url 地 址 , {&#39; 参 数 名 &#39; : 参 数 值 } , function(msg){
$("#result").html(msg);
});
$.ajax({
type:"post",
url:loadUrl,
cache:false,
data:"参数名=" + 参数值,
success:function(msg){
$("#result").html(msg);
}
});

18. 什么是会话控制?

简单地说会话控制就是跟踪和识别用户信息的机制。会话控制的思想就是能够在网站中跟踪一个变量,通过这个变量,系统能识别出相应的用户信息,根据这个用户信息可以得知用户权限,从而展示给用户适合于其相应权限的页面内容。 目前最主要的会话跟踪方式有 cookie,session。

19. 会话跟踪的基本步骤

1).访问与当前请求相关的会话对象

2). Find information related to the session

3). Store session information

4). Discard session data

20. What are the precautions for using cookies?

1) There cannot be any page output before setcookie(), even spaces, and empty white lines are not allowed;
2) After setcookie(), there will be no output when calling $_COOKIE['cookiename'] on the current page. You must refresh or go to the next page to see the cookie value;
3) Different browsers handle cookies differently. The client can disable cookies, and the browser can also idle the number of cookies. A browser can create up to 300 cookies, and each cookie cannot exceed 4kb, The total number of cookies that can be set by each web site cannot exceed 20.
4) Cookies are stored on the client side. If the user disables cookies, setcookie will not work. So don't rely too much on cookies.

21. When using session, what is used to represent the current user to distinguish it from other users?

sessionid, the current session_id can be obtained through the session_id() function.

22. What are the steps to use session and cookie? What is the life cycle of sesssion and cookie? What is the difference between session and cookie?

1. Cookies are stored on the client machine. For cookies with no expiration time set, the cookie value will be stored in the machine's memory. As long as the browser is closed, the cookie will disappear automatically. If the cookie expiration time is set, the browser will save the cookie to the hard disk in the form of a text file, and the cookie value will still be valid when the browser is opened again.
2. Session is to save the information that the user needs to store on the server side. Each user's session information is stored on the server side like a key-value pair, where the key is the sessionid and the value is the information the user needs to store. The server uses sessionid to distinguish which user the stored session information belongs to. The biggest difference between the two is that session is stored on the server side, while cookie is on the client side. Session security is higher, while cookie security is weak.
3. Session plays a very important role in web development. It can record the user
's correct login information into the server's memory. When the user accesses the management backend of the website with this identity, he or she can get identity confirmation without logging in again. Users who have not logged in correctly will not allocate session space, and cannot see the page content even if they enter the access address of the management background. The user's operation permissions on the page are determined through the session. Steps to use session:

1. Start session: Use the session_start() function to start.

2. Register session: Just add elements to the $_SESSION array directly.
3. Use session: Determine whether session is empty or registered. If already exists, use it like an ordinary array.
4. Delete session:
1. You can use unset to delete a single session;
2. Use $_SESSION=array() method, log out all session variables at once;
3. Use the session_destroy() function to completely destroy the session. How to use cookies?

1. Record part of the information visited by the user

2. Pass variables between pages

3. Internet pages are stored in cookies in temporary folders, which can
improve future browsing speeds.

创建 cookie:setcookie(stringcookiename,stringvalue,int expire);
读取 cookie:通过超级全局数组$_COOKIE 来读取浏览器端 的 cookie 的值。
删除 cookie:有两种方法
1.手工删除方法:
右击浏览器属性,可以看到删除 cookies,执行操作即可将所 有 cookie 文件删除。
2.setcookie()方法:
跟设置 cookie 的方法一样,不过此时将 cookie 的值设置为 空,有效时间为0或小于当前时间戳。

 23. 设置或读取 session 之前,需要做什么?

可以直接在php.ini中开启session.auto_start=1或者在页面 头部session_start();
开启 session,session_start()前面不能有任何输出,包括空行。

24. 在实际开发中,session 在哪些场合使用?

session 用来存储用户登录信息和用在跨页面传值。
1)常用在用户登录成功后,将用户登录信息赋值给 session;
2)用在验证码图片生成,当随机码生成后赋值给 session。

25. 注销 session 会话的形式有几种?

unset() $_SESSION=array(); 
session_destroy();

 相关推荐: 

php服务nginx不能使用file_get_contents的解决方法

php输出中文页面时出现中文乱码的解决方案

The above is the detailed content of Summary of some common problems in PHP (collection). For more information, please follow other related articles on the PHP Chinese website!

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

Related articles

See more