Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Outline: What Is PHP? Basic PHP PHP Arithmetic First PHP Sample Program Second PHP Sample Program

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 12

CGS 3066: Introduction to PHP

Outline
1. What is PHP? 2. Basic PHP 3. PHP Arithmetic 4. First PHP Sample program 5. Second PHP Sample program

What is PHP?

PHP is an acronym for PHP: Hypertext Preprocessor PHP is one of the fastest growing server-side scripting languages and is commonly used for web development. It has gained considerable popularity, especially within the Open Source community, because PHP is an openly developed language. PHP is a scripting language, which means that it is interpreted without being compiled (just as JavaScript is not compiled). This makes PHP ideal for rapid development and easy testing (two necessities for web-based applications) What is this going to do for us?
o o

We can now create pages with truly dynamic content PHP can connect to databases like MySQL to provide needed transaction-style services (like buying/selling items with various prices or quantities) PHP can perform other programming tasks, like pseudorandom number generation, looping, and arithmetic We will also see PHP's strength in parsing strings using regular expressions

You can look for more information on PHP at http://www.php.net. There are links to tutorials, as well as a complete reference for the language and its features.

Basic PHP

PHP programming is, in a very real sense, very similar to JavaScript programming. Many of the rules that apply to JavaScript also apply to PHP. Many of the control constructs (if/else statements, loops) are exactly the same in JavaScript and PHP. There are a

number of differences, however, but once you overcome a small learning curve you can do many, many things by applying your JavaScript programming skills. Simple PHP page. Notice that if you view the source, you see only XHTML. This is because PHP is a server-side scripting language. All processing of the PHP takes place at the web server (hence "server-side"); processed PHP typically yields XHTML. The processed PHP is then sent to the client (i.e., the browser) for rendering in the browser window. Notice the lack of the normal XHTML first line, <?xml version="1.0" ?> This is because the PHP script will be generating plain text from the webserver and not true XML specific data. It is still valid XHTML. The DOCTYPE line is still present, and is using XHTML 1.0 Strict as the DTD. PHP blocks are designated by the following tags: <?php $creator = "Mark Searles"; // Variable Declaration ?>
o

These lines are wrapped in tags (<?php ... ?>) that specify the inner content as PHP. The middle line is assigning the value "Mark Searles" to the variable $creator.

All PHP variables start with the dollar sign (e.g. $myvar, $yourvar, $creator, $_REQUEST... )
o

All variable names consist of a mix of upper and/or lower case characters, numbers, or underscores (_). Variables cannot begin with a number. A variable has a type associated with it. In the case of the example, the variable $creator currently is of the type "String". A table of the various data types of PHP

o o

Data Type Integer Double String Boolean Array Object

Description The Integers (Z) you are used to from math (e.g. whole numbers like 6) The Real numbers (R) you are used to from math (e.g. floating point numbers (decimal numbers) like 4.1409) Text enclosed in quotes (either " or ') Has the values of True or False A vector of data of the same type A group of associated data and methods (functions)

Data Type Resource

Description An external data source

Null No value, or the lack of a value There are three styles of comments in PHP
o

Comments using //. Everything after the two slashes until the end of the line is ignored, as can be seen with our script above. Comments using #. Everything after the pound sign until the end of the line is ignored. Comments in /* and */. Everything between these symbols is ignored, including new lines. Comments in PHP are even more important than those in XHTML.

You will notice the standard XHTML page information (head and title, body, ...) on the sample page as well. The next big difference appears as: <?php print($creator); ?>
o

Notice that this PHP action is inside of a paragraph. PHP blocks can appear anywhere on a XHTML page. You also can have as many of them as you want. PHP uses the semicolon to delimit statements. Unlike JavaScript, the semicolon is required at the end of each statement. This line of code prints the value of $creator on the page. print() is a function in PHP. It will print out the values of variables or strings. echo() is another function that you can use for, effectively, the same purpose. Functions work very similar to the way they do in JavaScript. They (optionally) take parameters in, perform operations on those values, and then (optionally) return a result.

o o

PHP String Concatenation Operator

Notice the use of a dot ( . ) inside of the print statements. o The dot acts as the string concatenation operator.
o

Also notice that you can use the $ notation inside of a string in a print statement and it will output the variable value. If you need to print out the letters $x and not the value of some variable, then escape the $ with a backslash (\) so that it looks like \$

PHP Arithmetic

Sample arithmetic in PHP. PHP follows the same rules for order of operations as regular arithmetic, and they can be summed up as follows: Highest Precedence Lowest Precedence Parentheses ( ) ++ -*/% +<< >> = += -= *= /= %= <<= >>= All of the operators should be familiar except the shift operators (<< , >> , <<= , >>=). The operators behave no differently than in JavaScript. The shift operators allow you to shift numbers logically. You are not responsible for the shift operators. They are covered for completeness.
o

x << y does a left shift on x of y bits. The tricky part here is that you have to imagine x as a string of bits (binary digits, base 2). So if we were going to do the operation 3 << 2, first we represent 3 as a bit string (convert it to base 2): 3 => 0011 which means one 1 and one 2 (counting from the right) Now we have to shift it 2 bits to the left. 0011 << 2 => 1100 Now we just convert this number back from binary to decimal: 1100 => 12 So the end result is 12.

>> is the right shift operation. It works similar to the left shift except the bits fall off the right side (if necessary). Other than that it works pretty much the same.

The final thing to notice in the example is the defined constant MYCONST
o

We can define a constant with the following code: define("MYCONST",21); Constants are replaced with their value wherever they appear in the PHP code. So in the sample code, wherever there is a MYCONST, it will be replaced with the

number 21. Note that it does not count if the letters MYCONST appear inside of a string however (e.g. "here is MYCONST" wouldn't become "here is 21").
o

Constants are useful for helping you to make code look clean and neat. If for example you created a program to handle the grades of 100 students, you might later want to change it to handle only 10 students. If you used a constant to define the original 100, then you can quickly change it to 10, thereby replacing maybe 50 or so references to that value in the code. You might be tempted to use a variable in some of these situations, but remember that someone might accidentally then change that variable, so it is safer to just use a constant that by definition cannot be changed (hence the word constant).

CGS 3066: PHP: Additional As mentioned on the Introduction to PHP page, many of the programming concepts are effectively - the same in PHP as in JavaScript. The following statements are written in, PHP, in the same manner as in JavaScript:

Comparison operators (> , < , >= , <= , == , !=) Logical operators (&& , ||) if ... else if ... else statment switch statement for loop

while loop do ... while loop PHP Selection (samp11b.php)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Sample PHP Page 11B</title> <link href="../../cgs3066.css" rel="stylesheet" type="text/css"> </head> <body> <p> <?php $x = 3; $y = 4; if ($x < $y) print( "$x is less than $y<br />" ); elseif ($x == $y) print( "$x is equal to $y<br />" ); else print( "$x is greater than $y<br />" ); $x = 5; if ($x < $y) print( "$x is less than $y<br />" ); elseif ($x == $y) print( "$x is equal to $y<br />" ); else print( "$x is greater than $y<br />" ); $y = 5; if ($x < $y) print( "$x is less than $y<br />" ); elseif ($x == $y) print( "$x is equal to $y<br />" ); else print( "$x is greater than $y<br />" ); if ($x < 7 && $x > 3) {

PHP Selection (samp11b.php) print( "$x is between 3 and 7 exclusive<br />" ); print( "This shows more than one line as part of an If block<br />" ); } elseif ($x > 3) print( "$x is greater than 3<br />" ); elseif ($x < 7) print( "$x is less than 7<br />" ); ?> </p> </body> </html>

PHP Switching (samp11c.php) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Sample PHP Page 11C</title> <link href="../../cgs3066.css" rel="stylesheet" type="text/css"> </head> <body> <p> <?php // Simple switching $x = "abc"; switch ($x) { case "def": print( "x is def" ); break; case "ghi": print( "x is ghi" );

PHP Switching (samp11c.php) break; case "abc": print( "x is abc" ); break; } print( "<br /><br />" ); // Show fall through effect $y = 2; switch ($y) { case 0: print( "y <= 0" ); case 1: print( "y <= 1" ); case 2: print( "y <= 2" ); case 3: print( "y <= 3" ); } print( "<br /><br />" ); // Show default case matching $z = 7; switch ($z) { case 0: print( "z is 0" ); break; case 1: print( "z is 1" ); break; case 2: print( "z is 2" ); break; default: print( "z is not 0, 1, or 2" ); break; } ?>

PHP Switching (samp11c.php) </p> </body> </html>

PHP While Loops (samp12a.php)


<?php print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Sample PHP Page 12A</title> <link href="../../cgs3066.css" rel="stylesheet" type="text/css"> </head> <body> <p> <?php /* Print numbers 0 1 2 3 4 */ $x = 0; while ($x < 5) { print( "$x " ); $x++; } ?> </p> <hr /> <p> <?php /* Print 6 times (count from 5-10 inclusive) */ $y=true; while ($y) { if ($x >= 10) $y=false; print( "y is still true: $x<br />\n" ); $x++; } ?> </p>

PHP While Loops (samp12a.php)


<!-- Validation Icon and Information for page --> <p> <a href="http://validator.w3.org/check/referer"><img src="../valid-xhtml10.png" alt="Valid XHTML 1.0!" height="31" width="88" /></a> <a href="http://jigsaw.w3.org/css-validator/check/referer"><img src="../vcss.png" alt="Valid CSS!" height="31" width="88" /></a> </p> </body> </html>

PHP Do While Loops (samp12b.php)


<?php print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Sample PHP Page 12B</title> <link href="../../cgs3066.css" rel="stylesheet" type="text/css"> </head> <body> <p> <?php /* Print out numbers from 5 down to 1 */ $x = 5; do { print( "$x " ); $x--; } while ($x > 0); ?> </p> <hr /> <p> <?php /* Print 11 times (count from 0-10) */ print( "x : y<br />\n" ); print( "----------------<br />\n" ); $y="s";

PHP Do While Loops (samp12b.php)


do { if ($x >= 10) $y=""; else { $y .= $x; print( "$x : $y<br />\n" ); } $x++; } while ($y); ?> </p> <!-- Validation Icon and Information for page --> <p> <a href="http://validator.w3.org/check/referer"><img src="../valid-xhtml10.png" alt="Valid XHTML 1.0!" height="31" width="88" /></a> <a href="http://jigsaw.w3.org/css-validator/check/referer"><img src="../vcss.png" alt="Valid CSS!" height="31" width="88" /></a> </p> </body> </html>

PHP For Loops (samp12c.php)


<?php print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Sample PHP Page 12B</title> <link href="../../cgs3066.css" rel="stylesheet" type="text/css"> </head> <body> <p> <?php /* Print out numbers from 0 to 4 */ for ($x = 0; $x < 5; $x++) print( "$x " ); ?> </p>

PHP For Loops (samp12c.php)


<hr /> <p> <?php /* Print 10 times (count down from 10-1), testing divisibility by 3 using the modulus function */ for ($y = 10; $y > 0; $y--) { print( "$y " ); if ( $y%3 == 0 ) print( "is divisible by 3" ); print( "<br />\n" ); } ?> </p> <!-- Validation Icon and Information for page --> <p> <a href="http://validator.w3.org/check/referer"><img src="../valid-xhtml10.png" alt="Valid XHTML 1.0!" height="31" width="88" /></a> <a href="http://jigsaw.w3.org/css-validator/check/referer"><img src="../vcss.png" alt="Valid CSS!" height="31" width="88" /></a> </p> </body> </html>

You might also like