1.²»ÒªÊ¹ÓÃÏa¶ÔÂ*¾¶
*³£³£»a¿´µ½:
require_once('../../lib/some_class.php');
¸Ã½¨Óкܶaȱµa:
ËuÊ×ÏȲeÕÒÖ¸¶¨µÄphp°uº¬Â¾¶, È»ºo²eÕÒµ±Ç°Ä¿Â¼.
Òo´Ë»a¼i²e¹ý¶a¾¶.
Èç¹u¸Ã½Å±¾±»ÁiһĿ¼µÄ½Å±¾°uº¬, ËuµÄ»u±¾Ä¿Â¼±a³ÉÁËÁiÒ»½Å±¾ËuÔÚµÄĿ¼.
ÁiÒ»ÎÊÌa, µ±¶¨Ê±ÈÎÎñÔËÐиýű¾, ËuµÄÉϼ¶Ä¿Â¼¿ÉÄܾͲ»Êǹ¤×÷Ŀ¼ÁË.
Òo´Ë×i¼ÑÑ¡ÔñÊÇʹÓþø¶Ô¾¶:
define('ROOT' , '/var/www/project/');
require_once(ROOT . '../../lib/some_class.php');
//rest of the code
ÎÒÃǶ¨ÒaÁËÒ»¸o¾ø¶Ô¾¶, Öµ±»Ð´ËÀÁË. ÎÒÃÇ»¹¿ÉÒԸĽøËu. ¾¶ /var/www/project Ò²¿ÉÄÜ»a¸Ä±a, ÄÇôÎÒÃÇÿ´Î¶¼Òª¸Ä±aËuÂð? ²»ÊǵÄ, ÎÒÃÇ¿ÉÒÔʹÓÃFILE³£Á¿, Èç:
//suppose your script is /var/www/project/index.php
//Then FILE will always have that full path.
define('ROOT' , pathinfo(FILE, PATHINFO_DIRNAME));
require_once(ROOT . '../../lib/some_class.php');
//rest of the code
ÏÖÔÚ, ÎÞÂÛÄaÒƵ½ÄĸoĿ¼, ÈçÒƵ½Ò»¸oÍaÍøµÄþÎñÆ÷ÉÏ, ´uÂeÎÞÐe¸u¸Ä±a¿ÉÕýÈÔËÐÐ.
2. ²»ÒªÖ±½ÓʹÓà require, include, include_once, required_once
¿ÉÒÔÔڽű¾Í²¿ÒýÈe¶a¸oÎļþ, ÏñÀa¿a, ¹¤¾ßÎļþºÍÖuÊÖº¯ÊýµÈ, Èç:
require_once('lib/Database.php');
require_once('lib/Mail.php');
require_once('helpers/utitlity_functions.php');
ÕaÖÖÓèÏaµ±Ôʼ. Ó¦¸Ã¸uÁe»iµa. Ó¦±aд¸oÖuÊÖº¯Êý°uº¬Îļþ. ÀýÈç:
function load_class($class_name)
{
//path to the class file
$path = ROOT . '/lib/' . $class_name . '.php');
require_once( $path );
}
load_class('Database');
load_class('Mail');
ÓÐʲô²»Ò»ÑuÂð? ¸Ã´uÂe¸u¾ß¿É¶ÁÐÔ.
Œ¢À´Äa¿ÉÒÔ°´ÐeÀ(C)Õ¹¸Ãº¯Êý, Èç:
function load_class($class_name)
{
//path to the class file
$path = ROOT . '/lib/' . $class_name . '.php');
if(file_exists($path))
{
require_once( $path );
}
}
»¹¿É×oµÃ¸u¶a:
ΪͬÑuÎļþ²eÕÒ¶a¸oĿ¼
ÄܺÜÈÝÒ׵ĸıaÅÖÃÀaÎļþµÄĿ¼, ÎÞÐeÔÚ´uÂe¸÷´¦Ò»Ò»ÐÞ¸Ä
¿ÉʹÓÃÀaËƵĺ¯Êý¼ÓÔØÎļþ, ÈçhtmlÄÚÈÝ.
3. ΪӦÓñ£Áoµ÷ÊÔ´uÂe
ÔÚ¿ª¢»¾³ÖÐ, ÎÒÃÇ´oÓ¡Êý¾Ý¿a²eѯÓi¾a, ת´aeÓÐÎÊÌaµÄ±aÁ¿Öµ, ¶øÒ»µ(C)ÎÊÌa½a¾o, ÎÒÃÇ×¢ÊÍ»oɾ³ýËuÃÇ. È»¶ø¸uºÃµÄ×o¨ÊDZ£Áoµ÷ÊÔ´uÂe.
ÔÚ¿ª¢»¾³ÖÐ, Äa¿ÉÒÔ:
define('ENVIRONMENT' , 'development');
if(! $db->query( $query )
{
if(ENVIRONMENT == 'development')
{
echo "$query failed";
}
else
{
echo "Database error. Please contact administrator";
}
}
ÔÚþÎñÆ÷ÖÐ, Äa¿ÉÒÔ:
define('ENVIRONMENT' , 'production');
if(! $db->query( $query )
{
if(ENVIRONMENT == 'development')
{
echo "$query failed";
}
else
{
echo "Database error. Please contact administrator";
}
}
4. ʹÓÿɿçƽ̨µÄº¯ÊýÖ´ÐÐÃuÁi
system, exec, passthru, shell_exec Õa4¸oº¯Êý¿ÉÓÃÓÚÖ´ÐÐϵͳÃuÁi. ÿ¸oµÄÐÐΪ¶¼ÓÐϸ΢²i±ð. ÎÊÌaÔÚÓÚ, µ±ÔÚ¹²ÏiÖ÷»uÖÐ, ijÐ(C)º¯Êý¿ÉÄܱ»Ñ¡ÔñÐԵĽuÓÃ. ´o¶aÊýÐÂÊÖÇ÷ÓÚÿ´ÎÊ×Ïȼi²eÄĸoº¯Êý¿ÉÓÃ, È»¶øÔÙʹÓÃËu.
¸uºÃµÄ½°¸ÊÇ*a³Éº¯ÊýÒ»¸o¿É¿çƽ̨µÄº¯Êý.
/*
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');
ÉÏÃaeµÄº¯ÊýŒ¢ÔËÐÐshellÃuÁi, Ö»ÒªÓÐÒ»¸oϵͳº¯Êý¿ÉÓÃ, Õa±£³ÖÁË´uÂeµÄÒ»ÖÂÐÔ.
5. Áe»i±aдº¯Êý
function add_to_cart($item_id , $qty)
{
$_SESSION['cart']['item_id'] = $qty;
}
add_to_cart( 'IPHONE3' , 2 );
ʹÓÃÉÏÃaeµÄº¯ÊýÌi¼Óµ¥¸oÏiÄ¿. ¶øµ±Ìi¼ÓÏiÁбiµÄʱºo,ÄaÒª´´½¨ÁiÒ»¸oº¯ÊýÂð? ²»ÓÃ, Ö»ÒªÉÔ¼ÓÁoÒa²»Í¬ÀaÐ͵IJÎÊý, ¾Í»a¸uÁe»i. Èç:
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) );
ÏÖÔÚ, ͬ¸oº¯Êý¿ÉÒÔ´¦Ài²»Í¬ÀaÐ͵ÄÊaÈe²ÎÊýÁË. ¿ÉÒÔ²ÎÕÕÉÏÃaeµÄÀý×ÓÖع¹ÄaµÄ¶a´¦´uÂe, ʹÆa¸uÖÇÄÜ.
6. ÓÐÒaºoÂÔphp¹Ø±Õ±eÇ(C)
ÎÒºÜÏeÖªµÀΪʲôÕaô¶a¹ØÓÚphp½¨ÒeµÄ²(C)¿ÍÎÄÕ¶¼Ã»Ìaµ½Õaµa.
<?php
echo "Hello";
//Now dont close this tag
ÕaŒ¢½ÚÔ¼ÄaºÜ¶aʱ¼a. ÎÒÃǾٸoÀý×Ó:
Ò»¸o super_class.php Îļþ
<?php
class super_class
{
function super_function()
{
//super code
}
}
?>
//super extra character after the closing tag
index.php
require_once('super_class.php');
//echo an image or pdf , or set the cookies or session data
ÕaÑu, ÄaŒ¢»aµÃµ½Ò»¸o Headers already send error. Ϊʲô? ÒoΪ ¡°super extra character¡± ÒѾ±»Êa³oÁË. ÏÖÔÚÄaµÃ¿ªÊ¼µ÷ÊÔÀ². Õa»a»¨Ñ´oÁ¿Ê±¼aÑ°ÕÒ super extra µÄλÖÃ.
Òo´Ë, Ñø³ÉÊ¡ÂԹرÕuµÄÏ°¹ß:
<?php
class super_class
{
function super_function()
{
//super code
}
}
//No closing tag
Õa»a¸uºÃ.
7. ÔÚijµØ*½ÊÕ¼¯ËuÓÐÊaÈe, Ò»´ÎÊa³o¸øa¯ÀÀÆ÷*
Õa³ÆΪÊa³o»º³a, ¼ÙÈç˵ÄaÒÑÔÚ²»Í¬µÄº¯ÊýÊa³oÄÚÈÝ:
function print_header()
{
echo "
}
function print_footer()
{
echo " ";
}
print_header();
for($i = 0 ; $i < 100; $i++)
{
echo "I is : $i
';
}
print_footer();
Ìae´u½°¸, ÔÚijµØ*½¼¯ÖÐÊÕ¼¯Êa³o. Äa¿ÉÒÔ´ae´¢ÔÚº¯ÊýµÄ¾Ö²¿±aÁ¿ÖÐ, Ò²¿ÉÒÔʹÓÃob_startºÍob_end_clean. ÈçÏÂ:
function print_header()
{
$o = "
¿ÉÒÔÔÚ¢Ë͸øa¯ÀÀÆ÷Ç°¸u¸ÄÊa³o. Èç str_replaces º¯Êý»o¿ÉÄÜÊÇ preg_replaces »oÌi¼ÓÐ(C)¼a¿Ø/µ÷ÊÔµÄhtmlÄÚÈÝ.
"; //Send xml data echo $xml; ¹¤×÷µÃ²»´i. µ«ÐeÒªÒ»Ð(C)¸Ä½ø. $xml = ''; $xml = "
Êa³o¸øa¯ÀÀÆ÷µÄͬʱÓÖ×ophpµÄ´¦ÀiºÜÔa¸a. ÄaÓ¦¸Ã¿´µ½¹ýÓÐÐ(C)Õ¾µaµÄ²a±ßÀ¸»oÖмa³oÏÖ´iÎoÐÅÏ¢. ÖªµÀΪʲô»a¢ÉuÂð? ÒoΪ´¦ÀiºÍÊa³o»iºÏÁË.
8. ¢ËÍÕýȵÄmimeÀaÐÍÍÐÅÏ¢, Èç¹uÊa³oÇhtmlÄÚÈݵĻ°.
Êa³oÒ»Ð(C)xml.
$xml = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>';
$xml = "
0
"; //Send xml data header("content-type: text/xml"); echo $xml; ×¢ÒaheaderÐÐ. ¸ÃÐиaeÖªa¯ÀÀÆ÷*¢Ë͵ÄÊÇxmlÀaÐ͵ÄÄÚÈÝ. ËuÒÔa¯ÀÀÆ÷ÄÜÕýÈ*µÄ´¦Ài. ºÜ¶aµÄjavascript¿aÒ²ÒÀÀµÍ*ÐÅÏ¢. ÀaËƵÄÓÐ javascript , css, jpg image, png image: JavaScript header("content-type: application/x-javascript"); echo "var a = 10"; CSS header("content-type: text/css"); echo "#div id { background:#000; }"; **9\. ΪmysqlÁ¬½ÓÉeÖÃÕýÈ*µÄ×Ö*u±aÂe **Ôø¾Óoµ½¹ýÔÚmysql±iÖÐÉeÖÃÁËunicode/utf-8±aÂe, phpadminÒ²ÄÜÕýÈ*ÏÔʾ, µ«µ±Äa»ñÈ¡ÄÚÈݲ¢ÔÚÒ³ÃaeÊa³oµÄʱºo,»a³oÏÖÂÒÂe. ÕaÀiµÄÎÊÌa³oÔÚmysqlÁ¬½ÓµÄ×Ö*u±aÂe. //Attempt to connect to database $c = mysqli_connect($this->host , $this->username, $this->password); //Check connection validity if (!$c) { die ("Could not connect to the database host: 0
". mysqli_connect_error()); } //Set the character set of the connection if(!mysqli_set_charset ( $c , 'UTF8' )) { die('mysqli_set_charset() failed'); } Ò»µ(C)Á¬½ÓÊý¾Ý¿a, ×iºÃÉeÖÃÁ¬½ÓµÄ characterset. ÄaµÄÓ¦ÓÃÈç¹uÒªÖ§³Ö¶aÓiÑÔ, Õaô×oÊDZØÐeµÄ. **10\. ʹÓà htmlentities ÉeÖÃÕýÈ*µÄ±aÂeÑ¡Ïi **php5.4Ç°, ×Ö*uµÄĬÈϱaÂeÊÇISO-8859-1, ²»ÄÜÖ±½ÓÊa³oÈçÀ aµÈ. $value = htmlentities($this->value , ENT_QUOTES , CHARSET); php5.4ÒÔºo, ĬÈϱaÂeΪUTF-8, ÕaŒ¢½a¾oºÜ¶aÎÊÌa. µ«Èç¹uÄaµÄÓ¦ÓÃÊǶaÓiÑÔµÄ, ÈÔȻҪÁoÒa±aÂeÎÊÌa,. **11\. ²»ÒªÔÚÓ¦ÓÃÖÐʹÓÃgzipѹËoÊa³o, ÈÃapache´¦Ài **¿¼ÂǹýʹÓà ob_gzhandler Âð? ²»ÒªÄÇÑu×o. ºÁÎÞÒaÒa. phpÖ»Ó¦ÓÃÀ´±aдӦÓÃ. ²»Ó¦²ÙÐÄ*þÎñÆ÷ºÍa¯ÀÀÆ÷µÄÊý¾Ý´«ÊaÓÅ»¯ÎÊÌa. ʹÓÃapacheµÄmod_gzip/mod_deflate Ä£¿eѹËoÄÚÈÝ. **12\. ʹÓÃjson_encodeÊa³o¶¯Ì¬javascriptÄÚÈÝ **ʱ³£»aÓÃphpÊa³o¶¯Ì¬javascriptÄÚÈÝ: $images = array( 'myself.png' , 'friends.png' , 'colleagues.png' ); $js_code = ''; foreach($images as $image) { $js_code .= "'$image' ,"; } $js_code = 'var images = [' . $js_code . ']; '; echo $js_code; //Output is var images = ['myself.png' ,'friends.png' ,'colleagues.png' ,]; ¸u´ÏÃ÷µÄ×o*¨, ʹÓà json_encode: $images = array( 'myself.png' , 'friends.png' , 'colleagues.png' ); $js_code = 'var images = ' . json_encode($images); echo $js_code; //Output is : var images = ["myself.png","friends.png","colleagues.png"] ÓÅÑźo? **13\. дÎļþÇ°, ¼i²eĿ¼дȨÏÞ **д»o±£´aeÎļþÇ°, È*±£Ä¿Â¼ÊÇ¿ÉдµÄ, ¼ÙÈç²»¿Éд, Êa³o´iÎoÐÅÏ¢. Õa»a½ÚÔ¼ÄaºÜ¶aµ÷ÊÔʱ¼a. linuxϵͳÖÐ, ÐeÒª´¦ÀiȨÏÞ, Ŀ¼ȨÏÞ²»µ±»aµ¼ÖºܶaºÜ¶aµÄÎÊÌa, ÎļþÒ²ÓпÉÄÜÎÞ*¨¶ÁÈ¡µÈµÈ. È*±£ÄaµÄÓ¦ÓÃ×a¹»ÖÇÄÜ, Êa³oijÐ(C)ÖØÒªÐÅÏ¢. $contents = "All the content"; $file_path = "/var/www/project/content.txt"; file_put_contents($file_path , $contents); Õa´oÌaÉÏÕýÈ*. µ«ÓÐÐ(C)¼a½ÓµÄÎÊÌa. file_put_contents ¿ÉÄÜ»aÓÉÓÚ¼¸¸oÔÒoʧ°Ü: ¸¸Ä¿Â¼²»´aeÔÚ Ä¿Â¼´aeÔÚ, µ«²»¿Éд Îļþ±»Ð´Ëøס? ËuÒÔдÎļþÇ°×oÃ÷È*µÄ¼i²e¸uºÃ. $contents = "All the content"; $dir = '/var/www/project'; $file_path = $dir . "/content.txt"; if(is_writable($dir)) { file_put_contents($file_path , $contents); } else { die("Directory $dir is not writable, or does not exist. Please check"); } Õaô×oºo, Äa»aµÃµ½Ò»¸oÎļþÔںδ¦Ð´¼°ÎªÊ²Ã´Ê§°ÜµÄÃ÷È*ÐÅÏ¢. **14\. ¸u¸ÄÓ¦Óô´½¨µÄÎļþȨÏÞ **ÔÚlinux»*¾³ÖÐ, ȨÏÞÎÊÌa¿ÉÄÜ»aÀË*ÑÄaºÜ¶aʱ¼a. ´Ó½ñÍuºo, ÎÞÂÛºÎʱ, µ±Äa´´½¨Ò»Ð(C)Îļþºo, È*±£Ê¹ÓÃchmodÉeÖÃÕýÈ*ȨÏÞ. *ñÔoµÄ»°, ¿ÉÄÜÎļþÏÈÊÇÓÉ"php"Óû§´´½¨, µ«ÄaÓÃÆaËuµÄÓû§µÇ¼¹¤×÷, ϵͳŒ¢»a¾Ü¾ø*ÃÎÊ»o´o¿ªÎļþ, Äa²»µÃ²»*ÜÁ¦»ñÈ¡rootȨÏÞ, ¸u¸ÄÎļþµÄȨÏ޵ȵÈ. // Read and write for owner, read for everybody else chmod("/somedir/somefile", 0644); // Everything for owner, read and execute for others chmod("/somedir/somefile", 0755); **15\. ²»ÒªÒÀÀµsubmit°´Å¥ÖµÀ´¼i²e±iµ¥Ìa½»ÐÐΪ **if($_POST['submit'] == 'Save') { //Save the things } ÉÏÃae´o¶aÊýÇe¿oÕýÈ*, ³ýÁËÓ¦ÓÃÊǶaÓiÑÔµÄ. 'Save' ¿ÉÄÜ´u±iÆaËuº¬Òa. ÄaÔoôÇø*ÖËuÃÇÄØ. Òo´Ë, ²»ÒªÒÀÀµÓÚsubmit°´Å¥µÄÖµ. if( $_SERVER['REQUEST_METHOD'] == 'POST' and isset($_POST['submit']) ) { //Save the things } ÏÖÔÚÄa´Ósubmit°´Å¥ÖµÖнaÍѳoÀ´ÁË. **16\. Ϊº¯ÊýÄÚ×ܾßÓÐÏaֵͬµÄ±aÁ¿¶¨Òa³É¾²Ì¬±aÁ¿** //Delay for some time function delay() { $sync_delay = get_option('sync_delay'); echo "
Delaying for $sync_delay seconds..."; sleep($sync_delay); echo "Done
"; } Óþ²Ì¬±aÁ¿È¡´u: //Delay for some time function delay() { static $sync_delay = null; if($sync_delay == null) { $sync_delay = get_option('sync_delay'); } echo "
Delaying for $sync_delay seconds..."; sleep($sync_delay); echo "Done
"; } **17\. ²»ÒªÖ±½ÓʹÓà $_SESSION ±aÁ¿ **ijÐ(C)¼oµ¥Àý×Ó: $_SESSION['username'] = $username; $username = $_SESSION['username']; Õa»aµ¼ÖÂijÐ(C)ÎÊÌa. Èç¹uÔÚͬ¸oÓoÃuÖÐÔËÐÐÁ˶a¸oÓ¦ÓÃ, session ±aÁ¿¿ÉÄÜ»a³aÍ». Á½¸o²»Í¬µÄÓ¦ÓÿÉÄÜʹÓÃͬһ¸osession key. ÀýÈç, Ò»¸oÇ°¶ËÃÅ»§, ºÍÒ»¸oºǫ¹ÜÀiϵͳʹÓÃͬһÓoÃu. ´ÓÏÖÔÚ¿ªÊ¼, ʹÓÃÓ¦ÓÃÏa¹ØµÄkeyºÍÒ»¸o°u×°º¯Êý: define('APP_ID' , 'abc_corp_ecommerce'); //Function to get a session variable function session_get($key) { $k = APP_ID . '.' . $key; if(isset($_SESSION[$k])) { return $_SESSION[$k]; } return false; } //Function set the session variable function session_set($key , $value) { $k = APP_ID . '.' . $key; $_SESSION[$k] = $value; return true; } **18\. Œ¢¹¤¾ßº¯Êý*a×°µ½ÀaÖÐ** ¼ÙÈçÄaÔÚijÎļþÖж¨ÒaÁ˺ܶa¹¤¾ßº¯Êý: function utility_a() { //This function does a utility thing like string processing } function utility_b() { //This function does nother utility thing like database processing } function utility_c() { //This function is ... } ÕaÐ(C)º¯ÊýµÄʹÓÃ*ÖÉ¢µ½Ó¦Óø÷´¦. Äa¿ÉÄÜÏeŒ¢ËuÃÇ*a×°µ½Ä³¸oÀaÖÐ: class Utility { public static function utility_a() { } public static function utility_b() { } public static function utility_c() { } } //and call them as $a = Utility::utility_a(); $b = Utility::utility_b(); ÏÔ¶øÒ×¼uµÄºÃ´¦ÊÇ, Èç¹uphpÄÚ½¨ÓÐͬÃuµÄº¯Êý, ÕaÑu¿ÉÒÔ±ÜÃa³aÍ». ÁiÒ»ÖÖ¿´*¨ÊÇ, Äa¿ÉÒÔÔÚͬ¸oÓ¦ÓÃÖÐΪͬ¸oÀaά»¤¶a¸o°ae±¾, ¶ø²»µ¼Ö³aÍ». ÕaÊÇ*a×°µÄ»u±¾ºÃ´¦, ÎÞËu. **19\. Bunch of silly tips **>>ʹÓÃechoÈ¡´uprint ʹÓÃstr_replaceÈ¡´upreg_replace, ³ý*ÇÄa¾ø¶ÔÐeÒª ²»ÒªÊ¹Óà short tag ¼oµ¥×Ö*u´®Óõ¥ÒýºÅÈ¡´uË«ÒýºÅ headÖض¨Ïoºo¼ÇµÃʹÓÃexit ²»ÒªÔÚÑ»*Öе÷Óú¯Êý isset±Èstrlen¿i ʼÖÐÈçÒ»µÄ¸ñʽ»¯´uÂe ²»ÒªÉ¾³ýÑ»*»oÕßif-elseµÄÀ¨ºÅ ²»ÒªÕaÑuд´uÂe: if($a == true) $a_count++; Õa¾ø¶ÔWASTE. д³É: if($a == true) { $a_count++; } ²»Òª³¢ÊÔÊ¡ÂÔÒ»Ð(C)Ói*¨À´Ëo¶Ì´uÂe. ¶øÊÇÈÃÄaµÄÂß¼¼o¶Ì. ʹÓÃÓиßÁÁÓi*¨ÏÔʾµÄÎı¾±a¼Æ÷. ¸ßÁÁÓi*¨ÄÜÈÃÄa¼oÉÙ´iÎo. **20\. ʹÓÃarray_map¿iËÙ´¦ÀiÊý×e **±ÈÈç˵ÄaÏe trim Êý×eÖеÄËuÓÐÔªËØ. ÐÂÊÖ¿ÉÄÜ»a: foreach($arr as $c => $v) { $arr[$c] = trim($v); } µ«Ê¹Óà array_map ¸u¼oµ¥: $arr = array_map('trim' , $arr); Õa»aΪ$arrÊý×eµÄÿ¸oÔªËض¼ÉeÇeµ÷ÓÃtrim. ÁiÒ»¸oÀaËƵĺ¯ÊýÊÇ array_walk. Çe²eÔÄÎĵµÑ§Ï°¸u¶a¼¼ÇÉ. **21\. ʹÓà php filter ÑeÖ¤Êý¾Ý **Äa¿Ï¶¨ÔøʹÓùýÕýÔo±i´iʽÑeÖ¤ email , ipµØÖ*µÈ. ÊǵÄ,ÿ¸oÈ˶¼ÕaôʹÓÃ. ÏÖÔÚ, ÎÒÃÇÏe×o²»Í¬µÄ³¢ÊÔ, ³ÆΪfilter. phpµÄfilterÀ(C)Õ¹Ìa¹(C)Á˼oµ¥µÄ*½Ê½ÑeÖ¤ºÍ¼i²eÊaÈe. **22\. Ç¿ÖÆÀaÐͼi²e** $amount = intval( $_GET['amount'] ); $rate = (int) $_GET['rate']; ÕaÊǸoºÃÏ°¹ß. **23\. Èç¹uÐeÒª,ʹÓÃprofilerÈçxdebug** Èç¹uÄaʹÓÃphp¿ª*¢´oÐ͵ÄÓ¦ÓÃ, php³Ðµ£Á˺ܶaÔËËaÁ¿, ËٶȻaÊÇÒ»¸oºÜÖØÒªµÄÖ¸±e. ʹÓÃprofile°iÖuÓÅ»¯´uÂe. ¿ÉʹÓà xdebugºÍwebgrid. **24\. СÐÄ´¦Ài´oÊý×e **¶ÔÓÚ´oµÄÊý×eºÍ×Ö*u´®, ±ØÐeСÐÄ´¦Ài. ³£¼u´iÎoÊÇ*¢ÉuÊý×e¿½±´µ¼ÖÂÄÚ´aeÒç³o,Å׳oFatal Error of Memory size ÐÅÏ¢: $db_records_in_array_format; //This is a big array holding 1000 rows from a table each having 20 columns , every row is atleast 100 bytes , so total 1000 * 20 * 100 = 2MB $cc = $db_records_in_array_format; //2MB more some_function($cc); //Another 2MB ? µ±µ¼Èe»oµ¼³ocsvÎļþʱ, ³£³£»aÕaô×o. ²»ÒªÈÏΪÉÏÃaeµÄ´uÂe»a¾³£ÒoÄÚ´aeÏÞÖƵ¼Ö½ű¾±ÀÀ£. ¶ÔÓÚСµÄ±aÁ¿ÊÇûÎÊÌaµÄ, µ«´¦Ài´oÊý×eµÄʱºo¾Í±ØÐe±ÜÃa. È*±£Í¨¹ýÒýÓô«µÝ, »o´ae´¢ÔÚÀa±aÁ¿ÖÐ: $a = get_large_array(); pass_to_function(&$a); Õaô×oºo, Ïoº¯Êý´«µÝ±aÁ¿ÒýÓÃ(¶ø²»ÊÇ¿½±´Êý×e). ²e¿´Îĵµ. class A { function first() { $this->a = get_large_array(); $this->pass_to_function(); } function pass_to_function() { //process $this->a } } ¾¡¿iµÄ unset ËuÃÇ, ÈÃÄÚ´aeµÃÒÔÊÍ*Å,¼oÇa½Å±¾¸ºµ£. **25\. ÓÉʼÖÁÖÕʹÓõ¥Ò»Êý¾Ý¿aÁ¬½Ó** È*±£ÄaµÄ½Å±¾ÓÉʼÖÁÖÕ¶¼Ê¹Óõ¥Ò»µÄÊý¾Ý¿aÁ¬½Ó. ÔÚ¿ªÊ¼´¦ÕýÈ*µÄ´o¿ªÁ¬½Ó, ʹÓÃËuÖ±µ½½aÊø, ×iºo¹Ø±ÕËu. ²»ÒªÏñÏÂÃaeÕaÑuÔÚº¯ÊýÖдo¿ªÁ¬½Ó: function add_to_cart() { $db = new Database(); $db->query("INSERT INTO cart ....."); } function empty_cart() { $db = new Database(); $db->query("DELETE FROM cart ....."); } ʹÓöa¸oÁ¬½ÓÊǸoÔa¸aµÄ, ËuÃÇ»aÍÏÂýÓ¦ÓÃ, ÒoΪ´´½¨Á¬½ÓÐeҪʱ¼aºÍÕ¼ÓÃÄÚ´ae. Ìض¨Çe¿oʹÓõ¥Àýģʽ, ÈçÊý¾Ý¿aÁ¬½Ó. **26\. ±ÜÃaÖ±½ÓдSQL, ³eÏoÖ® **²»ÑaÆa*³µÄдÁËÌ«¶aÈçϵÄÓi¾a: $query = "INSERT INTO users(name , email , address , phone) VALUES('$name' , '$email' , '$address' , '$phone')"; $db->query($query); //call to mysqli_query() Õa²»ÊǸo½¨×³µÄ*½°¸. ËuÓÐÐ(C)ȱµa: ÿ´Î¶¼ÊÖ¶¯×ªÒaÖµ ÑeÖ¤²eѯÊÇ*ñÕýÈ* ²eѯµÄ´iÎo»a»¨ºÜ³¤Ê±¼aʶ±ð(³ý*Çÿ´Î¶¼ÓÃif-else¼i²e) ºÜÄÑά»¤¸´ÔӵIJeѯ Òo´ËʹÓú¯Êý*a×°: function insert_record($table_name , $data) { foreach($data as $key => $value) { //mysqli_real_escape_string $data[$key] = $db->mres($value); } $fields = implode(',' , array_keys($data)); $values = "'" . implode("','" , array_values($data)) . "'"; //Final query $query = "INSERT INTO {$table}($fields) VALUES($values)"; return $db->query($query); } $data = array('name' => $name , 'email' => $email , 'address' => $address , 'phone' => $phone); insert_record('users' , $data); ¿´µ½ÁËÂð? ÕaÑu»a¸uÒ׶ÁºÍÀ(C)Õ¹. record_data º¯ÊýСÐĵĴ¦ÀiÁËתÒa. ×i´oµÄÓŵaÊÇÊý¾Ý±»Ô¤´¦ÀiΪһ¸oÊý×e, ÈκÎÓi*¨´iÎo¶¼»a±»²¶»ñ. ¸Ãº¯ÊýÓ¦¸Ã¶¨ÒaÔÚij¸odatabaseÀaÖÐ, Äa¿ÉÒÔÏñ $db->insert_recordÕaÑuµ÷ÓÃ. ²e¿´±¾ÎÄ, ¿´¿´ÔoÑuÈÃÄa´¦ÀiÊý¾Ý¿a¸uÈÝÒ×. ÀaËƵÄÒ²¿ÉÒÔ±aдupdate,select,delete*½*¨. ÊÔÊÔ°É. **27\. Œ¢Êý¾Ý¿aÉu³ÉµÄÄÚÈÝ»º´aeµ½¾²Ì¬ÎļþÖÐ **Èç¹uËuÓеÄÄÚÈݶ¼ÊÇ´ÓÊý¾Ý¿a»ñÈ¡µÄ, ËuÃÇÓ¦¸Ã±»»º´ae. Ò»µ(C)Éu³ÉÁË, ¾ÍŒ¢ËuÃDZ£´aeÔÚÁÙʱÎļþÖÐ. Ï´ÎÇeÇo¸ÃÒ³Ãaeʱ, ¿ÉÖ±½Ó´Ó»º´aeÖÐÈ¡, ²»ÓÃÔÙ²eÊý¾Ý¿a. ºÃ´¦: ½ÚÔ¼php´¦ÀiÒ³ÃaeµÄʱ¼a, Ö´Ðиu¿i ¸uÉÙµÄÊý¾Ý¿a²eѯÒaζןuÉÙµÄmysqlÁ¬½Ó¿ªÏu **28\. ÔÚÊý¾Ý¿aÖб£´aesession **»uÓÚÎļþµÄsession²ßÂÔ»aÓкܶaÏÞÖÆ. ʹÓûuÓÚÎļþµÄsession²»ÄÜÀ(C)Õ¹µ½¼¯ÈºÖÐ, ÒoΪsession±£´aeÔÚµ¥¸o*þÎñÆ÷ÖÐ. µ«Êý¾Ý¿a¿É±»¶a¸o*þÎñÆ÷*ÃÎÊ, ÕaÑu¾Í¿ÉÒÔ½a¾oÎÊÌa. ÔÚÊý¾Ý¿aÖб£´aesessionÊý¾Ý, »¹Óиu¶aºÃ´¦: ´¦ÀiusernameÖظ´µÇ¼ÎÊÌa. ͬ¸ousername²»ÄÜÔÚÁ½¸oµØ*½Í¬Ê±µÇ¼. Äܸu×¼±¸µÄ²eѯÔÚÏßÓû§×´Ì¬. **29\. ±ÜÃaʹÓÃÈ«¾Ö±aÁ¿ **>>ʹÓà defines/constants ʹÓú¯Êý»ñÈ¡Öµ ʹÓÃÀa²¢Í¨¹ý$this*ÃÎÊ **30\. ÔÚheadÖÐʹÓÃbase±eÇ(C)** ûÌý˵¹ý? Çe¿´ÏÂÃae: