文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Some PHP Tricks You May Not Know

Some PHP Tricks You May Not Know

时间:2009-04-05  来源:cobrawgl

PHP is a wonderful web programming language. Even though PHP has some very powerful features, it’s quite easy to get buried with the complex stuff that you may not be aware of some really cool PHP tricks. You can use these tricks to speed up your development quite a lot.

Short-Hand Echo

Normally:

view plaincopy to clipboardprint?
  1. <?php echo $variable; ?>  
<?php echo $variable; ?>

Trick:

view plaincopy to clipboardprint?
  1. <?php= $var?>  
<?php= $var?>

Quickly Preview the Contents of an Array

Instead of using the foreach loop to echo the values, you can simply use the code below to preview the indexes and the values associated with each index of an array using the following function.

view plaincopy to clipboardprint?
  1. <?php   
  2.   
  3. $array = array("Color" => "Blue", "Legs" => 4, "PlayStation 3" => "WWE Smackdown VS Raw");   
  4.   
  5. echo '<pre>';   
  6. print_r($array);   
  7. echo '</pre>';   
  8.   
  9. ?>  
<?php $array = array("Color" => "Blue", "Legs" => 4, "PlayStation 3" => "WWE Smackdown VS Raw"); echo '<pre>'; print_r($array); echo '</pre>'; ?> 

Outputs:

  1. Array   
  2. (   
  3.     [Color] => Blue   
  4.     [Legs] => 4   
  5.     [PlayStation 3] => WWE Smackdown VS Raw   
  6. )  
Array ( [Color] => Blue [Legs] => 4 [PlayStation 3] => WWE Smackdown VS Raw )

Print Blocks of Texts

Instead of using the complicated concatenation to join strings, you can use this nifty block command to print your long string.

view plaincopy to clipboardprint?
  1. $text = <<<OPEN   
  2.   
  3. <p>This is awesome!</p>   
  4.   
  5. <p>Indeed!</p>   
  6.   
  7. OPEN;   
  8.   
  9. echo $text;  
$text = <<<OPEN <p>This is awesome!</p> <p>Indeed!</p> OPEN; echo $text;

Force a Secure HTTP Connection

view plaincopy to clipboardprint?
  1. if(!($HTTPS))   
  2. {   
  3.     header("Location: https://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);   
  4.     exit;   
  5. }  
if(!($HTTPS)) { header("Location: https://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']); exit; }

Use Comma Instead of Concatenation

view plaincopy to clipboardprint?
  1. $name = 'roosevelt';   
  2.   
  3. echo 'just testing ', $name;  
$name = 'roosevelt'; echo 'just testing ', $name;

Use isset to check limits, rather than strlen

isset is faster, than strlen

view plaincopy to clipboardprint?
  1. $name = "Jack";   
  2.   
  3. //Slow   
  4. if (strlen($name) < 10)   
  5.     echo "Not long enough!";   
  6.   
  7. //Faster   
  8. if(!isset($name{10}))   
  9.     echo "Not long enough!";  
$name = "Jack"; //Slow if (strlen($name) < 10) echo "Not long enough!"; //Faster if(!isset($name{10})) echo "Not long enough!"; 

Use isset to search in an Array

view plaincopy to clipboardprint?
  1. $Items = array("Rock", "Paper", "Ball", "Bat");   
  2.   
  3. //Slow   
  4. if (in_array('Bat', $Items))   
  5.     echo "Found!";   
  6.   
  7. $Items = array("Rock" => 0, "Paper" => 0, "Ball" => 0, "Bat" => 0);   
  8.   
  9. //Fast   
  10. if (isset($Items['Bat']))   
  11.     echo "Found!";  
$Items = array("Rock", "Paper", "Ball", "Bat"); //Slow if (in_array('Bat', $Items)) echo "Found!"; $Items = array("Rock" => 0, "Paper" => 0, "Ball" => 0, "Bat" => 0); //Fast if (isset($Items['Bat'])) echo "Found!"; 

Quickly Get Date

view plaincopy to clipboardprint?
  1. $Today = getdate();   
  2. $Month = $Today ['month'];   
  3. $Day = $Today ['mday'];   
  4. $Year = $Today ['year'];  
$Today = getdate(); $Month = $Today ['month']; $Day = $Today ['mday']; $Year = $Today ['year'];

Avoid Unnecessary Curley Braces (2nd Bracket)

view plaincopy to clipboardprint?
  1. $test = true;   
  2.   
  3. //Not Good   
  4.   
  5. if($test)   
  6. {   
  7.     echo "Testing!";   
  8. }   
  9.   
  10. //Good   
  11.   
  12. if($test)   
  13.     echo "Testing!";  
$test = true; //Not Good if($test) { echo "Testing!"; } //Good if($test) echo "Testing!"; 

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载