本文目錄一覽:
有哪些 PHP 調試技巧?
1、最簡單經典的var_dump()或者echo +exit()
2、配置error_log,能夠解決很多疑難雜症
3、debug方式是:xdebug
4、firebug + firephp或者chrome + chromephp
PHP的入門技巧有哪些?
PHP 是一種開源的通用計算機腳本語言,尤其適用於網絡開發並可嵌入HTML中使用。PHP的語法借鑒吸收了C語言、Java和Perl等流行計算機語言的特點,易於一般程序員學習。杭州這邊到有碼互聯學習php還是挺好的,一般學習的都是3-4個月的學習,然後有2個月實訓,這樣下來就會增加自己的一些實戰能力。PHP的主要目標是允許網絡開發人員快速編寫動態頁面,但PHP也被用於其他很多領域。如果有其他編程語言的基礎,那上手更容易。
1、PHP 安裝與配置
新手建議使用PHP集成環境,Wampserver5。
Wamp就是Windows Apache Mysql PHP集成安裝環境,即在window下的apache、php和mysql的服務器軟件。
2、PHP 學習資料
建議參考w3school的PHP學習資料,簡單易懂,適合新手。
PHP視頻教程,建議參考PHP100的視頻教程,挺不錯的。
3、PHP 相關學習資料
PHP 的主要應用是web應用,web應用是由多種技術組成的。
學習PHP的同時,需要學習相關的其他技術。
SQL 是用於訪問和處理數據庫的標準的計算機語言
HTML 超文本標籤語言
CSS 層疊樣式表
JavaScript 世界上最流行的腳本語言
jQuery 是一個JavaScript庫,極大地簡化了 JavaScript 編程
高質量PHP代碼的50個技巧(3)
42
43
44
45
/**
Method to execute a command in the terminal
Uses :
1. system
2. passthru
3. exec
4. shell_exec
*/
function terminal($command)
{
//system
if(function_exists(‘system’))
{
ob_start();
system($command , $return_var);
$output = ob_get_contents();
ob_end_clean();
}
//passthru
else if(function_exists(‘passthru’))
{
ob_start();
passthru($command , $return_var);
$output = ob_get_contents();
ob_end_clean();
}
//exec
else if(function_exists(‘exec’))
{
exec($command , $output , $return_var);
$output = implode(“\n” , $output);
}
//shell_exec
else if(function_exists(‘shell_exec’))
{
$output = shell_exec($command) ;
}
else
{
$output = ‘Command execution not possible on this system’;
$return_var = 1;
}
return array(‘output’ = $output , ‘status’ = $return_var);
}
terminal(‘ls’);
上面的函數將運行shell命令, 只要有一個系統函數可用, 這保持了代碼的一致性.
5. 靈活編寫函數
?
1
2
3
4
5
6
function add_to_cart($item_id , $qty)
{
$_SESSION[‘cart’][‘item_id’] = $qty;
}
add_to_cart( ‘IPHONE3’ , 2 );
使用上面的函數添加單個項目. 而當添加項列表的時候,你要創建另一個函數嗎? 不用, 只要稍加留意不同類型的參數, 就會更靈活. 如:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function add_to_cart($item_id , $qty)
{
if(!is_array($item_id))
{
$_SESSION[‘cart’][‘item_id’] = $qty;
}
else
{
foreach($item_id as $i_id = $qty)
{
$_SESSION[‘cart’][‘i_id’] = $qty;
}
}
}
add_to_cart( ‘IPHONE3’ , 2 );
add_to_cart( array(‘IPHONE3’ = 2 , ‘IPAD’ = 5) );
現在, 同個函數可以處理不同類型的輸入參數了. 可以參照上面的例子重構你的多處代碼, 使其更智能.
6. 有意忽略php關閉標籤
我很想知道為什麼這麼多關於php建議的博客文章都沒提到這點.
?
1
2
3
?php
echo “Hello”;
//Now dont close this tag
這將節約你很多時間. 我們舉個例子:
一個 super_class.php 文件
?
1
2
3
4
5
6
7
8
9
?php
class super_class
{
function super_function()
{
//super code
}
}
?
//super extra character after the closing tag
index.php
?
1
2
require_once(‘super_class.php’);
//echo an image or pdf , or set the cookies or session data
這樣, 你將會得到一個 Headers already send error. 為什麼? 因為 「super extra character」 已經被輸出了. 現在你得開始調試啦. 這會花費大量時間尋找 super extra 的位置。因此, 養成省略關閉符的習慣:
?
1
2
3
4
5
6
7
8
9
?php
class super_class
{
function super_function()
{
//super code
}
}
//No closing tag
這會更好.
7. 在某地方收集所有輸入, 一次輸出給瀏覽器
這稱為輸出緩衝, 假如說你已在不同的函數輸出內容:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
function print_header()
{
echo “p id=’header’Site Log and Login links/p”;
}
function print_footer()
{
echo “p id=’footer’Site was made by me/p”;
}
print_header();
for($i = 0 ; $i 100; $i++)
{
echo “I is : $i ‘;
}
print_footer();
替代方案, 在某地方集中收集輸出. 你可以存儲在函數的局部變量中, 也可以使用ob_start和ob_end_clean. 如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function print_header()
{
$o = “p id=’header’Site Log and Login links/p”;
return $o;
}
function print_footer()
{
$o = “p id=’footer’Site was made by me/p”;
return $o;
}
echo print_header();
for($i = 0 ; $i 100; $i++)
{
echo “I is : $i ‘;
}
echo print_footer();
為什麼需要輸出緩衝:
可以在發送給瀏覽器前更改輸出. 如 str_replaces 函數或可能是 preg_replaces 或添加些監控/調試的html內容.
輸出給瀏覽器的同時又做php的處理很糟糕. 你應該看到過有些站點的側邊欄或中間出現錯誤信息. 知道為什麼會發生嗎? 因為處理和輸出混合了.
8. 發送正確的mime類型頭信息, 如果輸出非html內容的話.
輸出一些xml.
?
1
2
3
4
5
6
$xml = ‘?xml version=”1.0″ encoding=”utf-8″ standalone=”yes”?’;
$xml = “response
code0/code
/response”;
//Send xml data
echo $xml;
工作得不錯. 但需要一些改進.
?
1
2
3
4
5
6
7
$xml = ‘?xml version=”1.0″ encoding=”utf-8″ standalone=”yes”?’;
$xml = “response
code0/code
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/244595.html