1 / 21

PHP

PHP. Week 12 INFM 603. Agenda. PHP Overview. Comments/echo/print. PHP Code Written in files that end with . php extension Web server that has the php module will be able to process these files PHP could appear embedded in html Different delimeters for php code. We will use:

ashby
Download Presentation

PHP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP Week 12 INFM 603

  2. Agenda • PHP Overview

  3. Comments/echo/print • PHP Code • Written in files that end with .php extension • Web server that has the php module will be able to process these files • PHP could appear embedded in html • Different delimeters for php code. We will use: <?php // PHP CODE HERE ?> • Comments – // everything until end of the line is a comment # everything until end of the line is a comment /* Multiple lines comment */ • echo and print are language constructs used to send data to the browser • Strings can appear in single or double quotes • Example: commentsEchoPrint.php

  4. Escaping into PHP Mode • PHP Parser escapes into PHP Mode when it sees <?php • In a PHP script you are either in PHP Mode or in HTML • Everything within <?php ?> tags is understood by the parser to be PHP code and anything outside (e.g., html, JavaScript, etc.) is simply sent to the browser • You can switch between modes as many times as you want. (We will have more to say about this later on)

  5. Variables • Variable names start with $ and can be followed by _ (underscore), letter, number • We can use = operator to assign values to variables • Statements are terminated with a semicolon • No declaration or specification of variable types (implied from assignment) • We can see variables contents by using echo or print • Scope • Usually a variable is visible within the script is declared or within the function is declared • You can have global variables that can be "shared" across functions or scripts • Example: variables.php

  6. Superglobals • Variables that are always present and whose values are available to all your scripts. (Each variable is actually an array of other variables) • Global Variables $_GET  variables provided to a script through the GET method $_POST  variables provided to a script through the POST method $_COOKIE  variables provided to a script via a cookie $_FILES  variables provided to a script through file uploads $_SERVER  provides information regarding headers, script locations, file paths $_ENV  variables associated with server environment $_REQUEST  variables provided to a script via user input mechanisms $_SESSION  has variables currently registered in a session • Example: globals.php

  7. Data Types • Types integer (e.g., 4,5) float/double (e.g., 8.9, 10.5) string (e.g., "Mary", 'Happy Hour') boolean (true or false) array  ordered set of keys and values object  class instance resource  external resource (e.g., database) NULL  uninitialized variable • You can tell the variable’s type by using gettype or is_* family of functions is_bool, is_int, is_string, is_double, is_numeric, is_resource, is_null, is_array • When printing booleans true is represented as 1 and false as an empty string • In the context of a test expression, zero, an undefined variable, or an empty string will be converted to false. All other values will evaluate to true • Example: types.php

  8. Operators • Assignment Operator (=) • We have an expression when we assign a value to a variable • Example: echo $age = 10; // assigns 10 and expression evaluates to 10 • Typical Mathematical operators are available +, - , * , / , % • String concatenation through a period • Example: “Mary”.”land” • Typical compound assignment operators are available (+=, -=,*=,/=,%=,.=) • Increment/decrement operators (++/--) in both pre/post configuration are available • Example: operators.php

  9. Operators • Following Operators are available <, <=, >, >=, !=, == • Strings can be compared using == or === Example: "john" == "john“ • == vs. === ===  Equivalency with regards to both value and type • Example:comparisons.php

  10. Constants • You can define constants by using php's define() method • Example: define("PI", 3.14); echo "Value of our constant is: ".PI; // no need for $ • Predefined Constants • __FILE__ name of the file the PHP engine is currently reading • __LINE__ current line number of the file • PHP_VERSION version of PHP interpreting the script

  11. Logical Operators • Typical logical operators available • || (logical or) • && (logical and) • ! (negation) • xorleft or right is true but not both (one must be true for the expression to be true) • andandor correspond to && and || (the only difference is the precedence) and are available to make expressions clearer

  12. Conditional Statements • if statements syntactically and semantically as in JavaScript • if else statements syntactically and semantically as in JavaScript • Cascaded if statements syntactically and semantically as in JavaScript. The only difference if that elseif can replace else if • Example:cascadedIf.php • switch statements syntactically and semantically as in JavaScript • Example:switch.php

  13. Iteration Statements • while loop  syntactically and semantically as in JavaScript • Example: while.php • do while loop  syntactically and semantically as in JavaScript • Example: doWhile.php • for loop  syntactically and semantically as in JavaScript • Example: multiplicationTable.php

  14. Functions • Two types: built-in and user-defined • Built-in Examples: • strtoupper takes a string as an argument and returns the string in uppercase • abs returns the absolute value • General Format for User-Defined Function function <functionName>($param1, $param2, ...) { // function body } • Unlike variables, function names are not case sensitive • Example: function.php • We can use return to return values from a function • Example: maximum.php • You can check the existence of a function by using the function function_exists (returns true if the function exists and false otherwise)

  15. Arrays • Three approaches for creating arrays • Implicit creation  By just assigning a value to a variable representing an array Example: $data[1] = 10; • By calling a function that returns an array • By using the array() construct • Example: $languages = array(“C++”, “Java”, “Fortran”); • Example: $dictionary = array(“House” => “Casa”, “Friend” => “Amigo”); • When no explicit indices are specified, array starts at 0 • Indexing of elements possible via [ ] • Iteration of elements possible via foreach statement that has the following syntax: foreach ($array as $arrayElement) • Example: arrayCreation.php • Empty array: $emptyArray = array();

  16. Arrays • Helpful functions • count()/sizeof()  returns the size of the array • array_keys()  returns array with the keys • array_values()  returns array with the values • Example: arrayFunctions.php • Iteration Alternative for Associative Arrays foreach ($array as $key => $value) • Example: arrayFunctions.php • You can use print_r to print information about the array • To delete an element from an array use unset (e.g. unset($data[0])) • Multidimensional Arrays • Arrays that have other arrays stored in them • Example: multidimensionalArrays.php

  17. HTML Forms • Forms - means by which information passes from the user to the server • Tags • <form> defines the form. It has two attributes: • action  indicates where the form contents will be sent when the form is submitted • method defines how the contents will be sent • post contents sent using the HTTP POST method . Content is “hidden” • get contents sent using the HTTP GET method. Contents included in the URL • <input>  appears inside of the <form> tag and defines several input data alternatives The general format is: <input type="ALTERNATIVE" /> where ALTERNATIVE can be text, password, checkbox, radio, file, submit, image, button, reset, hidden

  18. HTML Forms • We can retrieve form information in the target script through $_POST or $_GET • Example: formPost.php/postProcessing.php • Example:formGet.php/getProcessing.php • Example: formsSummary.html/formsSummary.php

  19. Database Access Through PHP • Accessing databases from PHP by using the mysqli_* family of functions • Do not confuse them with mysql • mysqli documentation can be found at http://us3.php.net/mysqli • Functions we will use (non-procedural approach) • mysqli_connect - to connect to the database (returns a database handle) • mysqli_correct_errno - to check for errors • mysqli_close - to close a connection • mysqli_query - to submit a query • mysqli_fetch_array - to generate array with record data

  20. SQL Commands Review • help contents; • show databases; • create database myDB; • use myDB; • show tables; • create table friends (name varchar(20) primary key, gender enum(‘M’,’F’), salary float, id int); • describe friends; • insert into friends values (“Mary”, “F”, 10000, 10); • insert into friends (name) values (“Jose”); • select * from friends where salary > 5000; • select name,id from friends where salary > 5000; • update friends set salary=7778, gender=“F” where name = “Pat”; • delete from friends where name=“Pat”; • show grants; • drop table friends; • drop database myDB;

  21. Accessing the Database • For the following examples we are assuming: • We have created the database myDB and table friends as we saw in the slide “SQL Commands Review” • We have inserted one data record insert into friends values (“Mary”, “F”, 10000, 10); • A user student with password goodbyeWorld has been granted the following privileges (sql command): Two alternatives: grant all on myDB.friends to student@localhost identified by “goodbyeWorld”; grant all on myDB.friends to student@”%” identified by “goodbyeWorld”; • Example: readingDB.php • Let’s run the example with empty table • Let’s run the example after stopping the database system • Let’s run the example providing a wrong user password • Example: insertDB.php • Example: updateDB.php

More Related