benanamen Posted October 12, 2015 Share Posted October 12, 2015 (edited) We all know in PHP there are many ways to come up with the same exact result. Just for fun, lets see how many ways you can come up with to output the standard Hello World!. I did this on another forum awhile back and there are many. I will start off with the two most basic ways; echo "Hello, World!"; print "Hello, World!"; Edited October 12, 2015 by benanamen Quote Link to comment Share on other sites More sharing options...
scootstah Posted October 12, 2015 Share Posted October 12, 2015 Moving to misc. This could get fun. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 12, 2015 Share Posted October 12, 2015 foreach(array(72,101,108,108,111,44,32,87,111,114,108,100,33) as $l) { echo chr($l); } Quote Link to comment Share on other sites More sharing options...
ignace Posted October 12, 2015 Share Posted October 12, 2015 file_put_contents('php://output', 'Hello, World!'); Quote Link to comment Share on other sites More sharing options...
ignace Posted October 12, 2015 Share Posted October 12, 2015 (edited) $tmp = tmpfile(); fwrite($tmp, 'Hello, World!'); readfile(stream_get_meta_data($tmp)['uri']); fclose($tmp); Edited October 12, 2015 by ignace Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 12, 2015 Author Share Posted October 12, 2015 echo str_replace("JimL","World","Hello JimL!"); Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 12, 2015 Author Share Posted October 12, 2015 echo strrev("!dlroW olleH"); Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 12, 2015 Author Share Posted October 12, 2015 echo str_rot13("Uryyb Jbeyq!"); Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 12, 2015 Author Share Posted October 12, 2015 echo strtr("Happy Coder!","apyCder","eloWrld"); Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 12, 2015 Author Share Posted October 12, 2015 define("CONSTANT", "Hello world."); echo CONSTANT; Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 12, 2015 Author Share Posted October 12, 2015 $helloworld = 'Hello World!'; printf('%s', $helloworld); Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 12, 2015 Author Share Posted October 12, 2015 $helloworld = 'Hello World!'; $helloworld = sprintf('%s', $helloworld); echo $helloworld; Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 12, 2015 Author Share Posted October 12, 2015 $hex = '48 65 6c 6c 6f 20 57 6f 72 6c 64'; foreach (explode(' ', $hex) as $bit) { echo chr(hexdec($bit)); } Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 12, 2015 Author Share Posted October 12, 2015 $test = "Hello World!"; var_export($test); Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 12, 2015 Author Share Posted October 12, 2015 echo base64_decode('SGVsbG8gV29ybGQ='); Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 12, 2015 Author Share Posted October 12, 2015 echo hex2bin("48656c6c6f20776f726c6421"); Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 12, 2015 Author Share Posted October 12, 2015 foreach (range('d', 'w') as $v) { $a[] = $v; } echo ucwords("$a[4]$a[1]$a[8]$a[8]$a[11] $a[19]$a[11]$a[14]$a[8]$a[0]!"); Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 12, 2015 Author Share Posted October 12, 2015 echo <<<EOT Hello World! EOT; Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 12, 2015 Author Share Posted October 12, 2015 foreach (explode(' ', '72 101 108 108 111 32 87 111 114 108 100') as $bit) { echo chr($bit); Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 12, 2015 Author Share Posted October 12, 2015 $a = range('d', 'w'); echo ucwords("$a[4]$a[1]$a[8]$a[8]$a[11] $a[19]$a[11]$a[14]$a[8]$a[0]!"); Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 12, 2015 Author Share Posted October 12, 2015 $b=('010010000110010101101100011011000110111100100000010101110110111101110010011011000110010000100001'); $split = str_split($b, 8); $txt = ''; for($i = 0, $l = count($split); $i < $l; $i++) { $txt .= chr(bindec($split[$i])); } echo $txt; Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 12, 2015 Author Share Posted October 12, 2015 $_="\x70\162\151\x6e\164\x5f\162";$__="\145\170\x65\x63";$___="\x65\143\150\x6f";$____="\110\x65\154\x6c\x6f\040\127\x6f\x72\x6c\144\047"; $_($__("$___ '$____")); Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 12, 2015 Share Posted October 12, 2015 This code grabs the text from this forum post. Sort of a recursive implementation. $url = 'http://forums.phpfreaks.com/topic/298555-skinning-a-cat-or-how-many-ways-to-output-hello-world/'; $content = file_get_contents($url); $output = preg_match("#Or how many ways to output ([^\)]*)#", $content, $match); echo str_replace('"', '', $match[1]); 2 Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 12, 2015 Share Posted October 12, 2015 (edited) You'll have to wait for this one. Shuffle really isn't 'random' - it took 245,760 iterations to get the value (every time) on my machine. $letters = array('H','e','l','l','o',',',' ','W','o','r','l','d','!'); $string = ''; while($string != 'Hello, World!') { shuffle($letters); $string = implode('', $letters); } echo $string; Edited October 12, 2015 by Psycho 1 Quote Link to comment Share on other sites More sharing options...
scootstah Posted October 12, 2015 Share Posted October 12, 2015 <?php $string = ''; $expected = 'Hello, World!'; $biggestChar = 0; for ($i = 0; $i < strlen($expected); $i++) { $biggestChar = max($biggestChar, ord($expected[$i])); } while ($string != $expected) { $string = ''; for ($i = 0; $i < strlen($expected); $i++) { $char = mt_rand(1, $biggestChar); $string .= chr($char); } } echo $string;I've been running this for about 30 minutes now with $expected = 'Hello' and it hasn't finished yet. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.