Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Hide Buttons

Console based app using php

We normally use php for web development purpose and it’s popular for web development because of it’s ease of use. Anyone who do not know programming can learn basics of php for web development within a week.

In this tutorial we will see how one can use php to write console based applications. It’s really fun to write console based apps which do not have any user interface associated with the program. We can follow the tutorial to run first php script from console.

This tutorial explains how we can develop a reservation system using php. The below mentioned steps can be followed to create the menu based php application which can be executed on console.

Step 1: Create a php script to the desired location. I have created it at E:\php\console with the name reservations.php.

Step 2: Most of the console based apps run using an infinite while loop. This loop keep on printing the menu after an action is completed. Next we will write the infinite while loop as shown below which print the menu with various choices and quits when we choose option to quit and hit enter.

	while( true ) {
		
		// Print the menu on console
		printMenu();

		// Read user choice
		$choice = trim( fgets(STDIN) );

		// Exit application
		if( $choice == 5 ) {

			break;
		}
	}

	function printMenu() {
		
		echo "************ Reservation System ******************\n";
		echo "1 - Choose Source\n";
		echo "2 - Choose Destination\n";
		echo "3 - Personal Details\n";
		echo "4 - Make Reservation\n";
		echo "5 - Quit\n";
		echo "************ Reservation System ******************\n";
		echo "Enter your choice from 1 to 5 ::";
	}

Now execute the script as mentioned below:

E:\php\console>php reservations.php

Congratulations !!!!! We got our first menu which is interactive and act based on our actions.

Step 3: Now we will add functions for each of the option using a switch case as shown below.

	while( true ) {
		
		// Print the menu on console
		printMenu();

		// Read user choice
		$choice = trim( fgets(STDIN) );

		// Exit application
		if( $choice == 5 ) {

			break;
		}

		// Act based on user choice
		switch( $choice ) {
		
			case 1:
			{
				chooseSource();

				break;
			}
			case 2:
			{
				break;
			}
			case 3:
			{
				break;
			}
			case 4:
			{
				break;
			}
			default:
			{
				echo "\n\nNo choice is entered. Please provide a valid choice.\n\n";
			}
		}
	}

	function printMenu() {
		
		echo "************ Reservation System ******************\n";
		echo "1 - Choose Source\n";
		echo "2 - Choose Destination\n";
		echo "3 - Personal Details\n";
		echo "4 - Make Reservation\n";
		echo "5 - Quit\n";
		echo "************ Reservation System ******************\n";
		echo "Enter your choice from 1 to 5 ::";
	}

	function chooseSource() {
		
		// Logic to choose source location
	}

I have added the switch case to act based on user choice and the dummy function for choice 1 to choose the source location.

Similarly we can add functions for other choices and add logic for each of the function based on our application requirements.

So friends, in this very basic tutorial we saw how easy it is to develop a console based php application. This technique can also be used to develop complex software like a console based server application.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *