In the example above, we placed a random number generated by the web server into the web page. This example demonstrates several advantages of server-side scripting: In its place, the output of the script has appeared, and it looks just like standard HTML. PHP code has been used to generate a random number, but all signs of the PHP code have disappeared when viewing the source in the browser. Try running the script a couple of times and notice how the number changes. This HTML is generated dynamically on the server before being sent to the browser. There’s no HTML file on the server that contains this exact code. If you right-click inside your browser and choose View Source (the label may be different depending on the browser you’re using) you can see that the browser is presented with something like this: The web server is asked to interpret everything between these two delimiters and convert it to regular HTML code before it sends the web page to the requesting browser. To run this code, save it as random.php in the public directory and navigate to. Generating a random number between 1 and 10: php, the web server won’t run any PHP code, and the PHP code will be sent directly to the browser - which doesn’t understand PHP code! php extension, the server then runs it as a PHP script. When someone connects to the server and asks it to load a file with a. PHP code must be placed in a file with a. Anything inside these PHP tags is treated as PHP code and run on the server. Like most HTML tags, PHP has a start tag and and end tag: respectively. While these new uses for JavaScript offer exciting possibilities, there’s still very much a place for PHP - as this book sets out to demonstrate! And more recently still, JavaScript has been used both in the browser and on the server to create database-driven apps. Then along came technologies like Ajax, which allowed JavaScript to communicate with the server. Note: in the early days of the Web - and when the first edition of this book was published! - JavaScript was a client-side scripting language used mainly in the browser. That’s why PHP is called a “server-side language”: its work is done on the server. All the browser sees is standard HTML code when it receives the page. Once the web server has executed the PHP code embedded in a web page, the result takes the place of the PHP code in the page. Whereas client-side languages give you control over how a page behaves once it’s displayed by the browser, server-side languages let you generate customized pages on the fly before they’re even sent to the browser. In contrast, server-side languages like PHP are run by the web server, before the web page is sent to the browser. The key difference between JavaScript and PHP is the stage at which the code is run.Ĭlient-side languages like JavaScript are read and executed by the web browser after downloading the web page (embedded programs and all) from the web server. This concept may be a little difficult to grasp, especially if you’ve only ever designed websites using client-side languages like HTML, CSS, and JavaScript.Ī server-side language is similar to JavaScript in that it allows you to embed dynamically generated content into the HTML code of a web page, giving you greater control over what appears in the browser window than HTML alone can provide. Now that you have your server up and running, it’s time to write your first PHP script.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |