this lecture
play

This Lecture PHP Variables PHP and MySQL Arrays IF...ELSE - PDF document

This Lecture PHP Variables PHP and MySQL Arrays IF...ELSE statements Loops Database Systems Connecting to MySQL Further reading Michael Pound W3Schools online tutorials at http://www.w3schools.com/php/ The


  1. This Lecture • PHP • Variables PHP and MySQL • Arrays • IF...ELSE statements • Loops Database Systems • Connecting to MySQL • Further reading Michael Pound • W3Schools online tutorials at http://www.w3schools.com/php/ The Limitations of SQL Extending SQL • SQL is not a general purpose language • Some DBMSs add programming structures such as variables and loops to SQL. • It is designed to create, modify and query databases • Very specific to a DBMS • It is non- procedural, so doesn’t contain normal • Essentially a new language that includes SQL programming constructs • Not hugely flexible • Cannot handle platform-specific challenges such • Connect to SQL from another language as formatting output • Access SQL to run the relevant queries • All other work can be done using procedural code ODBC PHP • Connections to databases from programs are • PHP is a free, server-side scripting language often handled using Open DB Connectivity • Often embedded into web pages to produce • Provides a standard interface for communication dynamic content • Can connect to most modern DBMSs, and those with a DBMS implementing ODBC. • Can run queries, updates etc. • Contains specialised functions for connecting to • Results of queries can be used inside the program MySQL code 1

  2. How does PHP work? Running PHP in the School • Here are the steps required to get PHP to run on the School computers. • All files must be text files with a .php extension. E.g. index.php Web Server • All files must be in, or in a sub-directory of, H:/public_html/ Dynamic • You must have execute rights on these files. To do this you can use Data PHP web page chmod 600 file.php from the command line MySQL Web Interpreter • You can then run the files in an internet browser by going to Database Browser http://avon.cs.nott.ac.uk/~username/file.php PHP File • For more info, visit Page request http://support.cs.nott.ac.uk/help/docs/webpages/lphp/ • Note: You cannot run these php scripts outside the university for security reasons PHP Basics PHP Basics • PHP is procedural code that can be embedded into • You can have any number of php blocks, html documents inside php tags. Like this: separated by html. All php blocks will be <html> connected when the file is run <body> • Code you write in an earlier block can be seen <?php by code you write in later blocks. This will be // This is a comment important later // Some php code goes in here ?> • Anything outside a php block is HTML text </body> </html> Outputting Text Outputting HTML • Inside a PHP block, you can output text using the echo • Remember, you’re working in an HTML document, so anything you output will be read by the browser as HTML: command. Like SQL and C, commands end with a <html> semicolon: <html> <?php <body> echo “<head>”; echo “<title>Title of the Page</title>”; <?php echo “</head>”; echo “This will be output as text!”; ?> ?> <body> </body> </body> </html> </html> 2

  3. Variables Variables • All programming languages use variables as a • Variables in PHP act much like in C, but means to store values using names. For example, remember to always use $ to create a number, called “num1” that has a <?php value of 5: $var1 = 5; $var1 = $var1 + 10; $num1 = 5; ?> Then later: • PHP is a weakly typed language, which means <?php you don’t need to specify that num1 is of type echo $var1; “integer”, because it works it out. ?> Strings Arrays • Sometimes it is more helpful to store variables in lists • Strings are lists of characters rather than as individual names. For example: • Similar to varchar(n) in SQL • Can be declared using ‘single’ or “double” quotes <?php • Can be appended together using ‘.’ $var1 = 2; $var2 = 4; <?php $var3 = 8; $var1 = “Hello”; $var4 = 16; $var2 = “everybody”; $var5 = 32; echo $var1 . “ ” . $var2; echo $var1 . “, ” . $var2 . “, ” . $var3 . “, ” . $var4 . “, ” . $var5; ?> ?> Arrays Arrays • For even a few variables, this will become messy. An • Arrays are usually accessed by a number that alternative is to store them in a list structure, called an represents the position of the variable we want Array • Arrays can also be created and accessed by a keyword: • Arrays act like normal variables, but hold much more <?php data • Arrays are lists, and individual elements are accessed $courses = array(“DBS”=>”Database by the [] operator Systems”, “PRG”=>“Programming”); echo $courses[“DBS”]; <?php ?> $var1 = array(2,4,8,16,32); • Arrays are important because MySQL will give us an echo $var1[3]; array of data when we write a query. ?> 3

  4. Array Examples IF...ELSE $var1 = array( $var2 = array( • Sometimes we might want to choose what “DBS”=>”Database Systems”, 2,4,8,16,32 ); code to run depending on our variables: “PRG”=>“Programming”, “MCS”=> “Maths”); $var1 if (condition) 0 2 $var2 { 1 4 // Code to run if condition is true; “DBS” “Database Systems” 2 8 } “PRG” “Programming” 3 16 “MCS” “Maths” 4 32 IF...ELSE Loops • Conditions can be $var1 = true; • Sometimes we need to run similar code multiple boolean variables, or $var2 = 15; times other expressions. if ($var1) • We can use a loop to run the same code • Conditions will include { repeatedly a single IF, any number // Code • Four types of loops (We can get away with only of ELSE IFs, and then } an optional ELSE while loops for this course) else if ($var2 < 5) • Conditional operators { • WHILE are similar to those in // Code • DO...WHILE MySQL. } • FOR • E.g. <, >, ==, !=, <>, etc. • FOREACH While Loops Do...While Loops • While loops are structured like this: • Do...While loops are structured like this: while (condition) do { { // Code a // Code a } } while (condition); • Code a will be run repeatedly until that condition • Code a will be run once, then repeatedly until is false that condition is false 4

  5. For Loops Foreach Loops • Do...While loops are structured like this: • Foreach loops are not in C, but they are in Java, C++ C#, Objective C, Haskell etc. • Sometimes the foreach will still use the FOR keyword for (initialisation; condition; • Only used for iterating arrays increment) { foreach ($array as $value) { } // Do something with // each $value • Code a will be run once, then repeatedly until } that condition is false Foreach Loops Functions • Foreach loops can also obtain keys for associative • If you wish to reuse code, you can put it in a arrays function to access it later. • There are numerous PHP functions you will foreach ($array as $key => $value) find useful, e.g. { • count($array); // Do something with • mysql_close($connection); // each $key, $value pair } • print_r($array); • mysql_real_escape_string($s, $c) • Foreach loops exist mainly for convenience Functions Functions • Functions are defined like this: • An example of a function. Notice that you need not specify parameter or return types: function <name> (<parameters>) function factorial($val) { { return $val // Do something * factorial ($val – 1); // return; } // or return <value>; • Important : During the coursework, write all functions } in your main index.php file 5

Recommend


More recommend