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

  • WWF European Forest Programme - UNECE

    WWF European Forest Programme - UNECE

    WWF European Forest Programme From Casablanca...to Vladivostok... 85 Forest Officers in 31 Countries Protect, Manage, Restore Illegal Logging, Climate Change, Fire, Conversion
  • Christopher Chase-Dunn Alexis lvarez Institute for Research on

    Christopher Chase-Dunn Alexis lvarez Institute for Research on

    Christopher Chase-Dunn Alexis Álvarez Institute for Research on World-Systems University of California, Riverside Susan Manning Sociology Hofstra University
  • Health Impact Assessment in Chinese

    Health Impact Assessment in Chinese

    Garfield R, Santana S: The impact of the economic crisis and the US embargo on health in Cuba, American Journal of Public Health, Vol. 87, No. 1, p. 15-20 17. Mahoney M, Morgan RK: Health impact assessment in Australia and...
  • Cataloging Music Videos Workshop

    Cataloging Music Videos Workshop

    By Jay Weitz ([email protected]) Senior Consulting Database Specialist OCLC Online Computer Library Center For OLAC-MOUG Conference Cleveland, Ohio
  • Ontario College Application User Guide

    Ontario College Application User Guide

    Attend CollegeEvents. College Information . Program (CIP) This is your chance to meet with college reps and get answers to all of your questions about college.
  • 7Day SAT User Guide - User Registration

    7Day SAT User Guide - User Registration

    www.7daysat.nhs.uk September 2016 Survey Data Entry User Guide (v1 - 6th September 2016) Logging onto the 7 Day Self Assessment Tool Select your Trust from the drop down list Enter your password and Click 'Log In' If you are having...
  • CS5102 High Performance Computer Systems Warehouse-Scale ...

    CS5102 High Performance Computer Systems Warehouse-Scale ...

    Jai said in an interview that Google has been using the design since 2005 and now is in its sixth or seventh generation of design. ... (NCSA Blue Waters, UC Berkeley CRTF) Avoid UPS devices (lower Tier level) ... CS5102...
  • Graphs

    Graphs

    Graph terminology. Adjacent nodes: two nodes are adjacent if they are connected by an edge. Path: a sequence of vertices that connect two nodes in a graph. Complete graph: a graph in which every vertex is directly connected to every...