Udjuni.com

udjuni.com search:




Combining PHP and HTML in one script


As we have discussed in the Introduction to HTML posting, HTML is mainly a presentation language. If you are going to be dealing with some sort of logic in your scripts, you should consider combining HTML with another language. This posting will show you how to combine HTML with PHP. The basic structure of an HTML page code is as follows:
<html>
    <head> this is where you put information 
telling the browser what the page is about. </head>
<body>
This is where you put text and images visible to the human user. </body> </html>

To combine PHP with HTML we use the PHP code markers
(<?php and ?>)
to indicate the beginning and end of the PHP code. This is meant not to confuse the compiler on how PHP and HTML code should be interpreted. For example:
<html>
    <head> PHP and HTML combination </head>
    <body>
      <?php
      	echo "Printing my PHP data";
      ?>
    </body>
</html>


As you can see from the above example, there is really not too much about it. Now all that you have to do with this file is save it as a .php document and deploy to your web server. As a matter of fact, you can even save your html files as .php and they should run okay. The only difference between the code snippets (one with pure HTML and the one with PHP) is that the one with PHP code in it gives you the flexibility of using logic in your program, where as the one with pure HTML does not.

Example of Using PHP for logic programming


In this code I will demonstrate how useful it is to combine PHP and HTML. Say now that we have a website that sells shoes. The price for shoes of any given brand is determined by the selected shoe size. HTML alone will not be able to compute the pricing logic, but using a combination of HTML and PHP we will compute the price and display it for the customers to see.
<html>
    <head> PHP and HTML combination </head>
    <body>
      <?php
      	switch($shoe_size){
      	  case 9: $price = 89.45;
      	       break;
      	  case 8: $price = 79.45;
      	       break;
      	       
      	  case 7: $price = 69.45;
      	       break;    
      	}
      echo "The price for the shoe size you selected is: $price";	
      ?> 
    </body>
</html>


Code interpretation



Assuming that we supplied the value 9 for variable $shoe_size, this code will evaluate the value, if the value is 9, it is going to return the price of 89.45, if the size if 8, the price will be 79.45 and so forth. If you are having trouble understanding how the switch() function work, please see our posting on PHP control flow functions . It is easy to follow too. In the next few postings we will discuss the use of HTML forms. If you hang in long enough to see this section you will appreciate the value of combining the two languages and how you can use them together to produce dynamic web sites.


RELATED ARTICLES:


Dealing with HTML forms
HTML forms are used to acquire and work with user input. As you may have noticed in the scripts we have used so far, every variable that was meant to hold user input had been given a value within that script. To get the actua

HTML page title
So far we have covered the basics of an HTML file. If you were only developing HTML pages for your personal use, titles and all the things that go into the header section of your script may not matter as much. However chances are that you want to develop a set of pages that you want search engines s

Introduction to HTML
HTML is a computer language used to create websites. The fact that you are looking at this page, means you are also looking at the outcome of html code. It is relatively easy to learn, with the majority of what you really need to know being readily available and for free on the internet.

html reference


Combining Like Terms


breaking code periodic table


PL/SQL code blocks
Like any procedural language, the basic unit of PL/SQL program is a block. Within those blocks you find statements that are executed as a unit. In PL/SQL, blocks are further classified into different categories that include; Labeled blocks - this are used to give a c

Did not find it? Try udjuni.com search:







© 2009 UdjunI LLC. All rights reserved | Privacy policy | Tutorial Index | RSS Feed