Jump to content

Skinning a cat (Or how many ways to output "Hello World!")


benanamen

Recommended Posts

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 by benanamen
Link to comment
Share on other sites

 $b=('010010000110010101101100011011000110111100100000010101110110111101110010011011000110010000100001');
     
$split str_split($b8);
 
$txt '';
     for(
$i 0$l count($split); $i $l$i++) {
         
$txt .= chr(bindec($split[$i]));
     }
     echo 
$txt;

Link to comment
Share on other sites

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]);
  • Like 2
Link to comment
Share on other sites

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 by Psycho
  • Like 1
Link to comment
Share on other sites

<?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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.