PHP Tutorials

Server-Side Versus Client-Side Scripting

As already explained, PHP code is processed at the Web server before anything is returned to the browser. This is referred to as server-side processing. Most Web programming works this way: PHP, ASP, Perl, C, and others.

However, a few languages are processed by the browser after it receives the page. This is called client-side processing. The most common example of this is JavaScript.

TIP

Despite the similarity in their names, Java and JavaScript are far from being the same. Many Web developers are familiar with JavaScript, but this does not make them Java programmers. It’s important to remember that these languages are not the same.

This can lead to an interesting problem with logic. The following example demonstrates what I mean:



<script language="JavaScript">

if (testCondition())
{
<?php
echo "<b>The condition was true!</b>";
?>
} else {
<?php
echo "<b>The condition was not true.</b>";
?>
}

</script>


Many times the programmer of such a segment expects only one of the echo statements to execute. However, both will execute, and the page will be left with JavaScript that will generate errors (because the information in the echo statements is not valid JavaScript code). If this is a little unclear, read on; the following demonstration should clear things up for you.

NOTE

If you’re not familiar with JavaScript, don’t worry. The important concept behind this discussion is that PHP, being a server-side language, will be evaluated before the JavaScript, which is a client-side language. This won’t be an issue if you don’t use a client-side scripting language like JavaScript.

The resulting code from the previous snippet follows; notice that the JavaScript has been left intact and untouched, but the PHP code has been evaluated. PHP ignores the JavaScript code completely:



<script language="JavaScript">

if (testCondition())
{
<b>The condition was true!</b>
} else {
<b>The condition was not true.</b>
}

</script>


As you can see, this code will cause JavaScript errors when executed. Be cautious when combining PHP and JavaScript code: It can be done, but it must be done with attention to the fact that the PHP will always be evaluated without regard for the JavaScript. To successfully combine the two, it’s generally necessary to output JavaScript code with PHP.

The following example does just that:



<script language="JavaScript">

if (testCondition())
{
<?php
echo "document.write('<b>The condition was true!</b>');";
?>
} else {
<?php
echo "document.write('<b>The condition was not true.</b>');";
?>
}

</script>

As you can see, doing this gets complicated very quickly, so it’s best to avoid combining PHP and JavaScript. However, the resulting code below shows you that this will work.


<script language="JavaScript">

if (testCondition())
{
document.write('<b>The condition was true!</b>');
} else {
document.write('<b>The condition was not true.</b>');
}

</script>

Filed under: Chapter 1 @ 8:34 pm

Running Your New Program

Following the same procedure outlined at the beginning of this chapter, try running this program.

If your program doesn’t display “Hello, World!” in your browser, go through the next section and try to eliminate reasons why the program might not run.

TIP

A good directory structure should use general categories and narrow those categories through subdirectories. Such a directory structure, if followed consistently, will keep you from ever searching for a file. You will be able to find a file in less than thirty seconds every time.

Mirroring your directory structure on your Web server is also a good sign of organization. The idea is to create a “web root” folder on your hard drive and have it mirror the root public directory on your Web server. Doing this enables you to transfer a copy of your whole Web site between your server and hard drive without worrying about the files being organized differently.

What If It Didn’t Work?
There are quite a few things that could be going wrong, but this section provides a comprehensive list of reasons why your program may not be running. The following is a list of things that might have gone wrong; find the one that describes the behavior of your problem and jump ahead to the appropriate heading.

A Save As dialog box appears.

The page comes up, but the PHP code doesn’t appear to have executed.

The PHP code appears directly in the browser.

A “404 File Not Found” or a “CGI Error—The specified CGI application misbehaved by not returning a complete set of HTTP headers” message appears.

A “Parse error” message appears.

A Save As Dialog Box Appears
If this occurs, PHP is not installed correctly or the file is misnamed. It occurs because the Web server doesn’t recognize the file as a PHP file, but rather as an unknown file type. Since most unknown file types (Zip files, for example) are to be downloaded and not processed, the server is sending the file just as it is to be downloaded by the browser. This surely isn’t the behavior we want.

To fix this, first check to make sure you named your file with a .php extension. If you didn’t, rename it with the Rename command in your FTP client. If you chose to rename the copy on your local hard drive, make sure you transfer the file to the server. Try accessing the page again and see if the problem is solved; if not, repeat the process with .php3, .php4, and .phtml.

It is very possible that none of those will work. In that case, the problem is most likely that your Web server doesn’t have PHP installed or PHP is configured incorrectly. Get in touch with the server administrator to find out if PHP is installed, and, if so, what the correct extension is. If the extension is one that you’ve already tried, explain to the administrators that the extension isn’t working and see if they can help you find out why.

If you are your server administrator, you may need help with checking your configuration; first check the PHP manual (http://www.PHP.net/manual/). If you still have trouble, you may find help on the PHP installation mailing list. Send an email to php-install@lists.php.net including information about your server such as operating system, Web server, and the version of PHP you’re trying to install. The list members will be happy to help.

The PHP Code Doesn’t Appear to Have Executed
If this is the case, you will see only the parts of the page that were outside of the PHP tags. Specifically, you will see “Hello,” printed on the page, but “World!” will be missing. If you use your browser’s View Source command, you will notice that the PHP code appears in your HTML source just like it did in your editor. This means that the file was returned just like a normal HTML file (without any server-side processing).

Check to make sure that your file is named with an appropriate extension (such as .php); this is the most common reason the PHP code wouldn’t execute.

If that fails, read through the section describing what to do if the Save As dialog box appeared; the problem must be that .php isn’t associated with PHP in the Web server’s configuration. That section will help you straighten out your Web server’s configuration.

The PHP Code Appears Directly in the Browser
This is because you entered the code into a WYSIWYG editor such as FrontPage or DreamWeaver. As you entered the code, the editor converted key parts of it (such as the

To correct this, enter the code in a text-only editor, such as Notepad or PHPEd. (See Appendix A for more information about editors.)

A “404 File Not Found” or “CGI Error” Message Appears
The first of these may seem obvious, but it’s not always so obvious if you use Notepad to create your PHP file. One of the problems with using Notepad is its preference for .txt extensions; even if you give your file a .php extension, Notepad adds a .txt.

When the Web server tries to find the .php file you requested, the file isn’t there because it’s really named .php.txt. In most cases, the server would then return a “404 File Not Found” error, but if PHP is installed as a CGI filter, you might get the latter message about incomplete HTTP headers being returned.

In either case, rename the file to .php and try again.

A “Parse Error” Message Appears
This message, mentioned briefly before, means PHP doesn’t know how to interpret something inside of the PHP tags. This isn’t at all uncommon.

For the example shown previously, it probably means you mistyped something. Go back and check to make sure the files match exactly, line for line. Check to ensure that the same quotes are used (double quotes are not the same as two single quotes).

The parse error will be accompanied by a helpful message explaining exactly why your program isn’t running. Check the line that PHP mentions for possible errors, and then check the lines around it.

Filed under: Chapter 1 @ 8:35 pm

What’s next

You should now have a clear understanding of how PHP processes a PHP file. You should also have a basic understanding of PHP’s syntax, including how to use the PHP tags, how and when to use comments, and the importance of statement termination with semicolons.

In the next chapter, you will begin with discussions of variables, variable types, and constants. With this new knowledge, you will be able to store any information you want in the computer’s memory in order to manipulate it and send the results back to the browser, which we will discuss in the coming chapters.

Filed under: Chapter 1 @ 8:36 pm
« Previous Page

Powered by WordPress