Home arrow PHP arrow Page 2 - PHP and JavaScript, Pooling Your Resources

PHP Disguised as JavaScript? - PHP

As a web programmer, a wide variety of scripting languages are available for you to use. Each language has limitations, but by using more than one in conjunction many of those limitations may be overcome. This article will discuss the benefits of using two such languages, PHP and JavaScript, as well as provide source code for creating a basic demo application.

  1. PHP and JavaScript, Pooling Your Resources
  2. PHP Disguised as JavaScript?
  3. You Want More JavaScript?
  4. How Does This Work?
By: Brian Vaughn
Rating: starstarstarstarstar / 44
September 20, 2005

print this article



One very useful feature of PHP is that it allows you to set header information for the document you are generating. This header information can control a wide variety of things Ė forcing the userís browser to open the file in a certain application, or to redirect the user to another page, or to authenticate the user before proceeding, and so on.

By default, PHP files are treated as an HTML file by a userís browser, since the output rendered by the server is generally text or HTML. The default content type then for an HTML (or PHP) file is "text/html." When a browser encounters this content type, it opens the file itself and renders any HTML markup found within the file appropriately. This is good, as the primary purpose of an HTML page is to format and display information.

A JavaScript file ("text/javascript"), however, is different. It will be rendered in plain text, as its primary purpose is not to display information, but to manage it through the use of statements and functions. Put another way, placing HTML markup in a JavaScript file will result in Javascript errors, not an HTML page. Likewise putting JavaScript in an HTML file (without the appropriate encapsulating tags) will also cause undesirable results in most cases.

So how do we avoid this problem? The answer is simple: by telling our header functions to set the appropriate content type, we are able to tell the browser to treat a PHP file not as a "text/html" file but as a "text/javascript" file. To do this we simply place the following code at the top of our file:

header( 'Content-Type: text/javascript' );

Note: It is important to place the header() function call at the top of our PHP file. Placing it lower may result in compilation errors, as all header information must be sent before any other content is output.

The implications of what we have just discussed may already be clear to you, but in case they are not, let us consider the benefits of generating a JavaScript file with PHP. JavaScript files control various aspects of the userís browser and its display, ranging from simple elements of style and formatting to more complex validation and HTML generation. However, JavaScript is still generally written in a static nature Ė- that is, the code itself doesnít change, only the data it is manipulating.

In many cases where PHP is used to dynamically generate a page and its various HTML objects, this may be limiting. Our JavaScript code may need to know about every object in order to intelligently manage a single object. We can accomplish this goal by using the header tag mentioned above. PHP can now generate not only our HTML, but all associated Javascript code as well -- resulting in yet another level of complexity in our dynamic system.

>>> More PHP Articles          >>> More By Brian Vaughn

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: