1
Chris 0
Updated November 15, 2009 at 6:27 pm
Syntax is really important. Syntax is really, really, important. If you are new to programming, I cannot stress this enough: Syntax is really, really, really important. How important is it? Without proper syntax, your program isn't even a program at all. It's that important.

What is syntax? In both programming and linguistics definitions, syntax refers to the rules and principles for constructing sentences or statements in a language. What does this mean for you? Programming is very precise. One invalid keystroke anywhere in your code could result in the entire program breaking. Remember, computers aren't like humans; they don't read the code like humans might read it. Computers process your code by parsing it, or breaking it down into the rules of a language. If your code is not properly formed, the computer won't be able to break it down and figure out what you want it to do.

You must also ensure that you follow the syntax of the language in which you are programming. Code written in PHP may look entirely different in another language since each language has different rules and is parsed differently. It is for this reason that you need to be very accurate in your programming. Even if code is syntactically correct, if you don't follow the rules of the language, the code may do something you do not expect.

As a new programmer, one of the most time-consuming processes you will face is going through your code to find the one minor symbol you may have forgotten or by finding that extra character that wasn't supposed to be in the code at all. Over time you will train your eye to spot these errors, but until you reach that point, you must be extra careful when copying code or writing new code yourself.

PHP and its place on the web

I have previously referred to PHP as an interpreter. I'm now going to give you a very basic example of what it means to interpret. Before continuing, I would like to remind you that, from here until the end of the series, I will assume you've tested your development environment configuration and the examples given in the previous tutorial. If you were able to view the PHP configuration settings through the test.php file, you shouldn't have a problem continuing. If you did not test your configuration, you should go back and do that now.

We are going to start with the traditional "first program," or the "Hello World" program. Create a folder in your document root named "helloworld." Navigate to this folder in your browser. If you're running the web server on your computer and the localhost address worked in the previous examples, the location of your new directory would be localhost . Create a file in this folder named hello.php, and the contents should be the following:

Hello World
Save the file, refresh your browser, and it should list the hello.php file. If it is not listed, you can navigate to the file directly by appending hello.php to the end of the URL, or by clicking here: localhost . When the page loads you should be greeted by the "Hello World" message.

Great! You're a programmer, right? Not so fast. Just because the file ends in .php does not mean that you actually programmed any PHP code. PHP is an interpreter that interprets code located between PHP tags.

<?php this text is between opening and closing tags ?>
The hello world line you saved in the file was nothing more than plain text. It wasn't true PHP code that was parsed by the interpreter.

Much of our web programming will include a combination of plain text, HTML tags for formatting the page, and PHP code. The plain text and HTML tags are not parsed by the PHP interpreter.

Now let's program our hello world example in true PHP. Empty your hello.php file and copy the following code into it:

<?php echo "Hello World; ?>
Save the file and refresh your browser.

Well that wasn't expected. If you copied the code exactly as noted above, you should have received an error message. More than likely the message reads something like this: "Parse error: syntax error, unexpected $end in hello.php on line 1." This is a parse error message that lets you know there is a problem with your syntax. Can you spot the error?

The problem lies with the message we want to print out. Notice how the hello world text is opened with a quotation mark, but there is no quotation mark at the end of the greeting. You can correct the parse error by adding a quotation mark immediately after the word "world." Also note that the "unexpected $end" error will appear whenever the parser reaches the end of your PHP code while still expecting some closing value or additional code. The missing value is normally a curly brace ( } ), but the above is an example when your code was missing a quotation mark.

Copy the following (correct) code to see the hello world message in all its glory:

<?php echo "Hello World"; ?>
If you refresh the page, you'll notice that this Hello World greeting looks exactly the same as our first example. In fact, as far as your web browser is concerned, the greeting is the same. Why? All PHP code is interpreted by PHP on the web server. Unless you make a mistake in your programming, such as forgetting an opening PHP tag, all PHP code will be processed on the web server before the result is sent out to the user's computer. You don't have to worry about the user stealing any of your code since he or she will never have access to it. Of course there's always the possibility that the server could malfunction or someone could break into your account, and for this reason you will want to write secure code to limit damages to your website, but the average user will not see any of your code under normal operating conditions.

You can also make modifications to this example by adding any text you would like to your hello.php file as long as the text is not between the opening and closing PHP tags. For example, the following greeting will look the same on your computer, but it is a combination of plain text and PHP code:

Hello<?php echo " World"; ?>

Browser parsing and HTML

Since web documents are processed differently than traditional text or document files that you save on your computer, adding more than one space or adding multiple lines to your code will not result in multiple spaces or lines in your output. While the extra spaces are technically passed to your web browser, the browser will ignore them. In fact, the web browser is, much like PHP, its own parser. It expects your HTML or text output to follow certain syntax, and any exceptions to that syntax will be ignored by the browser. In the case of HTML, multiple spaces are not part of its syntax, and as such they will not appear on your screen.

Most browsers will let you view a website's source code, which is simply the output the browser receives from the web server. Normally you can view these additional spaces and/or multiple lines in the browser source even if they don't appear on the page output itself.

Do not confuse the browser source code with the combination of PHP, HTML, and text that you save on your server. In this sense, you can think of PHP as the generator of the source code that your browser can understand. The PHP interpreter is like the translator from the logic code you program in PHP to the HTML formatting code and text your browser uses to display your website on the user's computer.

We will look at a few HTML examples throughout this tutorial series, but our focus will be on the PHP code and not so much the HTML. If you already know HTML or if you want to see how you can use HTML in conjunction with PHP, check out the following example. Note that the < BR > tags are used to add line breaks to the output.

Welcome.<br><?php echo "Line 2<br>Line 3<br>"; ?>
Finally a use for the semicolon
While the semicolon ( ; ) has its uses in the English language, you will not find it used anywhere nearly as much as it is used in programming. While not all programming languages take advantage of the semicolon, many of the traditional languages use it extensively. PHP is one of those languages.

Semicolons can be thought of as the equivalent to periods in the English language. While there are instances where periods aren't used, they can normally be found at the end of sentences. In PHP, semicolons are used at the end of logical statements. While I would like to say that you should end every statement with a semicolon, I would be lying if I did so; however, a vast majority of statements will be marked off with a semicolon. Failure to add them will result in a parse error or some equivalent error. Don't fret! Children will often incorrectly use punctuation when learning to write. Likewise, new programmers will often make similar mistakes with semicolons and syntax in general.

In the next tutorial I will introduce the most fundamental concept in programming: the variable.
1

about mowia
Mowia is a collaborative content management website. You can share your thoughts, experiences, and knowledge with others, and we'll organize that information so anyone can read and learn from it. Live, explore, and share your knowledge with others.
mowia.com  |  myMowia  |  Help Center  |  Privacy  |  Terms

Notice: Mowia is an independent organization that does not provide offical support services for any of the products referenced on the forum nor does it represent any of the companies whose products are referenced. All product copyrights, patents, and trademarks belong to their respective owners.

Valid CSS!
© 2009 mowia.com