PHP入門常見問題

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

一、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 ,顯示空白網頁。html

二、move_uploaded_file移動文件未生效

練習:
http://www.runoob.com/php/php-file-upload.htmlsql

緣由: move_uploaded_file函數的目標目錄upload必須對賬號www-data可寫apache

由於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進程信息cookie

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

三、echo打印布爾值FALSE顯示爲空

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

FALSE=[]

FALSE顯示爲1session

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

TRUE=[1]

四、move_uploaded_file移動文件爲中文文件名,目標文件名顯示爲亂碼

練習:
http://www.runoob.com/php/php-file-upload.htmlcurl

指望: /tmp/2015-03-22 21:03:03的屏幕截圖.png
實際: /tmp/2015-03-22 21:03:03????Ļ??ͼ.png函數

解決:url

設置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目錄,能到中文文件名的圖片

五、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"

六、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
相關文章
相關標籤/搜索