做開發請開啟全部錯誤提示:error_reporting = E_ALL | E_STRICT
屏蔽錯誤提示等於掩耳盜鈴。
程式碼寫規範,錯誤少一半。
1:為什麼我得不到變數
我在一網頁向另一網頁POST資料name,為什麼輸出$name時卻得不到任何值?
在PHP4.2以後的版本中register_global默認為off
若想取得從另一頁提交的變數:
方法一:在PHP.ini找到register_global,並把它設定為on.
方法二:在接收網頁最前面放上這個extract($_POST); extract($_GET);(注意extract($_SESSION)前面必須要有Session_Start()).
//extract ( array $var_array [, int $extract_type [, string $prefix ]] )
//本函數用來將變數從陣列匯入到目前的符號表中。接受結合數組 var_array 作為參數並將鍵//名當作變數名,值作為變數的值。對每個鍵/值對都會在目前的符號表中建立變量,並受到 //extract_type 和 prefix 參數的影響。
//import_request_variables ( string $types [, string $prefix ] )超全局,在$_ $*時有漏洞
方法三:一個一個讀取變數$a=$_GET["a"];$b= $_POST["b"]等,這種方法雖然麻煩,但比較安全.
2:調試你的程式
在運行時必須知道某個變數為何值。我是這樣做的,建立一檔案debug.php,其內容如下:
PHP程式碼:---------------------------- -------------------------------------------------- --
複製程式碼
Ob_Start();
Session_Start();
Echo "
";<br>本頁得到的_POST變數有:";<br> Print_R($_POST);<br> Echo "本頁得到的_COOKIE變數有:";<br> Print_R($_COOKIE);<br>)<br> Print_R($_COOKIE);<br> ";<br> Print_R($_SESSION);<br> Echo "";
---------------------------- -------------------------------------------------- --
然後在php.ini中設定:include_path = "c:/php",並將debug.php放在此資料夾,
以後就可以在每個網頁裡包含此文件,查看得到的變數名稱和值.
3:如何使用session
凡是與session有關的,之前必須調用函數session_start();
為session付值很簡單,如:
PHP代碼:-------- -------------------------------------------------- ----------------------
在php4.2之後,可以為session直接付值:
PHP代碼:-------- -------------------------------------------------- ----------------------
[php]
Session_Start();
$_SESSION["name"]="value";
[/php]
------------------------------------------------ --------------------------------
取消session可以這樣:
PHP代碼:------ -------------------------------------------------- ------------------------
[php]
session_start();
session_unset();
session_destroy();
[/php]
------------------------------------------------- -------------------------------
取消某個session變數在php4.2以上還有BUG.
注意:
1:在呼叫Session_Start()之前不能有任何輸出.例如下面是錯誤的.
========================== ==================
1行
2行[php]
3行Session_Start();//之前在第一行已經有輸出
4行... ..
5行[/php]
======================================== ==
提示1:
凡是出現"........headers already sent..........",就是Session_Start()之前向瀏覽器輸出訊息.
去掉輸出就正常,(COOKIE也會出現這種錯誤,錯誤原因一樣)
提示2:
如果你的Session_Start()放在循環語句裡,並且很難確定之前哪裡向瀏覽器輸出信息,可以用下面這個方法:
1行[php] Ob_Start(); [/php]
........這裡是你的程式...
2:這是什麼錯誤
Warning: session_start(): open(/tmpsess_7d190aa36b4c5ec13a5c1649cc2da23f, O_RDWR) failed:....
因為你沒有指定session檔案的存放路徑.
解決方法打開php.ini,找到session.save_path,修改為session.save_path= "c:/tmp"
4:為什麼我傳送變數到另一網頁時,只得到前半部,以空格開頭的則全部遺失
PHP代碼:-------------------------------------------- ------------------------------------
[php]
$Var="hello php"; //修改為$Var=" hello php";試試得到什麼結果
$post= "receive.php?Name=".$Var;
header("location:$post");
[/php]
----------------- -------------------------------------------------- -------------
receive.php的內容:
PHP程式碼:------------------------ -------------------------------------------------- ------
[php]
Echo "
";<br>Echo $_GET["Name"];<br>Echo "";
[/php]
----- -------------------------------------------------- -------------------------
正確的方法是:
PHP程式碼:------------ -------------------------------------------------- ------------------
[php]
$Var="hello php";
$post= "receive.php?Name=".urlencode($Var) ;
header("location:$post");
[/php]
------------------------------- -------------------------------------------------
在接收頁面你不需要使用Urldecode(),變數會自動編碼.
5:如何截取指定長度漢字而不會出現以"[/php]"結尾,超出部分以"..."代替
一般來說,要截取的變數來自Mysql,首先要保證那個字段長度要足夠長,一般為char(200),可以保持100個漢字,包括標點.
PHP代碼:----- -------------------------------------------------- -------------------------
[php]
$str="這個字元好長呀,^_^";
$Short_Str= showShort($str,4);//截取前面4個漢字,結果為:這個字元...
Echo "$Short_Str";
Function csubstr($str,$start,$len)
{
$strlen= strlen($str);
$clen=0;
for($i=0;$i{
if ($clen>=$start+$len)
break;
if(ord(substr($str,$i,1))>0xa0)
{
if ($clen>=$start)
$tmpstr.=substr($str,$i,2);
$i++;
}
else
{
if ($clen>=$start)
$tmpstr.=substr($str,$i,1);
}
}
return $tmpstr; ,$len)
{
$tempstr = csubstr($str,0,$len);
if ($str$tempstr)
$tempstr .= "..."; //要以什麼結尾,修改這裡就可以.
return $tempstr;
}
------------------------------------ --------------------------------------------
6:規範你的SQL語句
在表格,字段前面加上"`",這樣就不會因為誤用關鍵字而出現錯誤,
當然我並不推薦你使用關鍵字.
例如
$Sql= "INSERT INTO `xltxlm` (`author`, `title`, `id`, `content`, `date`) VALUES ('xltxlm', 'use`', 1, 'criterion your sql string ', '2003- 07-11 00:00:00')"
"`"怎麼輸入? 在TAB鍵上面.
7:如何使Html/PHP格式的字串不被解釋,而是照原樣顯示
PHP程式碼:------------------------------------------------------------ ---------------------------------
[php]
$str="
PHP
";Echo "被解釋過的: ".$str."
經過處理的:";
Echo htmlentities(nl2br($str));
[/php]
nl2br() 函數在字符字串中的每個新行(n) 之前插入HTML 換行符(
)。
htmlentities(string,quotestyle,character-set)
quotestyle可選。規定如何編碼單引號和雙引號。
ENT_COMPAT - 預設。僅編碼雙引號。
ENT_QUOTES - 編碼雙引號和單引號。
ENT_NOQUOTES - 不編碼任何引號。
character-set
ISO-8859-1 - 預設。西歐。
ISO-8859-15 - 西歐(增加 Euro 符號以及法語、芬蘭字母)。
UTF-8 - ASCII 相容多位元組8 位元Unicode
cp866 - DOS 專用Cyrillic 字元集
cp1251 - Windows 專用Cyrillic 字元集
cp1252 - Windows 專用西歐字元集標準字元集
BIG5 - 繁體中文
----------------------------------------- ---------------------------------------
8:怎麼在函數裡取得函數外的變數值
PHP程式碼:--------------------------------------- -----------------------------------------
[php]
$a= "PHP";
foo();
Function foo()
{
global $a;//刪除這裡看看是什麼結果
Echo "$a";
}
[/php]
---- -------------------------------------------------- --------------------------
9:我怎麼知道系統預設支援什麼函數
PHP程式碼:---- -------------------------------------------------- --------------------------
[php]
$arr = get_defined_functions();
Function php() {
}
echo "
";<br>Echo "這裡顯示系統所支援的所有函數,和自定以函數phpn";<br>print_r($arr);<br>echo "";
[/php]
--------- -------------------------------------------------- ---------------------
10:如何比較兩個日期相差幾天
PHP代碼:---------- -------------------------------------------------- --------------------
[php]
$Date_1="2003-7-15";//也可以是:$Date_1="2003-6 -25 23:29:14";
$Date_2="1982-10-1";
$Date_List_1=explode("-",$Date_1);
$Date_List_2=explode("-",$Date_2);
$Date_List_2=explode("-",$Date_2);
$Date_List_2=explode("-",$Date_2);
$Date_List_2=explode("-",$Date_2);
$Date_List_2=explode("-",$Date_2);
$Date_List_2=explode("-",$Date_2);
$Date_。 $d1=mktime(0,0,0,$Date_List_1[1],$Date_List_1[2],$Date_List_1[0]);
$d2=mktime(0,0,0,$Date_List_2[1],$Date_List_2 [2],$Date_List_2[0]);
$Days=round(($d1-$d2)/3600/24);
Echo "偶已經奮鬥了$Days 天^_^";
[/php]
(strtotime($Date_1) - strtotime($Date_2))/3600/24
------------------------------ --------------------------------------------------
11:為什麼我升級PHP後,原來的程式出現滿屏的Notice: Undefined variable:
這是警告的意思,由於變量未定義引起的.
打開php.ini,找到最下面的error_reporting ,修改為error_reporting = E_ALL & ~E_NOTICE
對於Parse error錯誤
error_reporting(0)無法關閉.
如果你想關閉任何錯誤提示,打開php.ini,找到display_errors,設定為display_errors = Off.以後任何錯誤都不會提示.
那什麼是error_reporting?
12:我想在每個文件最前,最後面都加上一文件.但一個一個添加很麻煩
1:打開php.ini文件
設定include_path= "c:"
2:寫兩個檔案
auto_prepend_file.php 和auto_append_file.php 保存在c盤,他們將自動依附在每個php檔案的頭部和尾部.
3:在php.ini中找到:
Automatically add files before or after any PHP document.
auto_prepend_file = auto_prepend_file.php;依附在頭
auto_append_file = auto_append_file.php;依附在頭
auto_append_file = auto_append_file.php; :------------------------------------------------- -------------------------------
[php]
Include "auto_prepend_file.php" ;
.... ...//這裡是你的程式
Include "auto_append_file.php";
[/php]
----------------------- -------------------------------------------------- -------
13:如何利用PHP上傳檔案
PHP程式碼:------------------------ -------------------------------------------------- ------
[php]
[php]
$str=$_POST['str'];
eval(" $o=$str;");
Echo "$o";
[/php]
--------------------------- -------------------------------------------------- ---
另外,用此函數必須特別小心!!
如果有人輸入format: d:會是什麼結果?

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

tomakephpapplicationsfaster,關注台詞:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

到ImprovephPapplicationspeed,關注台詞:1)啟用opcodeCachingwithapCutoredUcescriptexecutiontime.2)實現databasequerycachingingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandreduceconnection.4 limitesclection.4.4

依赖注入(DI)通过显式传递依赖关系,显著提升了PHP代码的可测试性。1)DI解耦类与具体实现,使测试和维护更灵活。2)三种类型中,构造函数注入明确表达依赖,保持状态一致。3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

WebStorm Mac版
好用的JavaScript開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境