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

Console based app using php

We nor­mally use php for web devel­op­ment pur­pose and it’s pop­u­lar for web devel­op­ment because of it’s ease of use. Any­one who do not know pro­gram­ming can learn basics of php for web devel­op­ment within a week.

In this tuto­r­ial we will see how one can use php to write con­sole based appli­ca­tions. It’s really fun to write con­sole based apps which do not have any user inter­face asso­ci­ated with the pro­gram. We can fol­low the tuto­r­ial to run first php script from console.

This tuto­r­ial explains how we can develop a reser­va­tion sys­tem using php. The below men­tioned steps can be fol­lowed to cre­ate the menu based php appli­ca­tion which can be exe­cuted on console.

Step 1: Cre­ate a php script to the desired loca­tion. I have cre­ated it at E:\php\console with the name reservations.php.

Step 2: Most of the con­sole based apps run using an infi­nite while loop. This loop keep on print­ing the menu after an action is com­pleted. Next we will write the infi­nite while loop as shown below which print the menu with var­i­ous 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 exe­cute the script as men­tioned below:

E:\php\console>php reservations.php

Con­grat­u­la­tions !!!!! We got our first menu which is inter­ac­tive and act based on our actions.

Step 3: Now we will add func­tions 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 func­tion for choice 1 to choose the source location.

Sim­i­larly we can add func­tions for other choices and add logic for each of the func­tion based on our appli­ca­tion requirements.

So friends, in this very basic tuto­r­ial we saw how easy it is to develop a con­sole based php appli­ca­tion. This tech­nique can also be used to develop com­plex soft­ware like a con­sole based server application.

You may also like...

Leave a Reply

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