¸ßÖÊÁ¿PHP´úÂëµÄ50¸öʵÓü¼Çɱر¸£¨ÉÏ£©

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

50¸o¸ßÖÊÁ¿PHP´uÂeµÄʵÓü¼ÇÉ£¬Ï£Íu´o¼Òϲ»¶¡£

*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¼ÑÑ¡ÔñÊÇʹÓþø¶ÔÂ*¾¶:


    view sourceprint?
     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 "<div id='header'>Site Log and Login links</div>";
     }
     function print_footer()
     {
     echo "<div id='footer'>Site was made by me</div>";
     }
     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 = "<div id='header'>Site Log and Login links</div>";
     return $o;
     }
    function print_footer()
     {
     $o = "<div id='footer'>Site was made by me</div>";
     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 = "<response>
     <code>0</code>
     </response>";
    //Send xml data
     echo $xml;

¹¤×÷µÃ²»´i. µ«ÐeÒªÒ»Ð(C)¸Ä½ø.


    $xml = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>';
     $xml = "<response>
     <code>0</code>
     </response>";
     //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:


    <span style="color:#333333;font-family:''Helvetica, Arial, sans-serif'';">if($a == true) $a_count++;</span>

Õa¾ø¶ÔWASTE¡£Ð´³É:


    <span style="color:#333333;font-family:''Helvetica, Arial, sans-serif'';">if($a == true)
     {
     $a_count++;
     }</span>

²»Òª³¢ÊÔÊ¡ÂÔÒ»Ð(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Á¬½Ó.¡£

Õa¸oÃؼ®ÊDz»ÊǺܾ«²Ê£¬ÏaÐÅÒ»¶¨¶Ô´o¼Òѧϰphp³ÌÐoÉe¼ÆÓÐËu°iÖu¡£

 相关文章:
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分页类完整实例