PHP-CLi, The Flip Side – coin flipper

PHP-CLi, The Flip Side – coin flipper

Below is a simple CLi coin flip I created to help me decide on what to do for lunch.. Don’t forget to change the mode of the file:

$ chmod +x flipcoin

Usage:

$ ./flipcoin # Flips 3 times by default
$ ./flipcoin 1234 # Flips 1234 times
#!/usr/bin/php
<?php
/*
Determine the number of flips.
This assumes you won't be putting in values <=0 
*/
$lim = (array_key_exists(1,$argv)) ? $argv[1] : 3;
 
/*
Set counters, $heads, $tails and $c(ount)
*/
$heads=$tails=$c=0;
for($i=0;$i<$lim;$i++) {
        $c++;
        $int = rand(1,1000);
/*
If $int modulo 2 leaves a remainder, call heads;
else call tails
*/
        if ( $int % 2 ) { echo '!'; $heads++; }
        else { echo '.'; $tails++; }
        if( ($c % 50) == 0 ) { echo "\n"; }
}
/*
Echo the winner, if there was one
*/
echo "\nAfter {$lim} flips: {$heads} Heads(!) and {$tails} Tails(.)\n"
.( ($heads > $tails) ? 'Heads' : ( ($tails == $heads) ? 'Nobody' : 'Tails' ) )
." wins.\n";
 
/* Check for ties, and break them with one last flip  */
if($tails == $heads) {
$int = rand(1,1000);
echo "\n".'Single Tie Breaker Flip:  ';
if ( $int % 2 ) { echo 'Heads Wins.'; }
else { echo 'Tails Wins'; }
echo "\n";
}
exit(0);
?>

It’s nothing crazy, but it is fun to watch 100,000,000 coins being flipped..

After 100000000 flips: 50005803 Heads(!) and 49994197 Tails(.)
Heads wins.

Enjoy!

By Mike on February 2, 2014 | PHP
Tags: , ,

PHP Tutorial: Feeling loppy for() a while()

In PHP there will often be times when you want to repeat a certain amount of code until a goal is reached. You can accomplish this a number of ways, but I’m only going to cover two of them in this tutorial. The first way will be the while() loop, which will continue to execute the code within it, so long as the expression is returning true. The most common way to exit a while loop is to make the expression return false. Other ways include using ‘break;’ and ‘return;’, but we’ll just cover making the expression return false for now. Let’s get started:

<?php
/* first we're going to set a counter variable
something to keep track of how long our while loop as been running 
we'll call it $i and set it to a value of 0 */
$i = 0;
 
/* now we'll construct our while() loop */
while ( $i < 10 ) {
    echo "Current: $i<br />"; // echo out the current value of $i
    $i++; // add 1 to the value of $i
}
?>

The above block will output something like this:
Current: 0
Current: 1
Current: 2
… …
Current: 8
Current: 9

You may be wondering why the first echo is 0 and the last is 9, well that’s easy. We set our variable to 0 (so that’s the first output), then after the first loop, we added 1 to the value of $i, and 1 each loop after that, until $i equaled 10, and since 10 is not less than ( < ) 10, the loop ends at 9. Simple, right? Right. Now, we'll move on to the for() loop, the for() loop takes 3 expressions, all of which can be left blank, but we won't do that this time. Lets try a simple for() loop to get the same result as our previous while() loop.

<?php
/* unlike the while() loop we don't need to set a counter variable
outside, in order to end it.  We can do that right in the first expression */
 
for ( $i = 0 ; $i < 10 ; $i++ ) {
    echo "Current: $i<br />"; // simply echo the current value of $i
}
?>

So, the first expression is evaluated (executed) once before the loop begins.
The second expression works similarly to the while() loop’s expression, so long as it is true the code in the for() loop block is executed.
The third expression is evaluated (execute) after each iteration (loop) of the for() loop.

By Mike on March 24, 2011 | PHP, Tutorials
Tags: , , , ,