bmc3183 advance web programming programming session 2013/2014

67
BMC3183 Advance Web Programming Session 2013/2014

Upload: alyson-dawson

Post on 28-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Presentation Name

BMC3183Advance Web Programming Session 2013/20141 2OutlinesHistory Why phpPhp overviewInstall and configure a Web serverInstall and configure PHPInstall and configure MySQLCreate basic PHP scripts and PHP code blocksVariables and constantsData typesExpressions and operators

2HistoryStarted as a Perl hack in 1994 by Rasmus Lerdorf (to handle his resume), developed to PHP/FI 2.0By 1997 up to PHP 3.0 with a new parser engine by Zeev Suraski and Andi GutmansVersion 5.2.4 is current version, rewritten by Zend (www.zend.com) to include a number of features, such as an object modelCurrent is version 5php is one of the premier examples of what an open source project can beWhy php?..there are no. of server side scripting available like ASP, SSJS, JSP..PHP involvessimplicity in scripting (..generally using the database)platform independence.PHP is primarily designed for web applicationswell optimized for the response times needed for web applicationsIs an open source.

5PHP Overview PHP Hypertext Preprocessor.Other Names : Personal Home Page, Professional Home PagePHP is a server-side scripting language whose scripts are embedded in HTML documentsIs a server side scripting language.Capable of generating the HTML pagesHTML generates the web page with the static text and images.However the need evolved for dynamic web based application, mostly involving database usage.5 6Installing SoftwareInstall a Web serverhttp://httpd.apache.org/Install PHPhttp://www.php.net/downloads.phpInstall MySQLhttp://dev.mysql.com/downloads/

Portable WAMP serverhttp://www.uniformserver.com/NetBeans IDE: Quickly and easily develop desktop, mobile and web applicationswith Java, HTML5, PHP, C/C++ and more6Installation OptionsXAMPP: is a very easy to install package for (Linux-Windows- Apple). http://www.apachefriends.org/Apache, MySQL, PHP, Perl, and many more.WampServer will install Apache, PHP5 and MySQL on your Windows. http://www.wampserver.com/ZendFramework: - the most popular framework for modern, high performance php application. (http://www.zend.com/)Modular - ExtensibleHigh Performing - Secure

7ZendFrameworkthe most popular framework for modern, high performance php application. (http://www.zend.com/)Modular - ExtensibleHigh Performing - SecureZend provides Zend engine for PHP for freeThey provide other products and services for a feeServer side caching and other optimizationsEncoding in Zend's intermediate format to protect source codeIDE-a developer's package with tools to make life easierZend's web site is a great resourcePHP 5 ArchitectureZend engine as parser (Andi Gutmans and Zeev Suraski)SAPI is a web server abstraction layerPHP components now self contained (ODBC, Java, LDAP, etc.)This structure is a good general design for software (compare to OSI model, and middleware applications)

image from http://www.zend.com/zend/art/intro.php 10Understanding Binary and Source Code InstallationsBinary format (or binaries) refer to compiled files, such as executable installation programsSource code is the original programming code in which an application was writtenSource code must be compiled, or processed, and assembled into an executable format before it is usedCompiled programs only need to be recompiled when their code changes 11Installing and Configuring a Web ServerApache is the most popular Web server software used on the InternetMicrosoft IIS for Windows is the second most popular server softwareIn Windows, a service refers to a program that performs a specific function to support other programs

12Testing Your Web ServerOpen your Web browserType http://localhost/ in the Address box, click Enter

Apaches default Web page

13Testing Your Web Server (continued)Type http://127.0.0.1/ in the Address box, click Enter

Web page informing you that IIS is running

14Configuring ApacheTo configure ports and other settings you must edit the httpd.conf fileFor UNIX/Linux/usr/local/apache2/confFor WindowsC:\Program Files\Apache Group\Apache2\confLines that begin with the pound sign (#) are informational commentsLines without pound signs contain directives 15Configuring Apache (continued) httpd.conf

16Configuring Apache (continued)Directives define information about how a program should be configuredThe DocumentRoot directive identifies the default directory from where Apache serves Web pagesThe Alias directive identifies other directories that Apache can use to serve Web pages

17Configuring Internet Information Services Default Web Site Properties dialog box

18Configuring Apache for PHP on UNIX/Linux PlatformsOpen the httpd.conf file from the /usr/local/apache2/conf directorySearch for the LoadModule directive:LoadModule php5_module libexec/libphp5.soAdd the AddType directive to the end of the file:AddType application/x-httpd-php .phpSave and close the httpd.conf fileRestart Apache with the command:/usr/local/apache2/bin/apachectl restart 19Configuring Apache for PHP on WindowsClick the Start menu and point to All ProgramsSelect the Edit the Apache httpd.config Configuration File commandAdd the following to the end of the file:ScriptAlias /PHP/ C:/PHP/AddType application/x-httpd-php .phpAction application/x-httpd-php /PHP/php-cgi.exeSave and close the httpd.conf fileRestart Apache and select the Restart command

20Configuring PHP The php.ini configuration file

21Testing the MySQL ServerCheck to see if MySQL is runningFor UNIX/Linux systems: /usr/local/mysql/bin/mysqld_safe --user=mysql &For Windows, use the Services windowRun the mysqladmin version commandFor UNIX/Linux systems: /usr/local/mysql/bin/mysqladmin versionFor Windows, change to the C:\Program Files\MySQL\MySQL Server 4.1\bin\ directory and run: mysqladmin version 22Configuring the Uniform Serverphp.ini is located in:\UniServer\usr\local\php

httpd.conf is located in:\UniServer\usr\local\apache2\conf

my is located in:\UniServer\usr\local\mysql

23Creating Basic PHP ScriptsEmbedded language refers to code that is embedded within a Web page (XHTML document)PHP code is typed directly into a Web page as a separate sectionA Web page document containing PHP code must have an extension of .phpPHP code is never sent to a clients Web browser

24Creating Basic PHP Scripts (continued)The Web page generated from the PHP code, and HTML or XHTML elements found within the PHP file, is returned to the clientA PHP file that does not contain any PHP code should have an .html extension.php is the default extension that most Web servers use to process PHP scripts

PHP BlockThere are four different ways to embed the PHP code

echo(Some PHP code);

26Creating PHP Code BlocksCode declaration blocks are separate sections within a Web page that are interpreted by the scripting enginePHP code block is embedded within the tags.When the server encounters the PHP tags it switches from the HTML to PHP mode.There are four types of code declaration blocks:Standard PHP script delimitersThe element Short PHP script delimitersASP-style script delimiters

27Standard PHP Script DelimitersA delimiter is a character or sequence of characters used to mark the beginning and end of a code segmentThe standard method of writing PHP code declaration blocks is to use the script delimitersThe individual lines of code that make up a PHP script are called statements

28The ElementThe element identifies a script section in a Web page documentAssign a value of "php" to the language attribute of the element to identify the code block as PHP

29Short PHP Script DelimitersThe syntax for the short PHP script delimiters is

Short delimiters can be disabled in a Web servers php.ini configuration filePHP scripts will not work if your Web site ISP does not support short PHP script delimitersShort delimiters can be used in XHTML documents, but not in XML documents

30ASP-Style Script DelimitersThe syntax for the ASP-style script delimiters isASP-style script delimiters can be used in XHTML documents, but not in XML documentsASP-style script delimiters can be enabled or disabled in the php.ini configuration fileTo enable or disable ASP-style script delimiters, assign a value of On or Off to the asp_tags directive in the php.ini configuration file 31Understanding FunctionsA function is a subroutine (or individual statements grouped into a logical unit) that performs a specific taskTo execute a function, you must invoke, or call, it from somewhere in the scriptA function call is the function name followed by any data that the function needsThe data (in parentheses following the function name) are called arguments or actual parametersSending data to a called function is called passing arguments

32Displaying Script ResultsTo return to the client the results of any processing that occurs within a PHP code block, you must use an echo() statement or the print() statementThe echo() and print() statements create new text on a Web page that is returned as a response to a client

33Displaying Script Results (continued) PHP Diagnostic Information Web page

34Displaying Script Results (continued)The echo() and print() statements are language constructs of the PHP programming languageA programming language construct refers to a built-in feature of a programming languageThe echo() and print() statements are virtually identical except: The print() statement returns a value of 1 if it is successful It returns a value of 0 if it is not successful 35Displaying Script Results (continued)Use the echo() and print() statements to return the results of a PHP script within a Web page that is returned to a clientA text string, or literal string, is text that is contained within double or single quotation marksTo pass multiple arguments to the echo() and print() statements, separate them with commas like arguments passed to a function

36Creating Multiple Code Declaration BlocksFor multiple script sections in a document, include a separate code declaration block for each section ...

Multiple Script SectionsFirst Script Section

Second Script Section

37Creating Multiple Code Declaration Blocks (continued)PHP code declaration blocks execute on a Web server before a Web page is sent to a client...

Multiple Script SectionsFirst Script SectionOutput from the first script section.Second Script SectionOutput from the second script section.

37 38Creating Multiple Code Declaration Blocks (continued) Figure 1-9 Output of a document with two PHP script sections

39Case Sensitivity in PHPProgramming language constructs in PHP are mostly case insensitive

40Adding Comments to a PHP ScriptComments are nonprinting lines placed in code such as: The name of the scriptYour name and the date you created the programNotes to yourselfInstructions to future programmers who might need to modify your work

41Adding Comments to a PHP Script (continued)Line comments hide a single line of codeAdd // or # before the textBlock comments hide multiple lines of codeAdd /* to the first line of codeAnd */ after the last character in the code

42Adding Comments to a PHP Script (continued)

43Using Variables and ConstantsThe values stored in computer memory are called variablesThe name you assign to a variable is called an identifier and it:Must begin with a dollar sign ($)Cannot begin with an underscore (_) or a numberCannot include spacesIs case sensitive 44Declaring and Initializing VariablesSpecifying and creating a variable name is called declaring the variableAssigning a first value to a variable is called initializing the variableIn PHP, you must declare and initialize a variable in the same statement:$variable_name = value;

45Displaying VariablesTo print a variable with the echo() statement, pass the variable name to the echo() statement without enclosing it in quotation marks:$VotingAge = 18;Echo $VotingAge;To print both text strings and variables, send them to the echo() statement as individual arguments, separated by commas:echo "The legal voting age is ", $VotingAge, ".";

46Displaying VariablesTo print text strings and variables, you can send them to the echo() statement as one argument enclosed in double quotes:echo "The legal voting age is $VotingAge.";The legal voting age is 18.

To print text strings and the variable name, you can send them to the echo() statement as one argument enclosed in single quotes:echo The legal voting age is $VotingAge.;The legal voting age is $VotingAge 47Modifying VariablesYou can modify a variables value at any point in a script$SalesTotal = 40;echo "Your sales total is $$SalesTotal";$SalesTotal = 50;echo "Your new sales total is $$SalesTotal";

48Defining ConstantsA constant contains information that does not change during the course of program executionConstant names do not begin with a dollar signConstant names use all uppercase letters Use the define() function to create a constantdefine("CONSTANT_NAME", value);define("VOTING_AGE",18);define("VOTING_AGE",18,TRUE);The value you pass to the define() function can be a text string, number, or Boolean value

PHP Constants..values that never changesConstants are defined in PHP by using the define() function.For e.g.define(NCST, National Centre for Software Technology)defined() function says whether the constant exists or not.

50Working with Data TypesA data type is the specific category of information that a variable containsData types that can be assigned only a single value are called primitive types

PrimitivesFour scalar types: Boolean, integer, double, and stringTwo compound types: array and objectTwo special types: resource and NULLInteger & double are like those of other languagesBoolean -values are true and false (case insensitive)

PHP Data TypeThree basic data typesIntegerDoubleStringMore data typesArrayObjectPHP is an untyped language variables type can change on the fly.

PHP VariablesThe variables in PHP are declared by appending the $ sign to the variable name.For e.g$company = NCST;$sum = 10.0;variables data type is changed by the value that is assigned to the variable.Type casting allows to change the data type explicitly.Rich set of functions for working with variable.For e.ggettype, settype, isset, unset, is_int, intval etc etc

54Working with Data Types (continued)The PHP language supports:A resource data type a special variable that holds a reference to an external resource such as a database or XML fileReference or composite data types, which contain multiple values or complex types of informationTwo reference data types: arrays and objects

Basic arrays are covered later in this chapter54 55Working with Data Types (continued)Strongly typed programming languages require you to declare the data types of variables Static or strong typing refers to data types that do not change after they have been declaredLoosely typed programming languages do not require you to declare the data types of variablesDynamic or loose typing refers to data types that can change after they have been declaredPHP is a loosely typed programming language you actually cannot declare a data type of a variable, but you can force a variable to be converted to a specific type (shown later).

55 56Numeric Data TypesPHP supports two numeric data types:An integer is a positive or negative number with no decimal places (-250, 2, 100, 10,000)A floating-point number is a number that contains decimal places or that is written in exponential notation (-6.16, 3.17, 2.7541)Exponential notation, or scientific notation, is short for writing very large numbers or numbers with many decimal places (2.0e11) 57Boolean ValuesA Boolean value is a value of true or falseIt decides which part of a program should execute and which part should compare dataIn PHP programming, you can only use true or falseIn other programming languages, you can use integers such as 1 = true, 0 = false

58Dynamic Typing$Variable = "Hello World";$Variable = 8;$Variable = 5.367;$Variable = TRUE;$Variable = NULL;StringInteger NumberFloating PointBooleanNULL58PHP OperatorsAll the operators such as arithmetic, assignment, Comparison, and logical operators are similar to the operators in C and C++.In PHP the string concatenation operator is denoted by ..For e.g.$name = My name is.$myname;

Syntactic Characteristics PHP code can be specified in an HTML document internally or externally:Internally: Externally: include ("myScript.inc")the file can have both PHP and HTML If the file has PHP, the PHP must be in