PHP - anbo-easj.dk

PHP - anbo-easj.dk

PHP A very brief introduction PHP 1 PHP PHP competitors PHP = PHP: Hypertext Processor A recursive acronym! PHP scripts are executed on the server side Executed by (a plugin to) an application

server An HTTP server + more Microsoft ASP.NET Oracle JavaServer Faces (JSF) Python Ruby on Rails many others We will use the Apache HTTP server PHP can help developers make dynamic web pages As opposed to static web pages PHP is open source PHP 2 Application stacks Application stack

Application server + Database + programming language AMP stack AMP = Apache, MariaDB/MySQL, PHP LAMP = Linux + AMP WAMP = Windows + AMP XAMP = any OS + Amp We will use XAMP MEAN stack MongoDB, Express JS, Angular JS, Node JS Microsoft IIS, Microsoft SQL Server, ASP.NET PHP 3

PHP basic syntax PHP is (small) pieces of code written inside ordinary HTML . The PHP code is executed by the application server. The resulting (echo) HTML etc. is sent to the client. The PHP code is not sent to the client Browsers cannot execute PHP PHP comments // The rest of the line is a comment /* multi-line comment . */ Example: PhpFirstExample PHP 4 PHP variables PHP variables start with $

$txt = "Hello world!"; $x = 5; $y = 10.5; ?> A variable can have a short name (like x and y) or a more descriptive name (age, carName, total_volume). Rules for PHP variables: A variable starts with the $ sign, followed by the name of the variable A variable name must start with a letter or the underscore character A variable name cannot start with a number A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) Variable names are case-sensitive ($age and $AGE are two different variables) Source http://www.w3schools.com/php/php_variables.asp PHP

5 PHP data types Variables can store data of different types, and different data types can do different things. PHP supports the following data types: String content of the string Integer Float (floating point numbers - also called double) Boolean: true, false Array

Object NULL Resource Arrays PHP 6 Type systems Static type checking Strongly typed language Each variable, identifier, etc. has a type Variable etc. do not change type at runtime Explicit type conversion C#, Java Types are checked at compile-time Bugs are caught early C#, Java Dynamic type checking

Types are checked at run-time Bugs are caught late Weakly typed language Variables, etc. may have a type Variables, etc. can change type at run-time Type conversion generally not needed PHP, JavaScript Example: phpTypes Help from IDEs IDES like Visual Studio, PhpStorm, etc. provide more help for programmers of a strongly + statically typed language. PHP

7 PHP forms A form is a place for the user to submit some data to the system. Usually 2 pages are involved The page with the input form Another page to handle the input Example: firstform PHP 8 PHP super globals PHP offers a number of so-called super globals. They are defined automatically Examples $_POST: Access to data from forms sent with the POST HTTP method $name = $_POST[name]; $_GET: Access to data from forms sent with the GET HTTP method

$_REQUEST: Access to data from forms sent with GET or POST http://www.w3schools.com/php/php_superglobals.asp PHP 9 PHP conditions If statements Similar to C#, with an extra elseif If If else If elseif Example: PhpTypes http://www.w3schools.com/php/php_if_else.asp

Switch statement Similar to C# http://www.w3schools.com/php/php_switch.asp PHP 10 PHP loops While and do while Similar to C# While has test at the entry Do while has test at the exit At least one turn in the loop http://www.w3schools.com/php/php_looping.asp For and foreach Similar to C# Foreach element in an arrays http://www.w3schools.com/php/php_looping.asp PHP

11 PHP functions Comparable to C# methods Functions can return a value Many API functions return False if there is no proper result In C# the method would throw an exception Something of a different type if there is a proper result Functions can have parameters Functions can have local variables Only visible inside the function (like C# methods) Example: phpTypes PHP 12 PHP classes

PHP classes are similar to C# classes Private + public fields Private + public functions Constructors The syntax for referring to member (field or function) is slightly different C# obj.method(param) PHP obj -> function(param) Example: ClassExample PHP 13 PHP arrays Stores multiple values in a single variable Basic syntax (http://www.w3schools.com/php/php_arrays.asp)

Iteration foreach ($array as $value) { code to be executed; } Three types of arrays Indexed arrays: Indexes 0, 1, 2, Associative arrays: (key, value)-pairs Alias Map alias Dictionary Multi-dimensional arrays: arrays containing arrays PHP 14 PHP exception handling Syntax

Exception classes Try { . } Catch (Exception ex) Class MyException extends Exception { } Throw new Exception(.); Similar to C# PHP 15 PHP consuming SOAP/WSDL web services The tool wsdl2php reads WSDL file and produces PHP classes according to the file. Import the generated PHP file into your PHP file Require_once $filename.

Use the generated classes Example PhpCurrencySoapConsumer PHP 16 REST, an alternative to SOAP SOAP uses HTTP POST for requests REST uses the all the HTTP methods/verbs GET Read a resource POST Store a resource PUT Update an existing resource DELETE

Delete a resource Example: http://anbobookstorerest.cloudapp.net/Service1.svc/books/ Consume service using browser, Postman or an application PHP 17 PHP consuming REST web services Two stages 1. Make a GET request and get the response body (XML or JSON) 2. Read the response body and convert it into a more useful format GET request can be send in two ways With curl $client = curl_init($uri); curl_setopt($client, CURLOPT_RETURNTRANSFER, true); // default: false, curl_exec returns true/false $response = curl_exec($client); curl_close($client);

With file_get_contents $response = file_get_contents($uri); Make a useful format: associative arrays $convertToAssociativeArray = true; $arr= json_decode($jsondata, $convertToAssociativeArray); echo $arr['Title']; Example BookstoreRest PHP 18

Recently Viewed Presentations

  • Unit 1: 1. crust 2. pass 3. else

    Unit 1: 1. crust 2. pass 3. else

    tablecloth. everyone. nearby. outdoors. high school. anywhere. flashlight. air mail. whenever. driveway. sometimes. seat belt. basketball. mailbox. baby sitter ...
  • TecEco TecEco Tec-Cement Tec-Cement Concretes Concretes Abatement, Abatement,

    TecEco TecEco Tec-Cement Tec-Cement Concretes Concretes Abatement, Abatement,

    TecEco Tec-Cement Concretes - Abatement, Strength, Durability and Waste Utilization in the Built Environment If we can make materials that take less than half as much energy, last more than twice as long (are more durable) and have a use...
  • The Finite Difference Method This section presents a

    The Finite Difference Method This section presents a

    It is important to note that the majority of the equations found in the financial literature including exotic options and more sophisticated derivatives, are of parabolic type in one dimention (Black-Scholes equation - one state variable plus time).
  • Playing with idioms 2 - Università degli Studi di Enna Kore

    Playing with idioms 2 - Università degli Studi di Enna Kore

    Playing with idioms 2 ... Sottobanco On the Q. T. 2 ORIGIN: The slang term 'qt' is a shortened form of 'quiet'. There's no definitive source for the phrase 'on the q.t.' ... To stick/stuck To be the icing on...
  • 15 - Texas Tech University

    15 - Texas Tech University

    Consumers' Use of Reference Prices. Internal reference price. External reference price. Group activity: In groups, list the average prices of the following items: rent on a one-bedroom apartment, a backpack, a washing machine, a mid-size car, a house, a bicycle,...
  • Northwest Climate Variability

    Northwest Climate Variability

    Note the appearance of dry eras in the 20s-mid40s and 1980s-1995. Alan will show updated versions of this graph that highlight some fo the recent drought periods in 2001, 2003, and 2005. Why does NW climate vary? There are a...
  • ESCHER X NENDO ARTS PACKAGE PRICING WEEKDAYS Escher

    ESCHER X NENDO ARTS PACKAGE PRICING WEEKDAYS Escher

    FRIDAY. Morning tea Scones plain, fruit, served with homemade preserve and cream Lunch SANDWICHES. Sugarcured Ham, brie cheese, pickle, butter lettuce on focaccia.
  • Intuíció és szerkesztettség, avagy hogyan lesz egyformán ...

    Intuíció és szerkesztettség, avagy hogyan lesz egyformán ...

    Ungváry Rudolf (OSZK): Paradigmatikus relációk tipológiája avagy Ontológia és tezaurusz egyesítése 2008. április 25. Egy jellegzetes fogalmi hierarchia ( DOLCE) thing endurant physical endurant EZ NEM ONTOLÓGIA, HANEM (ELVILEG) amount of matter FOGALMAK GENERIKUS [is_a, faj-nem] RELÁCIÓN feature ALAPULÓ HIERARCHIÁJA!