ºÜÈÃÈËÊÜ½ÌµÄ Ìá¸ßphp´úÂëÖÊÁ¿36¼Æ

5年以前  |  阅读数:549 次  |  编程语言:PHP 

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 = "";
return $o;
}
function print_footer()
{
$o = "";
return $o;
}
echo print_header();
for($i = 0 ; $i < 100; $i++)
{
echo "I is : $i
';
}
echo print_footer();
ΪʲôÐeÒªÊa³o»º³a:

¿ÉÒÔÔÚ¢Ë͸øa¯ÀÀÆ÷Ç°¸u¸ÄÊa³o. Èç str_replaces º¯Êý»o¿ÉÄÜÊÇ preg_replaces »oÌi¼ÓÐ(C)¼a¿Ø/µ÷ÊÔµÄhtmlÄÚÈÝ.
Ê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 echo $xml; ¹¤×÷µÃ²»´i. µ«ÐeÒªÒ»Ð(C)¸Ä½ø. $xml = ''; $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:
". 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: base ±eÇ(C)*dz£ÓÐÓÃ. ¼ÙÉeÄaµÄÓ¦ÓÃ*ֳɼ¸¸o×ÓĿ¼, ËuÃǶ¼Òª°uÀ¨ÏaͬµÄµ¼º½²Ëµ¥. www.domain.com/store/home.php www.domain.com/store/products/ipad.php ÔÚÊ×Ò³ÖÐ, ¿ÉÒÔд: Home Ipad µ«ÔÚÄaµÄipad.php²»µÃ²»Ð´³É: Home Ipad ÒoΪĿ¼²»Ò»Ñu. ÓÐÕaô¶a²»Í¬°ae±¾µÄµ¼º½²Ëµ¥ÒªÎ¬»¤, ºÜÔa¸a°¡. Òo´Ë, ÇeʹÓÃbase±eÇ(C). Home Ipad ÏÖÔÚ, Õa¶Î´uÂe*ÅÔÚÓ¦Óõĸ÷¸oĿ¼ÎļþÖÐÐÐΪ¶¼Ò»ÖÂ. **31\. ÓÀÔ¶²»ÒªŒ¢ error_reporting ÉeΪ 0 **¹Ø±Õ²»ÏaµÄ´iÎo±¨¸ae. E_FATAL ´iÎoÊǺÜÖØÒªµÄ. ini_set('display_errors', 1); error_reporting(~E_WARNING & ~E_NOTICE & ~E_STRICT); **32\. ×¢Òaƽ̨Ìaϵ½a¹¹ **integerÔÚ32λºÍ64λÌaϵ½a¹¹Ö㤶ÈÊDz»Í¬µÄ. Òo´ËijÐ(C)º¯ÊýÈç strtotime µÄÐÐΪ»a²»Í¬. ÔÚ64λµÄ»uÆ÷ÖÐ, Äa»a¿´µ½ÈçϵÄÊa³o. $ php -a Interactive shell php > echo strtotime("1999-01-01 01:01:01"); -62170005200 php > echo strtotime('1000-01-30'); -30607739600 php > echo strtotime('2100-01-30'); 4104930600 µ«ÔÚ32λ»uÆ÷ÖÐ, ËuÃÇŒ¢ÊÇbool(false). ²e¿´ÕaÀi, Á˽a¸u¶a. **33\. ²»Òª¹ý*ÖÒÀÀµ set_time_limit **Èç¹uÄaÏeÏÞÖÆ×iСʱ¼a, ¿ÉÒÔʹÓÃÏÂÃaeµÄ½Å±¾: set_time_limit(30); //Rest of the code ¸ßÕiÎÞÓÇÂð? ×¢ÒaÈκÎÍa²¿µÄÖ´ÐÐ, Èçϵͳµ÷ÓÃ,socket²Ù×÷, Êý¾Ý¿a²Ù×÷µÈ, ¾Í²»ÔÚset_time_limitsµÄ¿ØÖÆÖ®ÏÂ. Òo´Ë, ¾ÍËaÊý¾Ý¿a»¨*ÑÁ˺ܶaʱ¼a²eѯ, ½Å±¾Ò²²»»aÍ£Ö¹Ö´ÐÐ. ÊÓÇe¿o¶ø¶¨. **34\. ʹÓÃÀ(C)Õ¹¿a** Ò»Ð(C)Àý×Ó: mPDF -- ÄÜͨ¹ýhtmlÉu³ÉpdfÎĵµ PHPExcel -- ¶Áдexcel PhpMailer -- ÇaËÉ´¦Ài*¢ËÍ°uº¬¸½½uµÄÓʼþ pChart -- ʹÓÃphpÉu³É±¨±i ʹÓÿªÔ´¿aÍe³É¸´ÔÓÈÎÎñ, ÈçÉu³Épdf, ms-excelÎļþ, ±¨±iµÈ. **35\. ʹÓÃMVC¿o¼Ü **ÊÇʱºoʹÓÃÏñ codeigniter ÕaÑuµÄMVC¿o¼ÜÁË. MVC¿o¼Ü²¢²»Ç¿ÆÈÄaдÃaeÏo¶ÔÏoµÄ´uÂe. ËuÃǽoŒ¢php´uÂeÓehtml*ÖÀe. Ã÷È*Çø*ÖphpºÍhtml´uÂe. ÔÚÍŶÓЭ×÷ÖÐÓкô¦, Ée¼ÆʦºÍ³ÌÐoÔ±¿ÉÒÔͬʱ¹¤×÷. ÃaeÏo¶ÔÏoÉe¼ÆµÄº¯ÊýÄÜÈÃÄa¸uÈÝÒ×ά»¤ ÄÚ½¨º¯ÊýÍe³ÉÁ˺ܶa¹¤×÷, Äa²»ÐeÒªÖظ´±aд ¿ª*¢´oµÄÓ¦ÓÃÊDZØÐeµÄ ºÜ¶a½¨Òe, ¼¼ÇɺÍhackÒѱ»¿o¼ÜʵÏÖÁË **36\. ʱ³£¿´¿´ phpbench **phpbench Ìa¹(C)ÁËÐ(C)php»u±¾²Ù×÷µÄ»u×¼²aÊÔ½a¹u, ËuչʾÁËÒ»Ð(C)»ÕСµÄÓi*¨±a»¯ÊÇÔoÑuµ¼Ö¾޴o²iÒiµÄ. ²e¿´phpÕ¾µaµÄÆÀÂÛ, ÓÐÎÊÌaµ½IRCÌaÎÊ, ʱ³£ÔĶÁ¿ªÔ´´uÂe, ʹÓÃLinux¿ª*¢. OSCHINAÔ­´´*­Òe
 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例