![]() |
Web Conference 2005Writing PHP for ITS/ASET Web servicesThe PHP Language |
![]() |
| <- Back - Your First PHP Script | | | Up | | | Learning Server Info - Next -> |
The examples on this page are highlighted for this course. You should find more detailed explanations via the documentation on php.net.
PHP is a fully featured programming language like many others. It may take a few weeks to competently learn the language and several years to master it. For the purposes of this course, we will illustrate the basic principles. Examples of the concepts below will be demonstrated later in this course.
<?php ...your php code... ?>
<?php $var1 = "sam"; ?>
<? ...your php code... ?>
<? $var1 = "sam"; ?>
<?php echo" ...some expression... "; ?>
<?php echo $_SERVER['REMOTE_USER']; ?>
<?= ...some expression... ?>
<?=$_SERVER['REMOTE_USER']; ?>
A comment is a part of programming code that doesn't do anything functionally, rather it is simply a note for others who read the code.
<html>
<body>
<?php
//
// This is an example of a comment in PHP. It is any text on a line that begins
// with two forward slashes. C++ uses the same style.
//
echo "<p>PHP will print this</p>\n"; // But not this - the rest of the line is a comment
# PHP also understands the Perl and UNIX shell way of creating comments,
# any text on a line that follows a hash (#) character.
#
echo "<p>PHP will print this</p>\n"; # But not this - the rest of the line is a comment
/* Finally, PHP also understands the C method of comments;
* lines that are between a slash/asterisks and an asterisks/slash.
*/
/* Like this. */
echo "<p>PHP will print this</p>\n"; /* But not this - the text between the asterisks is a comment */
?>
<!-- HTML also has its own form of comments. -->
<!--
Since PHP embeds its tags into HTML, you may use HTML comments within the HTML
code like this.
-->
</body>
</html>
A variable is a portion of memory that may be accessed from the program for temporary storage of a value.
It may be accessed by a name you choose. In PHP variables
begin with a dollar sign, such as $var1.
You may set a value to a variable by an assignment
using the equal sign, such as $var1 = "a
tree";. The value always transfers from right to left - so
"a tree" = $var1; would return an error. Note
that equivalency, such as that used in mathematics, is tested by other
symbols such as the double equal sign operator (==).
Similar to other languages, variables may have types or a property of the variable that descibes what types of data it my hold. PHP has several types such as simple (a.k.a. scalar) types: boolean (true or false), integer, float (floating point number) and strings (words); and more complex types such as arrays, objects and resources.
Like Perl, variables can be assigned to new values of different types on the fly. However, there are some differences:
Perl uses other symbols to denote other types, such as @name for (ordered) arrays, or %name for associated arrays or hashes; PHP uses the dollar sign, $name for all types.
PHP has one type of array, which may be used as either a sorted or associative array.
Addition, +.
$var1 + $var2
Subtraction, -.
$var1 - $var2
Multiplication, *.
$var1 * $var2
Division, /.
$var1 / $var2
Modulus (take the remainder), %.
$var1 % $var2
String concatenation operator . (period).
$var1 . $var2 is the same as "$var1$var2".
Equal: $a == $b, return TRUE if both $a and $b have the same value
Identical: $a === $b, return TRUE if both $a and $b have the same value and the same type
Not Equal: $a != $b, return TRUE if $a and $b do not have the same value
Not Identical: $a !== $b, return TRUE if $a and $b either have different values or different types
Less Than: $a < $b, return TRUE if $a is less than $b
Greater Than: $a > $b, return TRUE if $a is greater than $b
Less Than or Equal: $a <= $b, return TRUE if $a is less than or equal to $b
Greater Than or Equal: $a >= $b, return TRUE if $a is greater than or equal to $b
A function in a procedural language like PHP is similar to the mathematical term function in which it:
return value = function_name( argument1, argument2 )
However there are some differences:
Here is an example of code that defines and uses a function called my_fun, which calls other functions:
<html>
<body>
<?php
echo my_fun("you","see","this");
function my_fun ($var1, $var2, $var3)
{
echo ("$var1 $var2 $var3?<br>");
return ("$var3 $var1 $var2<br>");
}
// This should print to the screen:
// you see this?
// this you see
?>
</body>
</html>
A conditional statement or control structure is a device to designate which code instructions are to be followed and in which order. Some basic examples follow:
<html>
<body>
<?php
$var1 = true;
// If the expression in the parenthesis of the if statement resolve to be true
// run the code between the following braces; if not, skip them.
if ( $var1 ) {
echo "<p>The expression is true</p>\n";
}
// You may write a statement that has an else clause, which will be ran if
// the statement turns out to be false.
if ( $var1 ) {
echo "<p>The expression is true</p>\n";
}else{
echo "<p>The expression is false</p>\n";
}
// For statements to be run a multiple of times, the following will run
// as long as the expression is true.
$var1 = 10;
while ( $var1 > 0 ) {
echo "<p>The expression is still true</p>\n";
$var1 = $var1 - 1; // subtract 1 from $var1
// The above may also be written as: $var1 -= 1;
// Since it is a simple decrement, it can also be written as: $var1--;
}
// Loops that are to be ran a specific number of times like the above can
// be simplified into a for loop:
for ( $var1 = 10; $var1 > 0; $var1-- ) {
echo "<p>The expression is still true</p>\n";
}
// Here is a loop that can iterate over each element of an array
$my_array = array("one","two","three","four");
foreach ( $my_array as $element ) {
echo "$element ...\n";
}
// You can also handle the keys and values of an associated array separately
// with ease:
foreach ( $_GET as $parameter => $value ) {
echo "<p>Browser submitted CGI parameter \"$parameter\" set to \"$value\"</p>\n";
}
?>
</body>
</html>
This lesson will concentrate entirely upon procedural programming in PHP, however PHP also has object-oriented capabilities. Similar to Perl, both languages tend to offer most features of the language in both procedural and object-oriented.
| <- Back - Your First PHP Script | | | Up | | | Learning Server Info - Next -> |
|
If you have any questions, feel free to ask me. Content by: Jeff D'Angelo <jcd@psu.edu> © 2005 Last update on: Mon Jun 13, 2005, 2:11:28 AM |
|