No Comments

How to Write Your Very First PHP Program

Advanced programmers know a secret. The secret that they know relates to how a PHP program is constructed.

Here is the secret:

“Complex programs are built up from simple programs. If you can learn how to create a simple program, you can learn how to build a complex program, no matter how complicated.”

A program can be constructed of only a few lines. They always start the same way. Start with the PHP start tag, followed by your programming code, and last by the PHP end tag.

Example 1:</p> <p>Line 1: <php Line 2: // your program goes here Line 3:?>

The PHP program shown above has only 3 lines. Line 1 & 3 are the start and end tags, line 2 is your functional code. In this case however, line 2 is a comment and will not perform any specific action other than to remind you of something. Comments are not processed by the interpreter. Once a comment is encountered, it is basically ignored.

Example 2:</p> <p>Line 1: <php Line 2: // this is one comment Line 3: // this is another comment Line 4: // this is the last comment Line 5:?>

The code in example 1 behaves exactly as the code in example 2. Even though example 2 has 2 more lines. If you noticed, the additional lines in example 2 are only comments. Once again they are ignored by the PHP interpreter (PHP Engine).

If there is one comment or multiple comments, the interpreter will act the same way. It will ignore any comments that it finds.

Example 3:</p> <p>Line 1: <php Line 2: // below this line is a real PHP programming command Line 3: echo 'My First Real Program'; Line 4:?>

In example 3 you will see your first functioning PHP program. The ‘echo’ command was added to this program, located on line 3. An ‘echo’ statement is a built in PHP command that will output whatever follows it to the screen. Anytime you want to output something using PHP to the screen, web browser, or visual display. Use the simple ‘echo’ command.

As you can see, the words ‘My First Real Program’ follows the ‘echo’ command. It is very important to enclose the words that you want ‘echoed’ on the screen inside single or double quotes. This way the echo command knows from what character to start with, and

what character to end with, as it generates the output to the screen.

When the echo command is called, it takes the contents inside the quotes, and sends it out to the screen – minus the quotes. So the output would be:

My First Real Program

Example 4:</p> <p>Line 1: <php Line 2: // the next line is the first echo command Line 3: echo 'You are '; Line 4: // the next line is the second echo command Line 5: echo 'learning how to '; Line 6: // the next line is the last echo command Line 7: echo 'program in PHP.'; Line 8:?>

The output to this program would be:

You are learning how to program in PHP.

When you start programming, there is no limit to how many comments and commands you can enter in your program. Go ahead, try it yourself. As you get better you won’t need so many comments to remind you of what you are doing in your program.

Complex programs are made up of simple programs. Learning how to mix simple commands together, along with PHP start and end tags makes a fully functioning PHP program. As you are learning, a good suggestion is to add as many comments as you need to help remember what you are doing. Comments are like taking notes that you can refer back to. As you get better at programming, you will naturally enter less comments. The PHP ‘echo’ command outputs characters to the screen. Enclose all characters after an ‘echo’ command with single or double quotes.

Congratulations, you just wrote your first PHP program.

You might also like

More Similar Posts

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.