| [main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql] | |
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. |
PHP SyntaxThese tutorials are about PHP and its use for server-side Web programming. PHP SyntaxPHP 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 (
<?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 ( WhitespaceWhite 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 $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
LiteralsWe 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 Literals come in three flavors. These are:
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
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" ( 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 LiteralString 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 Literalskeyword: 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:
Identifiersidentifier: 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.
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('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. CommentsComments 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 CommentsPHP 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 CommentsSingle 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 ( /* 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 */
These pages can be found at:
[http://academ.hvcc.edu/~kantopet/]
|