Guide to PHP and MySQL (Buffalo State College Version)


Chapter 1: Introduction to PHP

PHP is a popular general-purpose scripting language that is especially suited to web development. (See php.net). PHP code runs on a web server unlike HTML or JavaScript which are handled by a web browser.

PHP code can be embedded in an HTML file usg the <?php and ?> tags.

Hello World!

Most beginning programmers start with the standard "Hello Wolrd" code:

An HTML/PHP Hello World Web Page

<!doctype html>
<html>
  <head>
    <title>The name of this page</title>
  <head>
  <body>
    <h2>Hello!</h2>
    <p><?php echo("Hello World!"); ?></p>
  </body>
</html>

Figure 1-1: Code for helloworld.php

The rendered web page would look like this:

Hello!

Hello World!


Figure 1-2: Hello World PHP Page

PHP Comments

PHP provides a few different way to add comments to your code. You can use multi-line comments or inline (single line) comments or PHPDoc comments. Comments should be used to explain what your code is doing. Comments should be used to provide a description of each function including whether there are parameters and whther the function returns a value or not.

<?php
/* Assignment: Form Web Page
Author: Your Name Here
Date: Month Day, Year file was last edited
Editor: Notepad++ or TextWrangler or whatever editor you used
*/

$i = 0; // Declare a variable named i to be used in a for loop and assign it a value of zero (0)

/**
 * PHPDoc Commments
 * This function does something
 * @param none
 * @return none
 */

function someName() {
  // some code
}
?>

Figure 1-3: PHP Comments

Using download.php

Save this file (download.php) to your cis475 web space. Then in your HTML code use this line:

<a href="download.php?file=index.php">index.php</a><br/>
<a href="download.php?file=vars.php">vars.php</a><br/ >

Figure 1-3: Using download.php

Test to make sure it works for you.

Let's Get Started with Your First PHP Page!
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2016-2018 Jim Gerland