Created
June 6, 2011 22:33
-
-
Save smasty/1011261 to your computer and use it in GitHub Desktop.
10 PHP One Liners to Impress Your Friends - http://smasty.net/blog/10-php-oneliners
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? foreach(range(1, 10) as $i) echo $i * 2 . " "; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? echo array_sum(range(1, 1000)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
$words = array("php", "foo", "framework", "apache", "nginx"); | |
$tweet = "This is an example tweet talking about PHP and Apache."; | |
foreach($words as $word) if(stripos($tweet, $word) !== false) echo "$word\n"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? echo file_get_contents("oneliners.php"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? foreach(range(1, 4) as $i) echo "Happy Birthday " .($i == 3 ? "dear Martin" : "to You") . "\n"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? foreach(array(49, 58, 76, 82, 88, 90) as $i) $i > 60 ? ($passed[] = $i) : ($failed[] = $i); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? echo simplexml_load_file("http://search.twitter.com/search.atom?q=php")->asXML(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
echo min(array(14, 35, -7, 46, 98)) . "\n"; | |
echo max(array(14, 35, -7, 46, 98)) . "\n"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? ($pid = pcntl_fork()) == -1 ? exit(1) : ($pid ? hardCoreAction() && pcntl_wait($status) : hardCoreAction()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
// The idea by @tvlooy | |
// and a small bug fix by me. | |
foreach($p = range(2, 100) as $v) foreach(range(2, $v - 1) as $c) if(!($v % $c) && $v != 2) unset($p[$v - 2]); | |
echo join("\n", $p); |
@tvlooy: Thanks for your contributions!
@tvlooy: There was a small bug in your version.
You have to unset index $v - 2
, because the range starts at two.
Also, number two is also a prime number, so it cannot be unset.
ah yes, whoops :-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
number 10:
$p=range(2, 120);
foreach($p as $v)foreach(range(2,$v-1) as $c)if(!($v%$c))unset($p[$v]);
print_r($p);