Let’s write the following (terrifying, and quite horrifying!) PHP script and try to execute it.

<?php
  echo "Hello World";
?>

Notice: If you don’t know how to create a new PHP project in NetBeans IDE, click here.

In window “index.php”, delete the commented lines (everything except <?php) and type only the first letter from the echo statement by hitting the “e” key on your keyboard. Automatically, a popup window appears, as shown in Figure 1. This window contains all available PHP statements, variables, functions, and other items that begin with the letter “e.”

6

Figure 1 The popup screen in the Source Editor window

You can highlight a selection by using the up and down arrow keys on your keyboard. Notice that each time you change a selection, a second window displays a small Help document about the item selected.

Type the second letter “c” from the echo statement. Now the options have become fewer as shown here.

6b

Select the option echo “|”; and hit “Enter ↵” key. The statement is automatically entered into your script as shown here!

6c

Complete the statement by writing “Hello World” inside double quotes and close the PHP script by writing ?> at the end of it (as in Figure 2). Your NetBeans IDE should look like this.

7

Figure 2 Entering a PHP script in the “index.php” file

Now let’s try to execute the script! From the main menu, select “Run Run File” or hit the SHIFT+F6 key combination. The PHP script executes and the output is displayed in the “Output” window as shown in Figure 3.

8

Figure 3 Viewing the results of the executed script in the Output window

Congratulations! You have just written and executed your first PHP script!

Now let’s write another PHP script, one that prompts the user to enter his or her name. Type the following PHP statements into the NetBeans IDE and hit SHIFT+F6 to execute the file.

<?php
  echo "Enter your name: ";
  $name = trim(fgets(STDIN));
  echo "Hello " . $name . "\n";
  echo "How do you do?";
?>

Remember! You can execute a file by selecting “Run Run File” from the main menu, or by hitting the SHIFT+F6 key combination.

Once you execute the script, the message “Enter your name” is displayed in the “Output” window. The script waits for you to enter your name, as shown in Figure 4.

9

Figure 4 Viewing a prompt in the Output window

To enter your name, however, you must place the cursor inside the “Output” window. Then you can type your name and hit the “Enter ↵” key. Once you do that, PHP interpreter continues executing the rest of the statements. When execution finishes, the final output is as shown in Figure 5.

10

Figure 5 Responding to the prompt in the Output window

Notice: There is an  incompatibility issue between XAMPP and NetBeans IDE. For more information click here.