PHP A very brief introduction PHP 1 PHP
PHP A very brief introduction PHP 1
PHP • PHP = PHP: Hypertext Processor • PHP competitors • A recursive acronym! • • • PHP scripts are executed on the server side • Executed by (a plugin to) an application server Microsoft ASP. NET Oracle Java. Server Faces (JSF) Python Ruby on Rails … many others • An HTTP server + more • 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, Maria. DB/My. SQL, PHP LAMP = Linux + AMP WAMP = Windows + AMP XAMP = any OS + Amp • We will use XAMP • MEAN stack • Mongo. DB, 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 …. <body> <? php echo date("r") ? > …</body> … <? php PHP code ? > 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: Php. First. Example PHP 4
PHP variables • PHP variables start with $ • <? php $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, car. Name, 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, 09, and _ ) • Variable names are case-sensitive ($age and $AGE are two different variables) • Source http: //www. w 3 schools. com/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 • Strongly typed language • Static type checking • Each variable, identifier, etc. has a type • Variable etc. do not change type at run-time • 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 • Help from IDEs • Variables, etc. may have a type • Variables, etc. can change type at run -time • Type conversion generally not needed • PHP, Java. Script • Example: php. Types • IDES like Visual Studio, Php. Storm, 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. w 3 schools. com/php_superglobals. asp PHP 9
PHP conditions • If statements • • • Similar to C#, with an extra elseif If If … elseif Example: Php. Types http: //www. w 3 schools. com/php_if_else. asp • Switch statement • Similar to C# • http: //www. w 3 schools. com/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. w 3 schools. com/php_looping. asp • For and foreach • Similar to C# • Foreach element in an arrays • http: //www. w 3 schools. com/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: php. Types 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: Class. Example PHP 13
PHP arrays • Stores multiple values in a single variable • Basic syntax (http: //www. w 3 schools. com/php_arrays. asp) • <? php $cars = array("Volvo", "BMW", "Toyota"); echo "I like ". $cars[0]. ", ". $cars[1]. " and ". $cars[2]. ". "; ? > • 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 My. Exception extends Exception { … } • Throw new Exception(“…. ”); • Similar to C# PHP 15
PHP consuming SOAP/WSDL web services • The tool wsdl 2 php 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 • Php. Currency. Soap. Consumer 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/Service 1. svc/books/ • Consume service using browser, Postman or an application PHP 17
PHP consuming REST web services • Two stages 1. 2. Make a GET request and get the response body (XML or JSON) 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 • $convert. To. Associative. Array = true; • $arr= json_decode($jsondata, $convert. To. Associative. Array); • echo $arr['Title']; • Example • Bookstore. Rest PHP 18
- Slides: 18