Difference between revisions of "PHP102"

From mi-linux
Jump to navigationJump to search
Line 4: Line 4:
 
* PHP can output HTML
 
* PHP can output HTML
  
For the vast majority of what you want to accomplish with PHP, you will likely what to output some information in the form of a webpage.  Webpages need to consist of valid HTML, so PHP needs to output '''VALID HTML'''.  We'll continue to use Transitional HTML 4.01 unless you feel confident enough to attempt the Strict version - either will be valid for assessments on CP1079.
+
For the vast majority of what you want to accomplish with PHP, you will likely want to output some information in the form of a webpage.  Webpages need to consist of valid HTML, so PHP needs to output '''VALID HTML'''.  We'll continue to use Transitional HTML 4.01 unless you feel confident enough to attempt the Strict version - either will be valid for assessments on CP1079.
  
 
== Exercise 2 - First VALID PHP/HTML page ==
 
== Exercise 2 - First VALID PHP/HTML page ==

Revision as of 16:12, 5 February 2007

I've got my first file working, what's next?

As you know from CP1082 (HTML), writing valid HTML is critical to the success of a website - this is no different when writing PHP.

  • PHP can output HTML

For the vast majority of what you want to accomplish with PHP, you will likely want to output some information in the form of a webpage. Webpages need to consist of valid HTML, so PHP needs to output VALID HTML. We'll continue to use Transitional HTML 4.01 unless you feel confident enough to attempt the Strict version - either will be valid for assessments on CP1079.

Exercise 2 - First VALID PHP/HTML page

1. Open your preferred editor

2. Code the following exactly - DO NOT CUT AND PASTE - you will not learn by just pasting this.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

 <HTML lang="en">
   <HEAD>
     <META http-equiv="Content-Type" content="text/html;charset=UTF-8">
     <TITLE>My First Valid HTML/PHP page</TITLE>
   </HEAD>
   <BODY>
 <?
   $myname="Matthew";
   echo "Hello - I am ".$myname;
 ?>
   </BODY>
 </HTML>

3. Save this in your public_html folder as "exercise2.php"

4. Open your preferred browser and point it to http://mi-linux.wlv.ac.uk/~YOURSTUDENTNUMBER/exercise2.php

5. The result should look like the following screenshot - if it does not, take a look at Troubleshooting PHP to help understand common error messages in PHP.

Second.png

Understanding Exercise 2

As you can see, you can combine HTML and PHP commands in one file, but the file must have a .php extension for it to be handled correctly by the webserver

  • The first line/s contain the DOCTYPE or DTD - see HTML teaching notes for details on this if you're not sure
  • <HTML lang="en">
    • this is the start of the HTML file with the language attribute set to English - this is used by some validators
  • <HEAD>
    • this is the start of the heading section of the webpage, where components such as title, meta tags, and style are defined
  • <META...>
    • this line is used by some validators to understand the character set the page has been written in - more information can be found on W3C Validator-Character encoding FAQ or more general information on troubleshooting W3 validator responses can be found at W3C Validator FAQ
  • <TITLE...>
    • this sets the title of the browser window that the page appears in - a common mistake is to believe this prints out somewhere in the body of a webpage - it does not - look at your window title bar and taskbar for evidence of where this appears
  • </HEAD>
    • the end of the heading section
  • <BODY>
    • the start of the body of the webpage - the visible output shown in a page
  • <?
    • these symbol indicates the start of PHP content in your page - everything from this point on is executed by the PHP program
  • $myname="Matthew";
    • this creates a variable called "myname" and puts "Matthew" into it.
  • echo "Hello - I am ".$myname;
    • this prints out the sentence "Hello - I am " and adds the content of the "myname" variable to the end of the sentence
  • ?>
    • these symbols indicate the end of the PHP code
  • </BODY>
    • this indicates the end of the body section of the webpage
  • </HTML>
    • this indicates the end of the html/php page

Reuse this page as a template

To save retyping, you should consider copying and pasting this page for your exercises and your assessment - this is a valid framework that you can paste your PHP code into - make sure that any PHP exists between the <? and ?> symbols

Do you understand everything you've done on this page?

If so, take a look at PHP103 - PHP Basics