[main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql]

HVCC Home
Blackboard HVCC
Blackboard Manual
Faculty Association

php main
1. what is php
2. http basics
3. php basics
a. embedding php
b. including php
* c. php syntax
d. php data types
e. php variables
4. php expressions
5. php client side
6. php flow control
7. php manual


print version

Note that all external links will open up in a separate window.

This is a stripped down version of these pages for older browsers. These pages are really meant to be viewed in a standards compliant browser.

Directions for surfing with audio.

PHP Syntax

These tutorials are about PHP and its use for server-side Web programming.

PHP Syntax

PHP is a C-style language. This means that much of its syntax is similar to C. If you are familiar with most any C-style language (C, C++, Java, JavaScript) then PHP should look very familar. It does, however, have a few little twists and turns you need to know about.

The Semi-Colon

The first rule is that every statement must end in a semi-colon ( ; ). The semi-colon is the programmatic equivalent of the period at the end of the sentence. There are only two exceptions to this rule. One is that if a statement ends in a curly bracket, then there is no semi-colon after the curly bracket. The other is that the semi-colon before the closing PHP tag is optional. In my programming universe, anything that is optional is required. It makes coding much easier if you adopt the same policy.

<?php
  if ($sometest) {
    echo "it seems that the";  // required
    echo " test is positive";  // required
    }                          // forbidden
  else {
    echo "it seems that the";  // required
    echo " test is negative";  // required
    }                          // forbidden
  echo " -- so now you know";  // optional
?>

Case Sensitivity

In PHP, all user-defined variables are case sensitive but nothing else is. If you assume everything is case sensitive it makes life easier though. Convention is that predefined function and method names are written in lower case while predefined variable names are written in upper case. User defined variable names are traditionally written in lower case or C-style mixed case (multiWordVar) to differentiate them from the system variables.

Whitespace

White space is normally ignored in PHP. This means you can space the code out and put line breaks in as you see fit to make the code more readable. Just remember that as in any programming language (except perhaps COBOL) you can't break strings or tokens across lines.

White space can affect coding in certain circumstances. Normally, it should be a common sense issue in determining wher white space is important. For instance, the following code snippet will yield an output of 2.5 -- 25 because the period is the string concatenation operator, but if it is between two numbers with no intervening space, it is a decimal point, just as you would expect it to be.

$xyz = 2.5;
$abc = 2 . 5;
echo $xyz;
echo " -- ";
echo $abc;

Alternately, if you fail to put white space after a variable name, then you have a different variable name. (Yes, putting a variable inside a string literal is allowed in PHP, so the following statements are legal.)

$myVar = "xy";
echo $myVar . "z" // outputs "xyz"
echo $myVar."z"   // outputs "xyz"
echo "$myVar.z";  // outputs "xyz"
echo "$myVar z";  // outputs "xy z"
echo "$myVarz";   // generates an error
                  // since there is no $myVarz

Literals

We will look at some aspects of literals in more detail under data types, but here is a quick overview.

literal: a data value that appears as itself in a program

A literal is an expression that directly represents itself in the code. For instance, the number 2 is a numeric literal, appearing as a number in the code and representing the value of the number two.

Literals come in three flavors. These are:

  • numeric literals
  • string literals
  • keyword literals

Numeric Literal

Numeric literals are numbers that appear as themselves. They may be integers or floating point numbers. Floating point numbers may be represented with a decimal point. They may also be represented in base-10 scientific notation as [number]E[exponent]. for instance, 1.75E-2.

Integers may be represented as decimal numbers or as octal (base-8) or hexadecimal (base-16) numbers. A numeric literal integer beginning with zero is treated as an octal number. A numeric literal integer preceded by a zero and an "x" (0x) is treated as a hexdecimal number. This means that integer numeric literals should not have leading zeros unless you want to signify a different type of enumeration that the base-10 that most people are used to.

All of the following are valid numeric literals:

0       // integer
123     // integer

1.23    // floating point
0.123   // floating point
1.23E5  // scientific notation floation point
        // (1.23 * 105 or 123000)
1.23E-7 // scientific notation floating point
        // (1.23 * 10-7 or .000000123)

0123    // octal
0x123   // hexdecimal

String Literal

String literals are any value that appears in the code inside quotes. A string literal can be any valid character, including numbers. What makes it a string is its use in the code within single or double quote marks.

String literals can either have single or double quotes But unlike some programming languages, which one you use makes a difference on PHP. Double-quoted string literals are parsed string literals while single-quoted string literals are not parsed. What this means is that single-quoted string literals are always represented exactly as entered (except for two special characters we will discuss later), while double-quoted strings can contain non-literal data to be processed at the time the program is run. In other words, PHP provides you with a way to embed variables in string literals.

Keyword Literals

keyword: a word reserved by the language for its own use

A keyword literal is a reserved word in the language that serves as representative of a single literal value. Examples of keyword literals are:

  • true -- Equates to true in a conditional situation.
  • false -- Equates to false in a conditional situation.
  • null -- Is the value assigned to a variable that has no value. It is the value of no value.

Identifiers

identifier: the name assigned to a variable to store data values

An identifier is a name. That name is used to refer to things within a program. That thing may be a variable, a function, an object class, or a constant. An identifier is also a shorthand that allows us to use names for things that make sense to use, instead of to the computer. Without identifiers, we would have to refer to everything by an offset defining its location in memory realitive to the location of this program's allocation of overall computer memory.

All identifiers in PHP must adhere to the following rules.

  • All identifiers must begin with an ASCII letter (A-z a-z)or an underscore ( _ ) or any character in the ASCII range of 0x7F to 0xFF. Those characters will be different for different character sets, but normally represent other European language character sets. Here is the that range from whatever character set you are currently using:
    Ox7f      -- 
    0x80-0x8f -- €    ‚  ƒ  „  …  †  ‡  ˆ  ‰  Š  ‹  Œ    Ž  
    0x90-0x9f --   ‘  ’  “  ”  •  –  —  ˜  ™  š  ›  œ    ž  Ÿ
    0xa0-0xaf --    ¡  ¢  £  ¤  ¥  ¦  §  ¨  ©  ª  «  ¬  ­  ®  ¯
    0xb0-0xbf -- °  ±  ²  ³  ´  µ  ¶  ·  ¸  ¹  º  »  ¼  ½  ¾  ¿
    0xc0-0xcf -- À  Á  Â  Ã  Ä  Å  Æ  Ç  È  É  Ê  Ë  Ì  Í  Î  Ï
    0xd0-0xdf -- Ð  Ñ  Ò  Ó  Ô  Õ  Ö  ×  Ø  Ù  Ú  Û  Ü  Ý  Þ  ß
    0xe0-0xef -- à  á  â  ã  ä  å  æ  ç  è  é  ê  ë  ì  í  î  ï
    0xf0-0xff -- ð  ñ  ò  ó  ô  õ  ö  ÷  ø  ù  ú  û  ü  ý  þ  ÿ
    
    Although there may be special characters in that range, the point is really to make available the characters in other European alphabets. A good rule is that if it is not on your keyboard you probably shouldn't use it as a variable name anyway.
  • After the first character, the digits 0 through 9 are also valid characters.
  • Function and object class names are not case sensitive.
  • Variable names are case sensitive and are always preceded by a dollar sign to identify them as variable identifiers.

The following are valid variable names.

$bob
$_bob
$b7b_1c5
$MixedCase
$_____line
$€_euro;

The following are not valid variable names.

$a-b         // cannot have dash
$bob&ron     // cannot have ampersand
$1c5_b7b     // cannot start with number
$Mixed Case  // cannot have a space
// and assuming you don't see only garbage
$ヴャロアバル  // not in the specified range

Since variables are all case sensitive, the following are all different variables.

$bobandron
$BobandRon
$bobAndRon
$BOBandRON

PHP also has constants. A constant can be thought of as a variable whose value cannot be changed. You set it with a define statement and then can use it in the code in place of the value it represents. Though not used much, constants allow you to set values that are used throughout the code so that the value can be changed by changing the code in one place. Constants are treated as literals whose value cannot be changed by the code.

define('topic', "PHP Programming");
echo topic;

Constants tend to make use of all sorts of programming conventions to differentiate them from other identifiers, like starting them with more than one underscore, of prefixing them with some string, etc. Usually the reason they are not used is that variable can also store values and, if the code is well written, will not get overwritten if they are not meant to be.

Comments

Comments are an important part of coding. They allow you to be able to quickly tell where parts of the program start and end when you are skimming through the code. They also explain how obscure bits of the code work. This not only helps people who have to read your code, but also helps you. Code you wrote a while ago can become very cryptic when you have to go back to modify it.

Coming out of multiple traditions, PHP allows multiple ways to put comments within its code.

Shell Comments

PHP allows Unix shell comments, which are preceded by a hash mark ( # ). Shell comments are single line comments. Everything after it on that line is ignored.

# a shell style comment

########################################
# they are useful for marking off sections
# since hash marks stand out well in the code
########################################

echo $this; # they can also occur after code

Single Line C Comments

Single line C-style comments are single line comments indicated by a double slash ( // ). Anything after the double slash is ignored

// a C-style comment

//========================================
// they don't stand out as well as hash
// marks but are more familiar to many
// people
//========================================

echo $this; // they also can occur after code

Multi-line C Comments

Multi-line C-style comments are comments meant to mark of large multi-line sections of the document. The begin and and with a nested slash asterisk combination ( /* */ ). You should be careful using them to comment out things, since they cannot be nested.

/* a C-style multi-line comment */

/* ****************************************
they are usually used to delimit large blocks
**************************************** */

echo $this; /* they also can occur after code */

/* this on the other hand
/* is a mistake */
because this line isn't commented out */

[top]