Home Tutorial PHP Lesson 02 - PHP Basics



MSN Random Online

Lesson 02 - PHP Basics PDF Print E-mail
Written by Administrator   
Thursday, 15 February 2007 18:12

Lesson 02 - PHP Basics

The greatest thing about PHP is the fact that it's a web-specific language.  The language is specifically intended to be used for internet development which means you get many features which makes it easier for you to work with the language on the internet.  One of the greatest things about PHP is how you can mix and match PHP code with HTML code.

If you're familiar with HTML, you know what all HTML tags (special words that format an HTML webpage) are encased in <s and >s.  For example, to bold a string, you would sandwich it between the <b> and </b> tags.  PHP doesn't quite work this way.  Instead, all PHP code goes right inside a huge single tag - the PHP tag.

The PHP tag can look one of two standard ways (there are other alternatives which are rarely used):
<?php ?> - the long version of the tag
or
<? ?> - the short version of the tag

Anything inside these tags will be recognized as PHP code, anything outside these PHP tags will be recognized as plain HTML.  A simple example is the following (again, you don't need to know exactly what the following does right now).  Save the following file into your "www" directory and run it by going to "http://127.0.0.1/basic.php".

basic.php

Code:
<html>
<head>
<title></title>
</head>
<body>

<? print 'php code here'; ?>

</body>
</html>

Some people don't like this method and will instead encase the entire file's contents inside PHP tags and use functions inside the PHP language to output their HTML code.

The other basic thing I want to cover in this lesson is how to "comment" in PHP.  A comment is a way for you to insert explanations of code in plain English which will in NO way affect your program.  Comments help other programmers understand code you've written.  Comments can be written in several ways (save and test the following file):

comments.php
Code:
<?
print 'hello<br>'; //this is a comment, this line prints "hello"

print 'how are you?<br>'; #this is also a comment, this line prints "how are you?"

print 'I am good thank you<br>'; /* this is a multi-line comment
                                    and can span several lines */
?>

Anything after "//" or "#" on a line is interpreted as a comment and will not be used.  Anything between the "/* */" characters will also be interpreted as comments.  Comments can be stuck anywhere in between lines of code or after code on a line, but not before except the "/* and */" comment because then all code after the comment will be interpreted as comments too.

For example:

Code:
<?
print 'hello<br>'; //this is a comment, this line prints "hello"
//here is another comment tag
print 'how are you?<br>'; #this is also a comment, this line prints "how are you?"
#more comments inbetween
/* a comment before with this type of comment tag */ print 'I am good thank you<br>';
/* this is a multi-line comment and can span several lines */
?>

As you've already guessed by now "print" outputs text to the browser, something we'll be going over in our next lesson anyway.

 

 
 

Top Members

No top members yet.