Lambda Functions Originate from Lambda Calculus Which Was Introduced by Alonzo Church and Stephen Cole Kleene in the 1930S

Lambda Functions Originate from Lambda Calculus Which Was Introduced by Alonzo Church and Stephen Cole Kleene in the 1930S

TDP013 PHP Anders Fröberg, [email protected] Dept. of Computer and Information Science, Linköping University fredag, 2009 september 18 Outline ● PHP History/background ● Server side scripting ● Hello World example ● Basic syntax ● What is new in 5.3 ● Database drive web applications in PHP ● SOAP and REST fredag, 2009 september 18 History/Background ● Created by Rasmus Lerdorf in 1995 – PHP/IF Personal Home Page / Forms Interpreter – A set of perl scripts and C -code ● PHP 3 1997 – Andi Gutmans and Zeev Suraski – Complete re-write ● PHP 4 1998 ● PHP 5 2004 July fredag, 2009 september 18 PHP ● recursive acronym "PHP: Hypertext Preprocessor" ● or "Pretty Hypertext Preprocessor". ● Used for server side applications ● LAMP – Linux, Apache, MySQL, PHP ● WAMP – Windows, Apache, MySQL, PHP ● WIMP – Windows, IIS, MySQL, PHP fredag, 2009 september 18 Server Side Scripting/ Client Side Server Side HTML,CSS, PHP/JSP/ASP/SQL JavaScript HTTP Request (URL http://www.ida.liu.se) Web server Databas e server HTTP Response HTML/CSS/JavaScript fredag, 2009 september 18 HTTP Request ● A file request with optional arguments ● 2 types of Request – GET ● File and arguments encoded in the URL – http://www.hotmail.com/viewMails.asp? login=anders&password=NisseLasse – POST ● File request – Arguments passed as a stream afterwards – Not visible in the URL fredag, 2009 september 18 Step-by-Step request ● Users types in a URL hits enter ● The web browser send the request to the server ● The web server receives the request and looks up the file – If the file exists send it to the browser – If not send a 404 error ● The web browser receives the file and renders it (nicely) fredag, 2009 september 18 Step-by-Step request ● Users types in a URL hits enter ● The web browser send the request to the server ● The web server receives the request and looks up the file – If the file exists send it to the browser – If not send a 404 error – If file has .php ending (often other may apply) start at the beginning of the file looks for the blocks that start with and ends with ?> fredag, 2009 september 18 PHP -simple example WEB server <?php print “<h1> Hello</ <h1>Hello</ h1”; ?> h1> <p> this is normal <p> this is normal html</p> html</p> <h2>World</ <?php h2> print “<h2> World</h2>”; ?> fredag, 2009 september 18 Types ● boolean ● integer ● float ● string ● array ● object ● resource ● NULL fredag, 2009 september 18 Basic Syntax ● Variables <?php $var = 'Anders'; $Var = 'Larsson'; echo "$var $Var"; // Anders Larsson ?> ● Can't start with numeric values ($4value = “crap” not valid) fredag, 2009 september 18 Variables as Variables <?php $x="lenny"; $lenny="Johan"; echo $$x; ?> fredag, 2009 september 18 Basic Syntax ● Constants (Not changing during execution) define("FOO", "something"); define("FOO2", "something else"); define("FOO_BAR", "something more") // Invalid constant names define("2FOO", "something"); fredag, 2009 september 18 Basic Syntax If <?php if (expression) statement ?> <?php if ($a > $b) echo "a is bigger than b"; ?> fredag, 2009 september 18 Basic Syntax If-Else <?php if ($a > $b) { echo "a is bigger than b"; } else { echo "a is NOT bigger than b"; } ?> fredag, 2009 september 18 Basic Syntax If-Elseif-Else <?php if ($a > $b) { echo "a is bigger than b"; } elseif ($a == $b) { echo "a is equal to b"; }else { echo "a is NOT bigger than b"; } ?> fredag, 2009 september 18 Basic Syntax While while (expr): statement ... endwhile; $i = 1; Equal $i = 1; while ($i <= 10): while ($i <= 10) { echo $i; echo $i++; $i++; } endwhile; fredag, 2009 september 18 Basic Syntax Do-While do{ statement ... }while(expression); $i = 0; do { echo $i; $i++; } while ($i < 10); // What will print? fredag, 2009 september 18 Basic Syntax For //Example 2 for (expr1; expr2; expr3) for ($i = 1; ; $i++) { if ($i > 10) { statement break; } //Example 1 echo $i; for ($i = 1; $i <= 10; $i++) { } echo $i; //Example 3 } $i = 1; for (; ; ) { if ($i > 10) { break; } echo $i; $i++; } fredag, 2009 september 18 PHP Arrays • A ordered Map • Values and Keys • Can be used as • array • list • hash table • dictionary • stack fredag, 2009 september 18 Arrays ● created with array(); array( [key =>] value , ... ) // key may be an integer or string // value may be any value $arr = array("foo" => "bar", 12 => true); echo $arr["foo"]; // bar echo $arr[12]; // 1 boolean not true/false but 1/0 fredag, 2009 september 18 Arrays cont. $arr = array(5 => 1, 12 => 2); $arr[] = 56; // This is the same as $arr[13] = 56; // at this point of the script $arr["x"] = 42; // This adds a new element to // the array with key "x" fredag, 2009 september 18 Arrays cont. $userinfo = array(); $userinfo['login'] = 'Gurra'; $userinfo['password'] = 'rattnalle'; $userinfo['hobbies']= array('sport','food','movie'); print $userinfo["hobbies"][1]; // what will print fredag, 2009 september 18 Basic Syntax Foreach foreach (array_expression as $value) statement foreach (array_expression as $key => $value) statement $arr = array('name'=>'Anders','language'=>'PHP'); foreach ($arr as $value) { print $value; } foreach($arr as $key =>$value) { print $key."=".$value; } fredag, 2009 september 18 Functions ● function namn(argument1,..argument*) { statements return $return_value; //if one } function add(value1 , value2){ return value1+value2; } fredag, 2009 september 18 Variable scope <?php <?php $a = 1; / $a = 1; /* global scope */ * global scope */ function test() function test() { { echo $a; global $a; } echo $a; test(); } ?> test(); ?> fredag, 2009 september 18 Variable scope <?php <?php $a = 1; / $a = 1; / * global scope */ * global scope */ function test() function test() { { global $a; echo GLOBAL[‘a’]; echo $a; } } test(); test(); ?> ?> fredag, 2009 september 18 Functions Arguments ● pass-by value ● pass-by reference ● pass-by default-value fredag, 2009 september 18 pass-by value vs. pass-by function inc(&$value) function inc($value) { { $value = $value+1; $value = $value+1; } } $number=8; $number=8; inc($number); inc($number); print $number; //????? print $number; //????? fredag, 2009 september 18 pass-by default-value Function makecoffee($type = "cappuccino") { return "Making a cup of $type.\n"; } echo makecoffee(); echo makecoffee("espresso"); fredag, 2009 september 18 Returning values ● using the return statement – return “some value”; ● Returning a reference function &returns_reference() { return $someref; } $newref =& returns_reference(); fredag, 2009 september 18 Classes and Objects ● Objects first occurred in PHP4 ● More advanced in PHP5 – New Object Model (total re-write) – Private and Protected Members/Methods – Abstract Classes and Methods – Interfaces – Object Cloning fredag, 2009 september 18 <?php Defining a Class class SimpleClass { // property declaration public $var = 'a default value'; //Constructor and Destructor public function __construct(){} public function __destruct(){} // method declaration public function displayVar() { echo $this‐>var; } } ?> fredag, 2009 september 18 Creating an instance <?php $instance = new SimpleClass(); // This can also be done with a variable: $className = 'Foo'; $instance = new $className(); // Foo() ?> fredag, 2009 september 18 Simple Class Inheritance • Single class inheritance • used with extends • parent:: fredag, 2009 september 18 Simple Class Inheritance <?php class ExtendClass extends SimpleClass { // Redefine the parent method function displayVar() { echo "Extending class\n"; parent::displayVar(); } } fredag, 2009 september 18 New features in 5.3 • Lambda functions • Closures fredag, 2009 september 18 fredag, 2009 september 18 • Lambda functions originate from lambda calculus which was introduced by Alonzo Church and Stephen Cole Kleene in the 1930s. fredag, 2009 september 18 • Lambda functions originate from lambda calculus which was introduced by Alonzo Church and Stephen Cole Kleene in the 1930s. • The lambda calculus can be thought of as an idealized, minimalistic programming language. It is capable of expressing any algorithm, and it is this fact that makes the model of functional programming an important one. fredag, 2009 september 18 • Lambda functions originate from lambda calculus which was introduced by Alonzo Church and Stephen Cole Kleene in the 1930s. • The lambda calculus can be thought of as an idealized, minimalistic programming language. It is capable of expressing any algorithm, and it is this fact that makes the model of functional programming an important one. • The lambda calculus provides the model for functional programming. Modern functional languages can be viewed as embellishments to the lambda calculus. fredag, 2009 september 18 fredag, 2009 september 18 • Implementing the lambda calculus on a computer involves treating "functions" as “first‐class objects”, which raises implementation issues for stack-based programming languages. fredag, 2009 september 18 • Implementing the lambda calculus on a computer involves treating "functions" as “first‐class objects”, which raises implementation issues for stack-based programming languages. • Many languages implement lambda functions. These include: fredag, 2009 september 18 • Implementing the lambda calculus on a computer involves treating "functions" as “first‐class objects”, which raises implementation issues for stack-based programming languages. • Many languages implement lambda functions. These include: • Python fredag, 2009 september 18 • Implementing the lambda calculus on a computer involves treating "functions" as “first‐class objects”, which raises implementation issues for stack-based programming languages. • Many languages

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    131 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us