PHP tutorials, MySQLi tutorials, by a PHP/MySQLi enthusiastPHP and MySQLi Tutorials -

PHP Standard input and loops on the command line

Getting input from standard in (STDIN) is fairly simple. There are two really easy ways: fgets() and fgetc(), for a string and character, respectively.

Usage of the two is straight-forward. Both take a resource as the first argument, in our case that resource will be STDIN. Let’s try that, shall we?

#!/usr/local/bin/php
<?php
/* Start with a string */
print 'A string: ';
$astring = fgets(STDIN);
print 'A typed string: '.$astring."\n";
 
/* Now a Character */
print 'A character: ';
$achar = fgetc(STDIN);
print 'A typed character: '.$achar."\n";
 
/* Exit cleanly */
exit;

So there it is, getting input from the user of your program.
Now let’s look at the same functionality, in a looping sense. Type q to exit, or use CTRL+C.

#!/usr/local/bin/php
<?php
while(!$exit) {
        /* clear the screen, using execution operators (backticks) */
        print `clear`;
 
        /* Request input */
        print 'Type something: ';
        /* Use fgets with the STDIN resource, and throw it in a variable */
        $var = trim(fgets(STDIN));
 
        /* break if it's q, otherwise keep going
           Redundant exit strategies, make $exit == 1,
                    and thus !$exit is no longer true.
           And break; explicitly tells the while() to 
                        end without going any further. */
        if ($var == 'q') {$exit++; break;}
 
        /* print the response */
        print 'You typed: '.$var."\n";
 
        /* require at least a press of enter to continue */
         fgetc(STDIN);
}
print 'Bye'."\n";
print `sleep 2s;clear`;
exit;

So, there it is! Getting input from standard in (STDIN) with PHP, and using the STDIN in a loop. Also, these functions are great friends with trim(), sanitizing user input is a very important step when dealing with security in your programs. You could even go one step further, and use regular expressions to ensure the type of data coming in, and ensure it’s what you want and need. Hope this was helpful!

By Mike on May 8, 2013 | PHP, Tutorials

PHP on the command line on Unix-like operating systems

Running php on the command line is fairly straightforward. First you must locate PHP:

$ type -path php
/usr/local/bin/php
$

Some /etc/bashrc files contain an alias that allows:

$ which php
/usr/local/bin/php
$

So now that you know where it is, you can create a new php file, starting with your (hash|she)bang:

#!/usr/local/bin/php
<?php
echo 'This is printed to STDOUT'."\n";
?>

Ok, so it’s still just a text file, not something that can.. execute. Make it so:

$ chmod +x file_name
$

Now you can run your PHP script!

$ ./file_name
This is printed to STDOUT
$

There you have it, running a PHP script on the command line of your Unix-like operating system. Running PHP in this fashion retains access to all PHP has running on your web server as a dynamic website. Save for HTTP requests, obviously.

By Mike on May 7, 2013 | PHP, Tutorials

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

MySQLi
PHP
Tutorials