Hello Script for PHP

In the last post I introduced the concept of ‘Hello Script’ – a file that contains the most commonly used elements of a programming language so that it can be used as a cheat sheet when working with that language. I thought I can elaborate on that concept by creating Hello Scripts for all the languages that I am familiar with.

Let’s start with PHP – I already provided this as an example for the last post. Here is the entire PHP Hello Script…

// Printing(IO)
print "Hello World!\n";

// Variables, concatenation
$name = 'Binny';
$year = 2008;
print "Hello, " . $name . " - welcome to " . $year . "\n";

//If,else conditions
if ($year > 2008) {
	print "Welcome to the future - yes, we have flying cars!";
else if($year < 2008) {
	print "The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and holy, stay away from your parents!";
else {
	print "Anything wrong with your time machine? You have not gone anywhere, kiddo.";
print "\n";

// For loop
for($i=0; $i<3; $i++) {
	print "$i) Hi there!\n";

//Numerical Array, While
$rules = array(
	'Do no harm',
	'Continue Living'
$i = 0;
while($i<count($rules)) {
	print "Rule " . ($i+1) . " : " . $rules[$i] . "\n";

// Associated array, foreach
$associated = array(
	'hello'	=>	'world',
	'foo'	=>	'bar',
	'lorem'	=>	'ipsum'
foreach($associated as $key => $value) {
	print "$key: $value\n";

// Using Join and Split
$csv_values = explode(',', "hello,world,how,are,you\n");
print implode(":", $csv_values);

// Function, argument, return, call
function hello($name) {
	return "Hello " . $name;
$hello_string = hello("Binny");

// One for the OOP fanboys - Class, members, object and stuff.
class Movie {
	public $name = '';
	public $rating = 0;
	function __construct($name) {
		$this->name = $name;
	function rateMovie() {
		$this->rating = (strlen($this->name) % 10) + 1; //IMDBs rating algorithm. True story!
	function printMovieDetails() {
		print "Movie : {$this->name}\n";
		print "Rating : " . str_repeat('*', $this->rating) . "({$this->rating})\n\n";
//Create the object
$ncfom = new Movie("New Country for Old Men"); //It's a sequel!

// File IO
// File reading, easy method...
$contents = file_get_contents('Hello.php');
print "Hello has " . strlen($contents) . " chars\n";
// Writing to a file
$file_handle = fopen('/tmp/hello.txt', 'w');
fputs($file_handle, "Hello World");

// Command Executing
print `ls`; //Execute the command 'ls' and print its output
print "\n";

// Regular Expressions
$string = "Hello World";
if(preg_match('/^Hell/', $string)) print "Yup - its evil\n";
print preg_replace('/l([^l])/', "$1", $string); //Remove an 'l' from both words. Should print 'Helo Word'

 * Specialized code
// Database connectivity(native)
mysql_connect('localhost', 'root', '') or die("Cannot connect to the Database server");
mysql_select_db('Data') or die('Could not find a database called "Data"');

// Executing Query
$sql_handle = mysql_query('SELECT name,url,description FROM Comment LIMIT 1') or die('Query Error: ' . mysql_error());
while($row = mysql_fetch_assoc($sql_handle)) {
	print "Name	:	$row[name]\n";
	print "URL	:	$row[url]\n";
	print "Desc	:	$row[description]\n\n";
/* Just a note here - if you are going to use PHP with database in a production 
 * system(and you will, trust me), use a Database abstraction layer rather than 
 * the above mentioned native methods.

print $_REQUEST['username']; // Method to get the value of the field 'username' after a form submit. Will not work at CLI execution

Save this to a file and keep it around for future reference – if you are just starting out with PHP

Coming up next – Python Hello Script.

About Binny V A 102 Articles
Freelance Web Developer on the LAMP Platform. Maintains a blogs http://blog.binnyva.com/. Other sites include Bin-Co and OpenJS


  1. Great idea!

    These days I seem to swap around between about 10 different programming languages. A cheat-sheet like this for each language, would be a great timesaver when going back to a language I haven’t used in a few months.

1 Trackback / Pingback

  1. Python Hello Script » Bin-Blog

Leave a Reply

Your email address will not be published.