Home Backend Development PHP Tutorial 最令PHP菜鸟头痛的十四个有关问题

最令PHP菜鸟头痛的十四个有关问题

Jun 13, 2016 am 11:01 AM
file name php session

最令PHP初学者头痛的十四个问题
【1】页面之间无法传递变量 get,post,session在最新的php版本中自动全局变量是关闭的,所以要从上一页面取得提交过来得变量要使用$_GET['foo'],$_POST['foo'],$_SESSION['foo']来得到。当然也可以修改自动全局变量为开(php.ini改为register_globals = On);考虑到兼容性,还是强迫自己熟悉新的写法比较好。

  【2】Win32下apache2 用get方法传递中文参数会出错:

  test.php?a=你好&b=你也好

  传递参数是会导致一个内部错误
 
  解决办法:"test.php?a=".urlencode(你好)."&b=".urlencode(你也好)

   .............

  【3】win32下的session不能正常工作

  php.ini默认的session.save_path = /tmp

  这显然是linux下的配置,win32下php无法读写session文件导致session无法使用,把它改成一个绝对路径就可以了,例如session.save_path = c:windows emp

  【4】显示错误信息

  当php.ini的display_errors = On并且error_reporting = E_ALL时,将显示所有的错误和提示,调试的时候最好打开以便纠错,如果你用以前php写法错误信息多半是关于未定义变量的。变量在赋值以前调用会有提示,解决办法是探测或者屏蔽。

  例如显示$foo,可以if(isset($foo)) echo $foo 或者echo @$foo

  【5】Win32下mail()不能发送电子邮件

  在linux下配置好的sendmail可以发送,在win32下需要调用smtp服务器来发送电子邮件,修改php.ini的SMTP = ip //ip是不带验证功能的smtp服务器(网上很难找到)

  php发送邮件的最好解决方法是用socket直接发送到对方email服务器而不用转发服务器。

  【6】初装的mysql如果没有设置密码,应该使用update mysql.user set password="yourpassword" where user="root" 修改密码

  【7】header already sent

  这个错误通常会在你使用HEADER的时候出现,他可能是几种原因:1,你在使用HEADER前PRING或者ECHO了2.你当前文件前面有空行3.你可能INCLUDE了一个文件,该文件尾部有空行或者输出也会出现这种错误。!

  【8】更改php.ini后没有变化

  重新启动web server,比如IIS,Apache等等,然后才会应用最新的设置。

  【9】php在2003上面安装(ISAPI的安装方法恳请高手指教)

  PHP4的php4isapi.dll好像和2003有些冲突,只能用CGI模式安装

  步骤一,先www.php.net 下在一个安装程序,我是装的是:php-4.2.3-installer.exe,你也可以去找最新的版本,在安装php-4.2.3-installer.exe之前保证你的IIS6.0启动了,并能够访问。安装好以后,在默认网站-->应用程序配置。

  步骤二:点击 web服务扩展 -->新建web服务扩展。

  步骤三: 扩展名-->php,然后添加

  步骤四:找到php.exe的路径添加上去。

  步骤五: 确定就可以了!
 
  步骤六: 选择php的服务扩展,然后点击允许。

  【10】有时候sql语句不起作用,对数据库操作失败,最简便的调试方法,echo那句sql,看看变量的值能得到不。

  【11】include和require的区别

  两者没有太大的区别,如果要包含的文件不存在,include提示notice,然后继续执行下面的语句,require提示致命错误并且退出。

  据我测试,win32平台下它们都是先包含后执行,所以被包含文件里最好不要再有include或require语句,这样会造成目录混乱。或许*nux下情况不同,暂时还没测试。

  如果一个文件不想被包含多次可以使用include_once或require_once## 读取,写入文档数据。

function r($file_name) {
[email protected]($file_name,"r");
[email protected]($filenum,LOCK_SH);
[email protected]($filenum,filesize($file_name));
[email protected]($filenum);
 return $file_data;
}
function w($file_name,$data,$method="w"){
[email protected]($file_name,$method);
 flock($filenum,LOCK_EX);
 $file_data=fwrite($filenum,$data);
 fclose($filenum);
 return $file_data;
}

  【12】isset()和empty()的区别

  两者都是测试变量用的,但是isset()是测试变量是否被赋值,而empty()是测试一个已经被赋值的变量是否为空。

  如果一个变量没被赋值就引用在php里是被允许的,但会有notice提示,如果一个变量被赋空值,$foo=""或者$foo=0或者 $foo=false,那么empty($foo)返回真,isset($foo)也返回真,就是说赋空值不会注销一个变量。
 
  要注销一个变量,可以用 unset($foo)或者$foo=NULL

  【13】mysql查询语句包含有关键字

  php查询mysql的时候,有时候mysql表名或者列名会有关键字,这时候查询会有错误。例如表名是order,查询时候会出错,简单的办法是sql语句里表名或者列名加上`[tab键上面]来加以区别,例如select * from `order`

  【14】通过HTTP协议一次上传多个文件的方法

  有两个思路,是同一个方法的两种实现。具体程序还需自己去设计。

  1、在form中设置多个文件输入框,用数组命名他们的名字,如下:

<form action="" method=post>
<input type=file name=usefile[]>
<input type=file name=usefile[]>
<input type=file name=usefile[]>
</form>

  这样,在服务器端做以下测试:

echo "<pre>";
print_r($_FILES);
echo "</pre>";

  2、在form中设置多个文件输入框,但名字不同,如下:

<form action="" method=post>
<input type=file name=usefile_a>
<input type=file name=usefile_b>
<input type=file name=usefile_c>
</form>

  在服务器端做同样测试:

echo "<pre>";
print_r($_FILES);
echo "</pre>";

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

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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles