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?- <?php echo $variable; ?>
<?php echo $variable; ?>
Trick:
view plaincopy to clipboardprint?- <?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?- <?php
- $array = array("Color" => "Blue", "Legs" => 4, "PlayStation 3" => "WWE Smackdown VS Raw");
- echo '<pre>';
- print_r($array);
- echo '</pre>';
- ?>
<?php $array = array("Color" => "Blue", "Legs" => 4, "PlayStation 3" => "WWE Smackdown VS Raw"); echo '<pre>'; print_r($array); echo '</pre>'; ?>
Outputs:
- Array
- (
- [Color] => Blue
- [Legs] => 4
- [PlayStation 3] => WWE Smackdown VS Raw
- )
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?- $text = <<<OPEN
- <p>This is awesome!</p>
- <p>Indeed!</p>
- OPEN;
- echo $text;
$text = <<<OPEN <p>This is awesome!</p> <p>Indeed!</p> OPEN; echo $text;
Force a Secure HTTP Connection
view plaincopy to clipboardprint?- if(!($HTTPS))
- {
- header("Location: https://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
- exit;
- }
if(!($HTTPS)) { header("Location: https://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']); exit; }
Use Comma Instead of Concatenation
view plaincopy to clipboardprint?- $name = 'roosevelt';
- 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?- $name = "Jack";
- //Slow
- if (strlen($name) < 10)
- echo "Not long enough!";
- //Faster
- if(!isset($name{10}))
- 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?- $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!";
$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?- $Today = getdate();
- $Month = $Today ['month'];
- $Day = $Today ['mday'];
- $Year = $Today ['year'];
$Today = getdate(); $Month = $Today ['month']; $Day = $Today ['mday']; $Year = $Today ['year'];
Avoid Unnecessary Curley Braces (2nd Bracket)
view plaincopy to clipboardprint?- $test = true;
- //Not Good
- if($test)
- {
- echo "Testing!";
- }
- //Good
- if($test)
- echo "Testing!";
$test = true; //Not Good if($test) { echo "Testing!"; } //Good if($test) echo "Testing!";