Code snippets

When I write a code snippet or a short program, I post the source code here for all to see. Browse through all the code below, or choose a language.

Bug in Matt Mullenweg’s “Hello Dolly”

I was looking through the source code of Matt Mullenweg’s WordPress plugin, Hello Dolly, which is shipped with the latest WordPress releases. It contains a minor bug on line 44:

$chosen = wptexturize( $lyrics[ mt_rand(0, count($lyrics) ) ] );

The range in the mt_rand() is too large. Suppose there is only one line, then mt_rand()’s range will be 0 to 1 — that’s one too much, since only element 0 exists. When this bug occurs, no text will be displayed. The risk for it to occur in the standard Hello Dolly plugin is 1 in 29, which is the number of lines plus one (the 0).

The bug could be fixed easily by changing count($lyrics) to count($lyrics) - 1.

Reverse Blog Name and Post Title in WordPress Title

I thought that it’d look a bit nicer with “Some Post Title — Timblog” as the HTML document’s title instead of “Timblog — Some Post Title”. As a reflex, I went to the WordPress Codex and checked for the functions’ syntax. They had example code for reversing the blog’s name (”Timblog”) and the post’s title (”Some Post Title”):

wp_title(' ');
if(wp_title(' ', false)) { echo '--'; }
bloginfo('name');

However, you should use the trinary operator (A ? B : C) whenever you can, because it’s so cool. I’d prefer this code:

wp_title(' ');
echo ( wp_title( ' ', false ) ? '--' : '' );
bloginfo( 'name' );

Fibonacci with TI Basic

If you want to calculate the first few numbers in the Fibonacci sequence:

: { 1, 1 } -> L1
: 2 -> A
: While 1
: A + 1 -> A
: L1( A - 2 ) + L1( A - 1 ) -> L1
: End

You will end up with L1, a list of the first few Fibonacci numbers up to nearly 10100, where overflow will occur. If you want to print the numbers too, just add this in the while loop (second last line):

: Disp L1( A )

BounceFuck: BounceFuck

This BounceFuck in BounceFuck prints BOUNCEFUCK.

$   O     O    O   /      \\  /O        /         \\  /      -\\
_               O                           O       /
      |  /         /         \\                      _
!               !            !            O         \\----\\
\\   O           Q                         .         /!  -/
     |                                 .  O         -
      |                                             .
         O      O  O      O                         -
\\   Q     O    O    O    O   _        Q/     O   /  \\------./

  /-------O     /        \\        O.----\\    /   \\
                .                               -
                         .                     -
  !             !              O             Q-
  /-----        /              /             Q
                               O             -
                                -            .
                O        O       -
  _              O      O         O-----/    \\   /.

Beautiful, isn’t it?blondes pornstarsbee porn blondie actressporn blondie dagwoodblonds shaving pornblone teen porngothic blood porn dollsbloodrayne pornporn videogame bloodryne Map

BounceFuck: Output Alphabet

This BounceFuck code outputs the alphabet, from A to Z.

 /\\/\\/\\O}\\/\\
$      \\     #
/      /   //>{\\
\\      \\   \\ - .
/      /   / }
\\      \\    /\\ /
/Q\\/\\/\\/ \\/
\\ Q! |

The first memory cell is first incremented to 65 (ASCII A) and the second to 26 (the number of letters in the alphabet). Then, the first is incremented and the second is decremented simultaneously until the second memory cell reaches 0, at which point the script terminates. The first memory cell is printed at every lap in the loop.club pornporn clubbingporn cmic bookcoach pornporna cobrajpegs gay porn cockcock raw pornporn sucking cock sluts Map

Javascript: POST Variables to Forms

It’s possible, and in fact very easy, to send POST variables with Javascript. I’m posting it here because I had some trouble coming up with a solution.

The only thing you have to do is to put an HTML form on a site, and use Javascript to submit it. If you don’t want the form to be visible, you can have the fields hidden. Here is a sample HTML code:

Use this JS function to submit the form:

function jsPost() {
  document.theForm.submit();
}

It can be called in any way, for instance in a hyperlink:


Submit

Or as an event, like on body load:



add ringtone cellular link2am mp3 breathefamily ringtones addamsmp3 6cycle upside mind downable mp3 dvd 504credit 1stadvantage union6buy levitra genericbody mp3 gloves 75547 Mapbig movie titsmovies porn childrensex movies pantyhose freemovie trailers gaymassive cock moviesperfect movies fuckmovie the rent musicalmovies summer cummings Map

How to have Fun Programming: BounceF*ck

Everyone knows that programming can get a bit boring after a while. How many times have you not become bored of writing your code just like you write text, line after line, from left to right? Most have.

That is where esoteric programming languages come into the picture. From Wikipedia:

An esoteric programming language (sometimes shortened to esolang) is a programming language designed as a test of the boundaries of computer programming language design [...]

Give yourself a challenge and test your boundaries, too. In BounceFuck, the code is written in a two-dimensional matrix, and the code flow can be redirected into eight different directions with mirrors and arrows.

Here is the Hello World example from the BounceFuck main page:

/     Q/++++++\\
Q/\\/\\/\\Q/!}| _+
$++++++\\.!++{\\|
/++++++/+++.\\.+
\\++++++\\++++Q++
/++++++/\\O - ++
\\++++++\\  -/+++\\
/++++++/ -/\\++/
\\/\\/\\/\\.Q----\\ /
/-.+++.OQ----/
\\-----.Q
       .

I’ve also written an online interpreter for BounceFuck in PHP.

PHP: Get a Random Element from Array, Biased

This is a function that fetches a random element from an array. However, it’s not like array_rand(), because you can bias the results so that some elements have a better chance of being picked. This is done by having an associated array with the elements to be fetched as keys and the relative probability as the values.

$items = array(
	'A' => 3,
	'B' => 1,
	'C' => 1,
);
 
function getRandElement( $array ) {
	$tot_prob = array_sum( $array );
	foreach ( $array as $element => $element_prob ) {
		if ( mt_rand( 1, $tot_prob ) <= $element_prob ) {
			return $element;
		} else {
			$tot_prob -= $element_prob;
		}
	}
	return 0;
}
 
echo getRandElement( $items );

This code has a 60% chance of echoing an A, 20% chance for B and 20% for C.us aa loansfinance about banking loans scams businessabout low fee loan paydayaccount savings cash loanday loan saving pay accountpay account savings using day loanday saving account loan paybad credit actual loan company Map

PATH: Hello, World

This is a script that prints “HELLO, WORLD”. Note that it is six bytes shorter than it would be if you put all the commands on one line and used only one memory cell.

/.----------\\
{ /\\/\\/\\/.--/\\/\\/\\/\\
 $      \\        /-/\\
 /      /\\       -  .
 \\      \\ .}     \\- /
 /      /        /-
 \\      \\   /\\/\\/./-\\
 /      /   \\ /\\/\\--/
 \\/\\/\\/\\.---.    /--
\\ .-----\\/  \\ /\\/ \\/
    >--!/.   /

after ejaculation viagra150 shipping free tramadol180 tramadol delivery cashaddiction xanax recoveryacyclovir tramadol aldaraadhd xanaxviagra commercial actress inxanax effective 50 mg Mapin michigan casinos allposter casino aladdinall casino beach miami online aboutonline casino all slotscasino 888 marketing affiliateaccount florida offshore internet casino merchantracing casino american andcasino royale rating 007 Map

PATH: Metacode

This is PATH metacode. It takes one character as input, and then outputs PATH code that, if interpreted, outputs what you just inputed.

  /\\/\\ /\\/\\/\\
/}    \\.
,/    /  /{}
\\\\    \\ /{v/.
$/    / . -
 \\/\\/\\ /\\}/

The script first outputs a $ for the code start, after which it enters a loop that outputs a number of equal to the memory cell that was inputed to. Last it outputs a ., which will output the character.cellular ringtone add linkmp3 breathe 2amringtones addams familydown upside mind 6cycle mp3504 dvd mp3 ablecredit 1stadvantage unionlevitra generic 6buy75547 mp3 gloves body Mapporn afganistanporno afrcanporn africa forumstart porn africaafrican american black pornamerican african star pornafrican pornography american sitesafrican porn gay Map

« Later PostsEarlier Posts »
FireStats iconAnvänder FireStats