PHP入门常见问题

php环境: Debian 7.8 + Apache 2.2.22 + Mysql 5.5.44 + PHP 5.4.44

1、php文件语法检查

shen@debian:/var/www$ php -l parse-error.php
PHP Parse error:  syntax error, unexpected end of file, expecting variable (T_VARIABLE) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in parse-error.php on line 11
Errors parsing parse-error.php
shen@debian:/var/www$ php -l parse-error.php 
PHP Parse error:  syntax error, unexpected end of file, expecting variable (T_VARIABLE) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in parse-error.php on line 11
Errors parsing parse-error.php
shen@debian:/var/www$ cat parse-error.php -n
     1	<!DOCTYPE html>
     2	<html>
     3	<body>
     4	
     5	<?php
     6	echo "My first PHP script!
     7	?>  
     8	
     9	</body>
    10	</html>
shen@debian:/var/www$ php -l index.php 
No syntax errors detected in index.php
shen@debian:/var/www$ cat -n index.php 
     1	<!DOCTYPE html>
     2	<html>
     3	<body>
     4	
     5	<?php
     6	echo "My first PHP script!";
     7	?>  
     8	
     9	</body>
    10	</html>

浏览器输入: http://localhost/parse-error.php ,显示空白网页。

2、move_uploaded_file移动文件未生效

练习:
http://www.runoob.com/php/php-file-upload.html

原因: move_uploaded_file函数的目标目录upload必须对帐号www-data可写

因为apache2服务的启动帐号是www-data,非root

shen@debian:/var/www/shm_fast$ ls -l upload
总用量 24
-rw-r--r-- 1 www-data www-data 23913  9月  1 11:48 1.png

查看apache2进程信息

shen@debian:/var/www/shm_fast$ ps -edf | grep apache2
www-data  2121 27140  0 09:45 ?        00:00:00 /usr/sbin/apache2 -k start
shen      4853 24406  0 11:50 pts/2    00:00:00 grep apache2
root     27140     1  0  8月31 ?      00:00:04 /usr/sbin/apache2 -k start
www-data 29266 27140  0  8月31 ?      00:00:00 /usr/sbin/apache2 -k start
www-data 29483 27140  0  8月31 ?      00:00:00 /usr/sbin/apache2 -k start
www-data 29484 27140  0  8月31 ?      00:00:00 /usr/sbin/apache2 -k start
www-data 29486 27140  0  8月31 ?      00:00:00 /usr/sbin/apache2 -k start
www-data 29487 27140  0  8月31 ?      00:00:00 /usr/sbin/apache2 -k start
www-data 29488 27140  0  8月31 ?      00:00:00 /usr/sbin/apache2 -k start
www-data 29489 27140  0  8月31 ?      00:00:00 /usr/sbin/apache2 -k start
www-data 29491 27140  0  8月31 ?      00:00:00 /usr/sbin/apache2 -k start
www-data 29494 27140  0  8月31 ?      00:00:00 /usr/sbin/apache2 -k start

3、echo打印布尔值FALSE显示为空

shen@debian:/var/www/shm_fast$ php
<?php
echo "FALSE=[".FALSE."]\n";

FALSE=[]

FALSE显示为1

shen@debian:/var/www/shm_fast$ php
<?php
echo "TRUE=[".TRUE."]\n";

TRUE=[1]

4、move_uploaded_file移动文件为中文文件名,目标文件名显示为乱码

练习:
http://www.runoob.com/php/php-file-upload.html

期望: /tmp/2015-03-22 21:03:03的屏幕截图.png
实际: /tmp/2015-03-22 21:03:03????Ļ??ͼ.png

解决:

设置php.ini中的default_charset = "UTF-8"

view /etc/php5/apache2/php.ini

 681 ; PHP's default character set is set to empty.
 682 ; http://php.net/default-charset
 683 default_charset = "UTF-8"

sudo /etc/init.d/apache2 restart

修改upload_file.php:

<?php
if ($_FILES["file"]["error"] > 0)
{
    echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
    move_uploaded_file($_FILES["file"]["tmp_name"], "/tmp/" . _FILES["file"]["name"]);
}
    echo "Moved to: " . "/tmp/" . $_FILES["file"]["name"];

?>

==>

<?php
if ($_FILES["file"]["error"] > 0)
{
    echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
    $filename_gbk = $_FILES["file"]["name"]; 
    $filename_utf8 = iconv("GBK", "UTF-8", $filename_gbk);   
    echo "Upload: " . $filename_utf8 . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
    move_uploaded_file($_FILES["file"]["tmp_name"], "/tmp/" . $filename_utf8);
    echo "Moved to: " . "/tmp/" . $filename_utf8;
}
?>

再次查看/tmp目录,能到中文文件名的图片

5、php网页中看不到错误信息

比如

shen@debian:/var/www/shm_fast$ cat trigger-error.php
<?php
$test=2;
if ($test>1)
{
trigger_error("Value must be 1 or below");
}
?>

网页中打开 http://localhost/shm_fast/trigger-error.php ,显示为空白网页。

在php命令行执行能显示错误:

shen@debian:/var/www/shm_fast$ php -f trigger-error.php
PHP Notice:  Value must be 1 or below in /run/shm/fast_www/trigger-error.php on line 5

浏览器打开php网页的错误在apache的错误日志中:

shen@debian:/var/www/shm_fast$ sudo tail /var/log/apache2/error.log
[Tue Sep 01 13:51:45 2015] [error] [client ::1] Negotiation: discovered file(s) matching request: /var/www/shm_fast/cookie (None could be negotiated).
[Tue Sep 01 13:56:02 2015] [error] [client ::1] PHP Warning:  Cookie values cannot contain any of the following ',; \\t\\r\\n\\013\\014' in /run/shm/fast_www/cookie.php on line 3
[Tue Sep 01 14:01:26 2015] [error] [client ::1] Negotiation: discovered file(s) matching request: /var/www/shm_fast/set-cookie (None could be negotiated).
[Tue Sep 01 14:02:42 2015] [error] [client 192.168.1.163] PHP Notice:  Undefined index: user in /run/shm/fast_www/get-cookie.php on line 4
[Tue Sep 01 14:55:44 2015] [error] [client ::1] PHP Warning:  fopen(welcome.txt): failed to open stream: No such file or directory in /run/shm/fast_www/error.php on line 2
[Tue Sep 01 14:56:12 2015] [error] [client ::1] PHP Warning:  fopen(welcome.txt): failed to open stream: No such file or directory in /run/shm/fast_www/error.php on line 2
[Tue Sep 01 15:03:15 2015] [error] [client ::1] script '/var/www/shm_fast/trigger-handler.php' not found or unable to stat
[Tue Sep 01 15:03:24 2015] [error] [client ::1] PHP Notice:  Value must be 1 or below in /run/shm/fast_www/trigger-error.php on line 5
[Tue Sep 01 15:04:06 2015] [error] [client ::1] PHP Notice:  Value must be 1 or below in /run/shm/fast_www/trigger-error.php on line 5
[Tue Sep 01 15:04:08 2015] [error] [client ::1] PHP Notice:  Value must be 1 or below in /run/shm/fast_www/trigger-error.php on line 5

顺便显示一下apache的访问日志:

shen@debian:/var/www/shm_fast$ sudo tail /var/log/apache2/access.log
::1 - - [01/Sep/2015:14:49:43 +0800] "GET /shm_fast/session.php HTTP/1.1" 200 355 "-" "curl/7.26.0"
::1 - - [01/Sep/2015:14:49:43 +0800] "GET /shm_fast/session.php HTTP/1.1" 200 355 "-" "curl/7.26.0"
::1 - - [01/Sep/2015:14:55:44 +0800] "GET /shm_fast/error.php HTTP/1.1" 200 306 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"
::1 - - [01/Sep/2015:14:56:12 +0800] "GET /shm_fast/error.php HTTP/1.1" 200 205 "-" "curl/7.26.0"
::1 - - [01/Sep/2015:14:57:41 +0800] "GET /shm_fast/die.php HTTP/1.1" 200 320 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.2.0"
::1 - - [01/Sep/2015:15:01:55 +0800] "GET /shm_fast/error-handler.php HTTP/1.1" 200 348 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.2.0"
::1 - - [01/Sep/2015:15:03:15 +0800] "GET /shm_fast/trigger-handler.php HTTP/1.1" 404 512 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.2.0"
::1 - - [01/Sep/2015:15:03:24 +0800] "GET /shm_fast/trigger-error.php HTTP/1.1" 200 306 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.2.0"
::1 - - [01/Sep/2015:15:04:06 +0800] "GET /shm_fast/trigger-error.php HTTP/1.1" 200 205 "-" "curl/7.26.0"
::1 - - [01/Sep/2015:15:04:08 +0800] "GET /shm_fast/trigger-error.php HTTP/1.1" 200 205 "-" "curl/7.26.0"

6、error_log输出php日志

shen@debian:/var/www/shm_fast$ cat error-log.php 
<?php
//error handler function
function customError($errno, $errstr)
 { 
 echo "<b>Error:</b> [$errno] $errstr<br />";
 echo "Webmaster has been notified";
 error_log("Error: [$errno] $errstr");
}

//set error handler
set_error_handler("customError",E_USER_WARNING);

//trigger error
$test=2;
if ($test>1)
 {
 trigger_error("Value must be 1 or below",E_USER_WARNING);
 }
?>

浏览器中输入: http://localhost/shm_fast/error-log.php
网页输出:

**Error: **[512] Value must be 1 or below
Webmaster has been notified

查看apache的错误日志:

shen@debian:/var/www/shm_fast$ sudo tail -1 /var/log/apache2/error.log
[Tue Sep 01 18:38:21 2015] [error] [client ::1] Error: [512] Value must be 1 or below

脚本方式执行error-log.php不会在apache错误日志中输出错误,而是输出到stdout:

shen@debian:/var/www/shm_fast$ php -f error-log.php 
<b>Error:</b> [512] Value must be 1 or below<br />Webmaster has been notifiedError: [512] Value must be 1 or below

转载于:https://my.oschina.net/fitnessefan/blog/500088

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值