分类:
2009-04-05 20:06:31
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.
Normally:
Trick:
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.
';
"Blue", "Legs" => 4, "PlayStation 3" => "WWE Smackdown VS Raw"); echo ''; print_r($array); echo ''; ?>
Outputs:
Array ( [Color] => Blue [Legs] => 4 [PlayStation 3] => WWE Smackdown VS Raw )
Instead of using the complicated concatenation to join strings, you can use this nifty block command to print your long string.
This is awesome!
Indeed!
$text = <<This is awesome! Indeed!
OPEN; echo $text;
if(!($HTTPS)) { header("Location: https://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']); exit; }
$name = 'roosevelt'; echo 'just testing ', $name;
isset is faster, than strlen
$name = "Jack"; //Slow if (strlen($name) < 10) echo "Not long enough!"; //Faster if(!isset($name{10})) echo "Not long enough!";
$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!";
$Today = getdate(); $Month = $Today ['month']; $Day = $Today ['mday']; $Year = $Today ['year'];
$test = true; //Not Good if($test) { echo "Testing!"; } //Good if($test) echo "Testing!";