php基础编程常见问题汇总
本版常见问题的汇总,限于篇幅答案可能不够详尽,需要更多答案可以充分利用搜索引擎。
本帖内容将陆续更新增加。希望能够帮助更多需要的新手,如果有错误敬请各位同学指正。
1.如何判断php是否安装成功,以及输出php的相关配置环境参数?
答:1.新建一个php文件,例如文件名:phpinfo.php
2.在php文件内添加代码
3.在浏览器打开这个文件,localhost/phpinfo.php
如果正常出现一个详细的文档那么安装成功,如果直接输出了上面的源码或者提示下载文件为不成功。
2.如何打开php的错误提示功能?默认是关闭状态
答:php.ini文件搜索字段error_reporting 修改成 error_reporting = E_ALL
display_errors 修改成 display_errors = On 如果字段前有# 请删除这个注释符号。
需要重启apache,即可生效.也可以在php文件添加代码 error_reporting(E_ALL);
3.出现类似 "Notice: Undefined index:xxx in..."的错误提示
答:此为5.3(如果没记错)版本后的一个新变化,这并不是一个错误,仅仅是一个提示。
提示内容是变量未声明就使用了。
解决的方法可以是:
a.关闭出错提示(或修改出错提示等级)。参考常见问题2 修改 display_errors = Off
b.在提交的参数 例如 $_POST['test']赋值之前添加 if (issset($_POST['test']))判断,其他
变量提示可以用初始化赋值的方式代替.例如 $test='';
4.数据库或者页面输出中文乱码,但是英文以及数字没有任何问题
答:一般这种问题都是编码引起的,所以保持所有编码一致就是解决问题的方法.
以gbk为例
php文件头输出 header ( "Content-Type: text/html; charset=gbk" );
php链接数据库之后执行 mysql_query("SET NAMES 'gbk'");
数据库/表编码设置为gbk
文件编码 检查是否为ANSI
5.如何搭建php 的运行工作环境?
答:我们将会有一个详细的帖子来介绍这个方面的内容,如果是刚开始学习的同学推荐你使用集成
的安装包来学习,由于php版本比较多所以如果仅仅参考网上的教材很容易造成学习的困惑。
请直接下载以下相应系统的完整安装包,运行安装,配置问题请参考官方的帮助文档。
Windows:http://www.apachefriends.org/zh_cn/xampp-windows.html
Linux:http://www.apachefriends.org/zh_cn/xampp-linux.html
6.为什么我看到有的代码里用 ?>就可以,而不需要这样
答:在php.ini中有一个短标记的选项,打开即可short_open_tag = On 记得如果前面有# 请删除这个注释符号。
个人并不推荐打开这个功能,请按照的方式来书写代码.
7.常用php的开发工具有哪些?
答:如果你喜欢看起来很专业功能繁多的开发工具
eclipse zendstudio phpdesinger phpstorm netbeans ...
如果你喜欢朴素,原始的开发工具
记事本 notepad++ editplus ....
Linux下基本上上面所列IDE都有,朴素的可用Vim Gvim ...
8.在浮点数运算的过程中出现“灵异”现象?
答:Warning
浮点数精度
显然简单的十进制分数如同 0.1 或 0.7 不能在不丢失一点点精度的情况下转换为内部二进制的格式。这就会造成混乱的结果:例如,floor((0.1+0.7)*10) 通常会返回 7 而不是预期中的 8,因为该结果内部的表示其实是类似 7.9999999999...。
这和一个事实有关,那就是不可能精确的用有限位数表达某些十进制分数。例如,十进制的 1/3 变成了 0.3333333. . .。
所以永远不要相信浮点数结果精确到了最后一位,也永远不要比较两个浮点数是否相等。如果确实需要更高的精度,应该使用任意精度数学函数或者 gmp 函数
9.UTF8编码的文件session_start(),header(),settcookie()等函数出错,提示"headers already sent "。
答: UTF8编码通常的编辑器都会在文件头部加上三字节的BOM编码来识别UTF8编码格式,这三个字节是普通文件编辑器看不到,而输出时去先行以HTML输出了。执行以上函数时就会提示以上错误。解决办法:用editplus等可以清除BOM的编辑器,清除BOM(设置为utf8清除BOM)保存一下即可。
10.mysql和php编码应该注意的一个地方.
答:需要提醒大家的是 mysql 里 SET NAMES 'UTF8';这里是没有横杠的,而php 的正确写法是 UTF-8,很多初学者在这个地方容易弄混和不解,希望特别注意这个小小的区别。
------解决方案--------------------
看了,学习学习,谢谢牛哥
------解决方案--------------------
支持一下。。 妞妞还没放假呢!
------解决方案--------------------
早看到这个帖子就好了呵呵
------解决方案--------------------
学习了 列出了我见到的全部问题啊 呵呵

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor
