php5

PHP-CLi basic functions

The following functions can help you quickly get going with a CLi project.

#!/usr/bin/env php
<?php
 
// Clear the terminal screen.
// Using the passthru function to fun the local system's clear command
// returns none
function clearTerm()
{
        passthru('clear');
}
 
// Short wrapper for fwrite
// Permits easier explicit writing to STDOUT
// returns none
function toSTDOUT($string)
{
        fwrite(STDOUT,$string);
}
 
// Output with default prefix and suffix
function sendOutput($output = '> ',$NL=PHP_EOL)
{
        toSTDOUT($output.$NL);
}
 
// New exit allowing simple debugging via var_dump()
// Otherwise exits normally
// returns none
function nuExit($str=0)
{
        clearTerm();
        if(is_array($str))
        {
                var_dump($str);
                exit(0);
        }
        exit($str.PHP_EOL);
}
 
// get a trimmed string from STDIN
// returns a string
function getSTDIN()
{
        return trim(fgets(STDIN));
}
 
// get input with a prompt
// built in quit catching
// returns a string or nothing
function getInput($prompt = "> ", $silent = FALSE)
{
        if( ! $silent) { sendOutput($prompt); }
        $var = getSTDIN();
 
        if('quit' == $var) nuExit('Quit entered at prompt.');
 
        return $var;
}
 
// Check input with regex
// returns boolean
function checkMatch($needle,$haystack)
{
        if(preg_match('/^'.$needle.'$/i',$haystack)) return TRUE;
        return FALSE;
}

Now that we have those in place, something like the following is easy enough.

#!/usr/bin/env php
<?php
 
// include the functions here
include "functions.file.php";
 
clearTerm();
sendOutput('Hi there, welcome to a simple CLi functions tutorials.');
$response = getInput('Have you done this before?');
if( checkMatch('([Yy][Ee]?(?:[Ss]|[Aa])?)',$response) )
{
        nuExit('Okay, bye!');
}
else
{
        sendOutput('Welcome to PHP-CLi.');
        nuExit();
}

That’s it.

By Mike on December 18, 2014 | PHP, Tutorials | A comment?
Tags: , , , ,

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: , ,

Before you ob_start(), don’t forget ob_flush() and ob_end_clean()…

Adding compression to your PHP scripts is as easy as:

<?php
// start at the beginning of your output.
ob_start();
 
/*
//////////////////////////////////////////////////
output will be here, whatever it is.
/////////////////////////////////////////////////
*/
 
// and after your output.
ob_flush();
ob_end_clean();
?>

No, really. It is that easy.

By Mike on September 9, 2011 | PHP, Tutorials
Tags: , , ,