How to use PHP for Developing Dynamic Website?

[ NOTE: This article just show concept of how to use php for developing dynamic Websites.]

 [ NOTE: If you want to know how php execute practically, Please visit our PRACTICLE tutorial “How to execute PHP files on LOCALHOST using WAMP, MAMP or XAMPP?”  It shows you how to execute php files on LOCAL computer. It also show which software’s and tools used for doing it. ]

Ok, let’s constraint on this topic.

First you know little bit about php.

What is PHP?

PHP is a server side scripting language which is used for handling client side data. Like customer login, registration etc. If you know fundamental of C, C++, java or other objective languages then you know all about PHP. Fundamental of C, C++ includes followings:

  1. A.    Conditional statements: It includes if, if…else, switch etc
  2. B.    Looping statements: It includes while, do…while, for, for…each etc
  3. C.    Functions
  4. D.   Classes
  5. E.    Constructors
  6. F.    Destructors

If you know all these concepts then you know php.

“Now, In this article I just show you how to use php for creating dynamic web sites. For more detail about php visit http://www.php.net.”

 

Check out following example of simple HTML file.

  1. <html>
  2. <style>
  3. body {
  4.                 margin: 0px auto;
  5.                 width: 70%;
  6.                 text-align: center;
  7.                 font-family: arial;
  8.                 color: #999999;
  9. }
  10. .header {
  11.                 border: 1px solid #999999;
  12.                 margin: 1em;
  13. }
  14. .content {
  15.                 border: 1px solid #999999;
  16.                 margin: 1em;
  17. }
  18. .footer {
  19.                 border: 1px solid #999999;
  20.                 margin: 1em;
  21. }
  22. </style>
  23. <body>
  24.                 <div class=”header”>
  25.                                 <h2> THIS IS HEADER  </h2>
  26.                 </div>
  27.                 <div class=”content”>
  28.                                 <h2> THIS IS CONTENT</h2>
  29.                 </div>
  30.                 <div class=”footer”>
  31.                                 <h2> THIS IS FOOTER  </h2>
  32.                 </div>
  33. </body>
  34. </html>

This is code is index.html file

OK. Just see this simple HTML file. It contains 3 div tags with 3 css classes these are header, content & footer respectively. CSS classes are used for modifying color, font & other visual elements. CSS ( Cascading Style Sheet) uses id’s & classes’s for assigning styles for HTML contents. If you want to know all about CSS I strongly recommend you to visit w3school.com.

It show all about CSS properties for making best user interface of websites.

Ok, Now concentrate on our HTML file. Following is the output of index.html file

How to create website using php for creating website by http://mgwebthemes.com | index.php
index.html 

Why use PHP for creating dynamic web pages?

One of the most important features of PHP is avoiding reparative work for making web site. We know web site contain several pages such as Home, About, Contact, Services etc. All of these files contain common data such as header & footer (In this example). When we use html then all of these files contain header & footer.

Suppose we want to make changes in header section then we make those changes on all of these html files. So, this is time consuming and tedious to make changes. Then,

 

How PHP avoid this reparative work?

It is very interesting. PHP allow us to enter html tags into php file. We easily create index.php on our existing file index.html. It shows us similar output as index.html.

But here we create index.php by using 3 files such as header.php, content.php & footer.php by dividing our index.html file.

i.e. header.php + content.php + footer.php = index.php

Such as follows:

header.php

  1. <html>
  2. <style>
  3. body {
  4.                 margin: 0px auto;
  5.                 width: 70%;
  6.                 text-align: center;
  7.                 font-family: arial;
  8.                 color: #999999;
  9. }
  10. .header {
  11.                 border: 1px solid #999999;
  12.                 margin: 1em;
  13. }
  14. .content {
  15.                 border: 1px solid #999999;
  16.                 margin: 1em;
  17. }
  18. .footer {
  19.                 border: 1px solid #999999;
  20.                 margin: 1em;
  21. }
  22. </style>
  23. <body>
  24.                 <div class=”header”>
  25.                                 <h2> THIS IS HEADER  </h2>
  26.                 </div>

 

content.php

  1.                 <div class=”content”>
  2.                                 <h2> THIS IS CONTENT</h2>
  3.                 </div>

 

footer.php

  1.                <div class=”footer”>
  2.                                 <h2> THIS IS FOOTER  </h2>
  3.                 </div>
  4. </body>
  5. </html>

 


Ok, we include all these file into index.php file by using special function of php i.e. include & require functions. See how we use include function to include these files in index.php file.

index.php

  1. <?php
  2.               include(“header.php”);
  3.               include(“content.php”);
  4.               include(“footer.php”);
  5. ?>


This is Index.php, Here we show little difference between include & require functions.

 

Include: This function used for include files into php file. If that included file is contain any error like code mistakes or other mistakes then it does include file without show error message.

 

Require: This function also includes a file but if this included file contain any mistake or error then it does not include that file it show FATTEL error.

 

These are also some function like include_once, require_once. For more details visit www.php.net

 

So, our index.php file is group of 3 files header.php, content.php & footer.php when we want to update of any one of the common file then we easily update changes. These changes are made by once and applicable on all of those.

 

Suppose, We want to create about.php Then just create body of about page e.g. but before that include header.php & footer.php i.e. following

  1. 1.    <?php include(“header.php”); ?>
  2.   <div>
  3.                                 <h2> This is about us page.</h2>
  4. </div>
  5. 7.    <?php include(“footer.php”); ?>

 

  This is about.php file

When we want to update header then we just open header.php and make changes. These are easily updated on all of pages which are including header.php file.

I think you got the answer of the question How to create website using php for creating website?

There are various online sources available to learn php. I recommend you w3school.com to learn PHP, MYSQL, HTML, CSS & others. It provides best tutorials for beginner.

 

If you have any questions feel free to contact me at admin@mgwebthemes.com.

Thank you.

Advertisements

Author: maheshwaghmare

WordPress Developer since 2012

1 thought on “How to use PHP for Developing Dynamic Website?”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s