Tuesday, April 1, 2008

PHP and HTML

Now let see how we blend PHP and HTML together.



my php page

echo "hello there!";
?>


Echo is a special statement in php for outputing data to the browser. This statement is more often used when we need to print something to the browser. You will learn how to use echo in as we go along.



  1. Open up notepad.
  2. Copy paste the above PHP and HTML code in notepad.
  3. Now, lets save the file in our www folder, i.e. c:/wamp/www
  4. Remember, we need PHP to run PHP pages on our computer. To install PHP, please go to the PHP installation page.
  5. Name the file my_php_page.php and save it in your www folder.
  6. All PHP files must be saved with extension .php. This is important because we need to tell the browser that we are working with PHP.
  7. Now, go to http://localhost/my_php_page.php in your web browser.

You should see "hello there!" printed out on the browser. Pretty simple, eh?

There you have it. You first web page in PHP. Now, let’s play around with the code. Let’s do some more cool stuff.

Doing Cool Stuff with PHP and HTML

Echo statement is powerful in a sence that you can put HTML code in it and it will render the text as if it was HTML in your browser. Try it!

Example - Make Text Bold

Replace the PHP line in your code to the following code. Save the file and now refresh your page.

	echo "hello there!";

?>>

You should see hello there! in bold letters in your browser.

Example - Make Text Green

Replace the PHP line in your code to the following code. Save the file and now refresh your page.

	echo "hello there!";

?>>

You should see hello there! in green in your browser.

Example - What's today's date?

Lets print out today's date using the php date function. Don't worry we will see more examples of the date function in later tutorials. We will learn some useful techniques for printing date in different formats. For now, let's try the following code.

	echo "today is ".date('Y-m-d');

?>

Try it! Replace previous echo line with this line. You should see today's date, "today is 2008-04-01", printed out on your browser.

So, in this section we saw how we can add PHP into our HTML code. This should give you a brief glimpse into how PHP works and what we can do with it. You're now ready to move on to some advance PHP stuff! What are we waiting for, let’s go!

Are you still stuck using HTML?
"Learn how to build websites using PHP and MYSQL"

Note: Note that, at the end of each PHP statement we need need to put a semicolon, i.e. ";" as you see in the above code. Otherwise PHP will report syntax error.

Pretty neat, eh? I remember when i first wrote my php code, i was just so amazed :)
So, let do that. Lets write our first PHP page.

Learn PHP and MySQL fast!


Learn how you can start building rich interactive database driven sites like a professional developer easily and quickly...

>> Order your copy now for just $39.95 <<


PHP sits between your browser and the web server. When you type in the URL of a PHP website in your browser, your browser sends out a request to the web server. The web server then calls the PHP script on that page. The PHP module executes the script, which then sends out the result in the form of HTML back to your browser, which you see on the screen. Here is a diagram which illustrate the process.

php process
PHP Process Diagram

What are PHP benefits?

PHP is a free open source language. That means you don't have to pay thousands of dollars in licensing fee to acquire PHP. Best of all, it is easy to install. The most striking feature of it is that it is easy to learn. PHP is used by millions of people and developers around the world. There are thousands of websites on the internet which are written using PHP. One primary example is Yahoo! Bookmarks. PHP has an ever growing community. There are thousands of websites, forums and community websites dedicated on PHP. I built this site to share my PHP examples, recourses and code with you that you can use on your website easily. I will also show you how to build your own PHP website with all the bells and whistles from ground up. You will also learn the fundamentals of PHP and write your own PHP code.

What I need in order to build PHP driven sites?

You need PHP Hosting

You need PHP and MySQl hosting. Good news is, you can find an affordable php and mysql host on the web anywhere. There are tons of web hosting companies trying to compete for your business.

I recommend Netfirms Hosting. For just $9.95/mo, you get PHP4 and PHP5 support, 30 MySQL Databases, 250GB of web space to store your files, videos and images, 2000GB of Data Transfer (plenty for even high traffic websites), 2 FREE Domains, and you can HOST UNLIMITED WEBSITES on the same account and best of all the BEST and QUICKEST customer support I have seen.


I recommend Netfirms because I use Netfirms hosting myself and have no complaints. This is the best hosting I have used so far. They also have a $4.95 plan called the PLUS hosting plan. Just go here and click on their specifications or read the review here