Privacy Policy

Community
Standards


Youth Tech's Online PHP Class: Lesson One

Introduction, Basic Format, Output [Chat: 7/12/02]

Hosted by YTCC Spark

Welcome to the first Lesson of Youth Tech's PHP Course.  In this lesson, we will introduce PHP, what it is/can be used for, describe the basic format of ALL PHP pages, and talk about output functions.

What is PHP?

"PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML." -- php.net

Essentially, PHP is an inline scripting language developed for use with HTML.  What that means is that PHP and HTML can be intertwined with each other in a webpage.  This means that not all of the webpage must be generated by the PHP.

What can I use it for?

PHP is typically used to add dynamic content to a website.  PHP can be used to read/write files on the server, to access databases, to include files from other sources, and to execute server-side programs.  It also provides the ability to solve more complex problems than with HTML alone. It can also be used to just generate HTML on the fly allowing maintenance of a page to be simplified.

Some tasks that can be almost entirely handled with PHP include:

  • Shopping Carts
  • User Authentication
  • News Scripts
  • File Upload Scripts

So what does PHP code look like?

First off, PHP is a lot like C and C++.  It is an object-oriented programming language, but with some twists.  Ordinarily, all pages that contain PHP code will end in the extension .php.  There are basically two ways to do a PHP page.  All PHP pages are variations on these two methods:

Method 1: Static with PHP Blurbs

<HTML>
<HEAD>
<TITLE>Page Title</TITLE>
</HEAD>
<BODY>

<!-- SOME HTML CODE HERE -->

<?PHP
// START PHP CODE

// SOME PHP CODE HERE!

// END PHP CODE
?>

<!-- SOME MORE HTML CODE -->

</BODY>
</HTML>

Method 2: Dynamic by Default


<?PHP
// START PHP CODE

// PHP CODE TO PRINT OUT A WHOLE PAGE

// END PHP CODE
?>

There are some cases where you MUST use method 2, but for most of our work, you will be able to use whichever method you prefer.

NOTE: Comments begin with // in PHP and continue to the end of the line.

NOTE: All lines of actual code MUST end with a ;.  The only exceptions will be shown later.

Two basic output functions.

The two basic output functions are print and echo.  These functions are used like this:

print "Hello World";

echo "Hello World";

Now, you might be wondering what the use of these two functions is if they're the same.  Well, there are really two differences.

Print will place a "newline character" at the end of each print statement so that you don't manually have to print it out.  With echo, you really should use:

echo "Hello World\n";

The \n is the representation of a newline.

Echo can also accept multiple arguments, as in:

echo "Hello ", "World!", "\n";

You can use either a single quote (') or a double quote (") unless what you are trying to print contains a variable or a special character (like \n).  More details on that in Lesson Two.

Questions from the Class

Q: Can't I use <? ?> instead of <?PHP ?> ?

A: Well, on some servers yes, on some no.  I recommend going on the side of caution and using the extra 3 letters.  It ensures compatibility on ANY server with PHP, and 3 characters is not much.  If you need to use lots of opening/closing tags and want to type less, then I suggest a search/replace from "<?" to "<?PHP".

Q: What does the mathematical symbol '%' do?

A: While it is technically known as the modulus operator, it essentially is REMAINDER division.  For example, 22%5=2, because 22/5=4, with a remainder of 2.  28%4=0, because 28/4=7 with no remainder.  Because you can't do division by 0, any number %0 is also problematic.  Programmers should try to avoid this in advance and prevent it divide by 0 and mod 0.

Q: In some programming languages, the @ sign must be escaped. ("\@")  Does PHP require this?

A: No, in fact, this could cause problems.  My email address would not be "ytccspark\@youthtech.com", but "ytccspark@youthtech.com". 

Back to the Index

 

Copyright © 1996-2003, Youth Tech
Questions? Comments?