PHP Syntax

Welcome! In this section we're going to learn how PHP and HTML works together

PHP Tags
PHP is a web scripting language which means it was designed to work with HTML. You can easily embed PHP code into your HTMl code. Hmmm! Nice but how do we do that? Simple. When writing php code, you enclose your PHP code in special php tags. This tells the browser that we're working with PHP. See below.

<?php
//your php code goes here
?>

PHP and HTML

Now let see how we blend PHP and HTML together.

<html>
<title>my php page</title>
<body>
<?php
echo "hello there!";
?>
</body>
</html>


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.

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.

My First PHP Page


Open up notepad.
Copy paste the above PHP and HTML code in notepad.
Now, lets save the file in our www folder, i.e. c:/wamp/www Remember, we need PHP to run PHP pages on our computer. To install PHP, please go to the PHP installation page.
Name the file my_php_page.php and save it in your www folder. 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.
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.

<?php
echo "<b>hello there!</b>";
?>>


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.

<?php
echo "<font color='green'>hello there!</font>";
?>
>
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.

<?php
echo "today is ".date('Y-m-d');
?>

Try it! Replace previous echo line with this line. You should see today's date, "today is 2011-02-20", 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!

Web Hosting Reviews
Thousands of web hosting USER reviews. Don't sign-up with any web host before checking out the web's biggest web hosting reviews site first, available at WebHostingReviews.com.