CN5109 WEB APPLICATION DEVELOPMENT Chapter 1 Introduction to Server-Side Development with PHP
What is Server-Side Development? • The basic hosting of your files is achieved through a web server. • Server-side development is much more than web hosting: it involves the use of a programming technology like PHP or ASP to create scripts that dynamically generate content. • Server-side scripting is a method of designing websites so that the process or user request is run on the originating server. • Server-side scripts provide an interface to the user and are used to limit access to proprietary data and help keep control of the script source code.
What is Server-Side Development?
Server-Side Scripting Examples • ActiveVFP • ASP • C • DC • Java • JavaScript (using Server-side JavaScript (SSJS) e.g., node.js) • Perl • PHP • Python • R • Ruby
Client-side vs. Server-side Scripting Client-side Environment • The client-side environment used to run scripts is usually a browser. • The processing takes place on the end users computer. • The source code is transferred from the web server to the users computer over the internet and run directly in the browser. • The scripting language needs to be enabled on the client computer. • Sometimes if a user is conscious of security risks they may switch the scripting facility off. • When this is the case a message usually pops up to alert the user when script is attempting to run.
Client-side vs. Server-side Scripting Server-side Environment • The server-side environment that runs a scripting language is a web server. • A user's request is fulfilled by running a script directly on the web server to generate dynamic HTML pages. • This HTML is then sent to the client browser. • It is usually used to provide interactive web sites that interface to databases or other data stores on the server. • This is different from client-side scripting where scripts are run by the viewing web browser, usually in JavaScript. • The primary advantage to server-side scripting is the ability to highly customize the response based on the user's requirements, access rights, or queries into data stores.
Client-side vs. Server-side Scripting 1. Client page request 2. Decision making based on requested page code content 3. HTML output returned to browser
Introduction to PHP • Created by Rasmus Lerdorf in 1994 and publicly released June 8, 1995 • PHP, which is short for PHP: Hypertext Preprocessor, is a server-side interpreted scripting language. • It was designed for creating dynamic web pages and web pages that effectively work with databases. • Files that include PHP code on a web server may have any file extension, but most commonly they end in .PHP, .PHP3, or .PHTML. • A PHP file can be created and the contents can be viewed by using a programming code editing program, such as Dreamweaver or Notepad++.
Common Use of PHP • PHP can generate dynamic page content • PHP can create, open, read, write, delete, and close files on the server • PHP can collect form data • PHP can send and receive cookies • PHP can add, delete, modify data in your database • PHP can be used to control user-access • PHP can encrypt data
Advantages of PHP • PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.) • PHP is compatible with almost all servers used today (Apache, IIS, etc.) • PHP supports a wide range of databases • PHP is free. • PHP is easy to learn and runs efficiently on the server side
PHP - Environment Setup • In order to develop and run PHP Web pages three vital components need to be installed on your computer system. • Web Server − PHP will work with virtually all Web Server software, including Microsoft's Internet Information Server (IIS) but then most often used is freely available Apache Server. • Database − PHP will work with virtually all database software, including Oracle and Sybase but most commonly used is freely available MySQL database. • PHP Parser − In order to process PHP script instructions a parser must be installed to generate HTML output that can be sent to the Web Browser.
Quiz 1. Define Server Scripting Language 2. Give any four examples of Server Scripting Language 3. Discuss any four common use of PHP. 4. Discuss any four advantages of PHP.
PHP Installation • To start using PHP, you can: • Find a web host with PHP and MySQL support • Install a web server on your own PC, and then install PHP and MySQL • If your server has activated support for PHP you do not need to do anything. • Just create some .php files, place them in your web directory, and the server will automatically parse them for you. • You do not need to compile anything or install any extra tools. • Because PHP is free, most web hosts offer PHP support.
PHP Installation • However, if your server does not support PHP, you must: • install a web server (XAMPP, WAMP) • install PHP • install a database, such as MySQL
Write PHP Online • Write PHP Online is an online code editor which helps you to write and test run/execute your php code online from your browser. • This is one of the simple and powerfull online php code editor tool available on the internet. • Write PHP Online supports all PHP functionalities and it runs using PHP version 7. http://www.writephponline.com/
Write PHP Online 1. Write the PHP Code. 2. Click the “Run Code” button to see the output.
PHP Syntax • A PHP script is executed on the server, and the plain HTML result is sent back to the browser. • A PHP script can be placed anywhere in the document. • A PHP script starts with <?php and ends with ?>. • PHP statements are terminated by a semicolon (;) • The default file extension for PHP files is ".php". • A PHP file normally contains HTML tags, and some PHP scripting code.
PHP Syntax • Example <?php echo "Hello World!"; ?> Output:
PHP in HTML • PHP is designed to interact with HTML and PHP scripts can be included in an HTML page without a problem. • In an HTML page, PHP code is enclosed within special PHP tags. <html> <body> <?php echo "Hello World!"; ?> </body> </html>
HTML in PHP • To write HTML code in PHP file, you need to use Echo or Print statement. <?php echo “Hello World”; echo “<h1> Hello World</h1>”; ?> Output:
Echo/ Print Statement • Used to output data to the screen. • The echo statement can be used with or without parentheses: echo or echo(). <?php echo "Hello world!"; echo 123; ?> Output:
Echo / Print Statement • Using HTML tags in PHP: <?php echo “Hello”; echo “<br>”; echo “World”; ?> <?php echo “Hello <br> World”; ?>
Try This <?php echo "<table border=1 width=500>"; echo "<tr>"; echo "<td>1</td><td>2</td>"; echo "</tr>"; echo "<tr>"; echo "<td>3</td><td>4</td>"; echo "</tr>"; echo "</table>"; ?> Output:
Quiz Write a PHP code to display the following outputs: a. Welcome to FTMS College b. University of East London c. CN5109 Web Application Development
Variables • Variables are "containers" for storing information. • In PHP, a variable starts with the $ sign, followed by the name of the variable Note: When you assign a text value to a variable, put quotes around the value. Note: Unlike other programming languages, PHP has no command for declaring a variable. It is created the moment you first assign a value to it. $txt = "Hello world!"; $x = 5; $y = 10.5;
Variables 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)
Quiz Determine whether these variable names are valid or not valid. a. text b. $txt1 c. $1txt d. $txt 1 e. $txt#1 f. $txt_1
Comments in PHP • A comment in PHP code is a line that is not read/executed as part of the program. Its only purpose is to be read by someone who is looking at the code. • Comments can be used to: • Let others understand what you are doing • Remind yourself of what you did - Most programmers have experienced coming back to their own work a year or two later and having to re-figure out what they did. Comments can remind you of what you were thinking when you wrote the code
Comments in PHP <?php // This is a single-line comment # This is also a single-line comment /* This is a multiple-lines comment block that spans over multiple lines */ echo “This is not a comment”; ?>
Data Types • Variables can store data of different types, and different data types can do different things. • Data types are declarations for variables. This determines the type and size of data associated with variables. • PHP supports the following data types: • String • Integer • Float (floating point numbers - also called double) • Boolean • Array • NULL
Data Types - String • A string is a sequence of characters, like "Hello world!". • A string can be any text inside quotes. <?php $x = "Hello world!"; $y = “Hi There!”; echo $x; echo "<br>"; echo $y; ?>
Data Types - Integer • An integer data type is a non-decimal number between - 2,147,483,648 and 2,147,483,647. • Rules for integers: • An integer must have at least one digit • An integer must not have a decimal point • An integer can be either positive or negative • Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0) <?php $x = 5985; echo $x; ?>
Data Types - Float • A float (floating point number) is a number with a decimal point or a number in exponential form. • In the following example $x is a float. • The PHP var_dump() function returns the data type and value. <?php $x = 10.365; echo $x; ?>
Data Types - Boolean • A Boolean represents two possible states: TRUE or FALSE. • Booleans are often used in conditional testing. $x = true; $y = false; echo $x;
Data Types - Array • An array stores multiple values in one single variable. • In the following example $cars is an array. <?php $cars = array("Volvo","BMW","Toyota","Honda"); echo $cars[0],"<br>"; echo $cars[1],"<br>"; echo $cars[2],"<br>"; echo $cars[3],"<br>"; ?>
Data Types - Null • Null is a special data type which can have only one value: NULL. • A variable of data type NULL is a variable that has no value assigned to it. • Tip: If a variable is created without a value, it is automatically assigned a value of NULL. • Variables can also be emptied by setting the value to NULL <?php $x = null; echo $x; ?>
Quiz Identify the data type of the following data: a. “Cyberjaya” b. 1.35 c. 50 d. True e. A f. “False” g. h. 1, 2, 4, 8 i. “25”
Operator • Operators are used to perform mathematical operations on variables and values. • PHP divides the operators in the following groups: • Arithmetic operators • Assignment operators • Comparison operators • Increment/Decrement operators • Logical operators • String operators • Array operators
Arithmetic Operators • Arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc. Operation Name Example Result + Addition $x + $y Sum of $x and $y - Subtraction $x - $y Difference of $x and $y * Multiplication $x * $y Product of $x and $y / Division $x / $y Quotient of $x and $y % Modulus $x % $y Remainder of $x divided by $y
Arithmetic Operators • Example $x = 10; $y = 5; echo $x + $y,"<br>"; echo $x - $y,"<br>"; echo $x * $y,"<br>"; echo $x / $y,"<br>"; echo $x % $y,"<br>"; Output:
Comparison Operators • The PHP comparison operators are used to compare two values (number or string). Operator Name Example Result == Equal $x == $y Returns true if $x is equal to $y != Not equal $x != $y Returns true if $x is not equal to $y <> Not equal $x <> $y Returns true if $x is not equal to $y > Greater than $x > $y Returns true if $x is greater than $y < Less than $x < $y Returns true if $x is less than $y >= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y <= Less than or equal to $x <= $y Returns true if $x is less than or equal to $y
Comparison Operators • Example $x = 10; $y = 5; echo $x == $y,"<br>"; echo $x <> $y,"<br>"; echo $x < $y,"<br>"; echo $x > $y,"<br>"; Output:
Increment /Decrement Operators • Increment operators are used to increment a variable's value. • Decrement operators are used to decrement a variable's value. Operator Name Description ++$x Pre-increment Increments $x by one, then returns $x $x++ Post-increment Returns $x, then increments $x by one --$x Pre-decrement Decrements $x by one, then returns $x $x-- Post-decrement Returns $x, then decrements $x by one
Increment /Decrement Operators • Example $x = 10; echo $x ,"<br>"; echo ++$x ,"<br>"; echo $x ,"<br>"; echo $x++ ,"<br>"; Output:
Logical Operators • Example Operator Name Example Result and And $x and $y True if both $x and $y are true or Or $x or $y True if either $x or $y is true xor Xor $x xor $y True if either $x or $y is true, but not both && And $x && $y True if both $x and $y are true || Or $x || $y True if either $x or $y is true ! Not !$x True if $x is not true
Logical Operators • Example $x = 100; $y = 50; if ($x == 100 and $y == 50) { echo "Hello world!"; } Output:
Quiz Solve the following equations: Given $x = 10 and $y = 5 a. $x + $x + $y b. $x - $y + $x c. $x + $y * $x d. $x / $y + $x e. $x > $y f. $y != $x g. ++$x h. $y--

Web Application Development using PHP Chapter 1

  • 1.
  • 2.
    What is Server-SideDevelopment? • The basic hosting of your files is achieved through a web server. • Server-side development is much more than web hosting: it involves the use of a programming technology like PHP or ASP to create scripts that dynamically generate content. • Server-side scripting is a method of designing websites so that the process or user request is run on the originating server. • Server-side scripts provide an interface to the user and are used to limit access to proprietary data and help keep control of the script source code.
  • 3.
    What is Server-SideDevelopment?
  • 4.
    Server-Side Scripting Examples •ActiveVFP • ASP • C • DC • Java • JavaScript (using Server-side JavaScript (SSJS) e.g., node.js) • Perl • PHP • Python • R • Ruby
  • 5.
    Client-side vs. Server-sideScripting Client-side Environment • The client-side environment used to run scripts is usually a browser. • The processing takes place on the end users computer. • The source code is transferred from the web server to the users computer over the internet and run directly in the browser. • The scripting language needs to be enabled on the client computer. • Sometimes if a user is conscious of security risks they may switch the scripting facility off. • When this is the case a message usually pops up to alert the user when script is attempting to run.
  • 6.
    Client-side vs. Server-sideScripting Server-side Environment • The server-side environment that runs a scripting language is a web server. • A user's request is fulfilled by running a script directly on the web server to generate dynamic HTML pages. • This HTML is then sent to the client browser. • It is usually used to provide interactive web sites that interface to databases or other data stores on the server. • This is different from client-side scripting where scripts are run by the viewing web browser, usually in JavaScript. • The primary advantage to server-side scripting is the ability to highly customize the response based on the user's requirements, access rights, or queries into data stores.
  • 7.
    Client-side vs. Server-sideScripting 1. Client page request 2. Decision making based on requested page code content 3. HTML output returned to browser
  • 8.
    Introduction to PHP •Created by Rasmus Lerdorf in 1994 and publicly released June 8, 1995 • PHP, which is short for PHP: Hypertext Preprocessor, is a server-side interpreted scripting language. • It was designed for creating dynamic web pages and web pages that effectively work with databases. • Files that include PHP code on a web server may have any file extension, but most commonly they end in .PHP, .PHP3, or .PHTML. • A PHP file can be created and the contents can be viewed by using a programming code editing program, such as Dreamweaver or Notepad++.
  • 9.
    Common Use ofPHP • PHP can generate dynamic page content • PHP can create, open, read, write, delete, and close files on the server • PHP can collect form data • PHP can send and receive cookies • PHP can add, delete, modify data in your database • PHP can be used to control user-access • PHP can encrypt data
  • 10.
    Advantages of PHP •PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.) • PHP is compatible with almost all servers used today (Apache, IIS, etc.) • PHP supports a wide range of databases • PHP is free. • PHP is easy to learn and runs efficiently on the server side
  • 11.
    PHP - EnvironmentSetup • In order to develop and run PHP Web pages three vital components need to be installed on your computer system. • Web Server − PHP will work with virtually all Web Server software, including Microsoft's Internet Information Server (IIS) but then most often used is freely available Apache Server. • Database − PHP will work with virtually all database software, including Oracle and Sybase but most commonly used is freely available MySQL database. • PHP Parser − In order to process PHP script instructions a parser must be installed to generate HTML output that can be sent to the Web Browser.
  • 12.
    Quiz 1. Define ServerScripting Language 2. Give any four examples of Server Scripting Language 3. Discuss any four common use of PHP. 4. Discuss any four advantages of PHP.
  • 13.
    PHP Installation • Tostart using PHP, you can: • Find a web host with PHP and MySQL support • Install a web server on your own PC, and then install PHP and MySQL • If your server has activated support for PHP you do not need to do anything. • Just create some .php files, place them in your web directory, and the server will automatically parse them for you. • You do not need to compile anything or install any extra tools. • Because PHP is free, most web hosts offer PHP support.
  • 14.
    PHP Installation • However,if your server does not support PHP, you must: • install a web server (XAMPP, WAMP) • install PHP • install a database, such as MySQL
  • 15.
    Write PHP Online •Write PHP Online is an online code editor which helps you to write and test run/execute your php code online from your browser. • This is one of the simple and powerfull online php code editor tool available on the internet. • Write PHP Online supports all PHP functionalities and it runs using PHP version 7. http://www.writephponline.com/
  • 16.
    Write PHP Online 1.Write the PHP Code. 2. Click the “Run Code” button to see the output.
  • 17.
    PHP Syntax • APHP script is executed on the server, and the plain HTML result is sent back to the browser. • A PHP script can be placed anywhere in the document. • A PHP script starts with <?php and ends with ?>. • PHP statements are terminated by a semicolon (;) • The default file extension for PHP files is ".php". • A PHP file normally contains HTML tags, and some PHP scripting code.
  • 18.
    PHP Syntax • Example <?php echo"Hello World!"; ?> Output:
  • 19.
    PHP in HTML •PHP is designed to interact with HTML and PHP scripts can be included in an HTML page without a problem. • In an HTML page, PHP code is enclosed within special PHP tags. <html> <body> <?php echo "Hello World!"; ?> </body> </html>
  • 20.
    HTML in PHP •To write HTML code in PHP file, you need to use Echo or Print statement. <?php echo “Hello World”; echo “<h1> Hello World</h1>”; ?> Output:
  • 21.
    Echo/ Print Statement •Used to output data to the screen. • The echo statement can be used with or without parentheses: echo or echo(). <?php echo "Hello world!"; echo 123; ?> Output:
  • 22.
    Echo / PrintStatement • Using HTML tags in PHP: <?php echo “Hello”; echo “<br>”; echo “World”; ?> <?php echo “Hello <br> World”; ?>
  • 23.
    Try This <?php echo "<tableborder=1 width=500>"; echo "<tr>"; echo "<td>1</td><td>2</td>"; echo "</tr>"; echo "<tr>"; echo "<td>3</td><td>4</td>"; echo "</tr>"; echo "</table>"; ?> Output:
  • 24.
    Quiz Write a PHPcode to display the following outputs: a. Welcome to FTMS College b. University of East London c. CN5109 Web Application Development
  • 25.
    Variables • Variables are"containers" for storing information. • In PHP, a variable starts with the $ sign, followed by the name of the variable Note: When you assign a text value to a variable, put quotes around the value. Note: Unlike other programming languages, PHP has no command for declaring a variable. It is created the moment you first assign a value to it. $txt = "Hello world!"; $x = 5; $y = 10.5;
  • 26.
    Variables Rules for PHPvariables: • 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)
  • 27.
    Quiz Determine whether thesevariable names are valid or not valid. a. text b. $txt1 c. $1txt d. $txt 1 e. $txt#1 f. $txt_1
  • 28.
    Comments in PHP •A comment in PHP code is a line that is not read/executed as part of the program. Its only purpose is to be read by someone who is looking at the code. • Comments can be used to: • Let others understand what you are doing • Remind yourself of what you did - Most programmers have experienced coming back to their own work a year or two later and having to re-figure out what they did. Comments can remind you of what you were thinking when you wrote the code
  • 29.
    Comments in PHP <?php //This is a single-line comment # This is also a single-line comment /* This is a multiple-lines comment block that spans over multiple lines */ echo “This is not a comment”; ?>
  • 30.
    Data Types • Variablescan store data of different types, and different data types can do different things. • Data types are declarations for variables. This determines the type and size of data associated with variables. • PHP supports the following data types: • String • Integer • Float (floating point numbers - also called double) • Boolean • Array • NULL
  • 31.
    Data Types -String • A string is a sequence of characters, like "Hello world!". • A string can be any text inside quotes. <?php $x = "Hello world!"; $y = “Hi There!”; echo $x; echo "<br>"; echo $y; ?>
  • 32.
    Data Types -Integer • An integer data type is a non-decimal number between - 2,147,483,648 and 2,147,483,647. • Rules for integers: • An integer must have at least one digit • An integer must not have a decimal point • An integer can be either positive or negative • Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0) <?php $x = 5985; echo $x; ?>
  • 33.
    Data Types -Float • A float (floating point number) is a number with a decimal point or a number in exponential form. • In the following example $x is a float. • The PHP var_dump() function returns the data type and value. <?php $x = 10.365; echo $x; ?>
  • 34.
    Data Types -Boolean • A Boolean represents two possible states: TRUE or FALSE. • Booleans are often used in conditional testing. $x = true; $y = false; echo $x;
  • 35.
    Data Types -Array • An array stores multiple values in one single variable. • In the following example $cars is an array. <?php $cars = array("Volvo","BMW","Toyota","Honda"); echo $cars[0],"<br>"; echo $cars[1],"<br>"; echo $cars[2],"<br>"; echo $cars[3],"<br>"; ?>
  • 36.
    Data Types -Null • Null is a special data type which can have only one value: NULL. • A variable of data type NULL is a variable that has no value assigned to it. • Tip: If a variable is created without a value, it is automatically assigned a value of NULL. • Variables can also be emptied by setting the value to NULL <?php $x = null; echo $x; ?>
  • 37.
    Quiz Identify the datatype of the following data: a. “Cyberjaya” b. 1.35 c. 50 d. True e. A f. “False” g. h. 1, 2, 4, 8 i. “25”
  • 38.
    Operator • Operators areused to perform mathematical operations on variables and values. • PHP divides the operators in the following groups: • Arithmetic operators • Assignment operators • Comparison operators • Increment/Decrement operators • Logical operators • String operators • Array operators
  • 39.
    Arithmetic Operators • Arithmeticoperators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc. Operation Name Example Result + Addition $x + $y Sum of $x and $y - Subtraction $x - $y Difference of $x and $y * Multiplication $x * $y Product of $x and $y / Division $x / $y Quotient of $x and $y % Modulus $x % $y Remainder of $x divided by $y
  • 40.
    Arithmetic Operators • Example $x= 10; $y = 5; echo $x + $y,"<br>"; echo $x - $y,"<br>"; echo $x * $y,"<br>"; echo $x / $y,"<br>"; echo $x % $y,"<br>"; Output:
  • 41.
    Comparison Operators • ThePHP comparison operators are used to compare two values (number or string). Operator Name Example Result == Equal $x == $y Returns true if $x is equal to $y != Not equal $x != $y Returns true if $x is not equal to $y <> Not equal $x <> $y Returns true if $x is not equal to $y > Greater than $x > $y Returns true if $x is greater than $y < Less than $x < $y Returns true if $x is less than $y >= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y <= Less than or equal to $x <= $y Returns true if $x is less than or equal to $y
  • 42.
    Comparison Operators • Example $x= 10; $y = 5; echo $x == $y,"<br>"; echo $x <> $y,"<br>"; echo $x < $y,"<br>"; echo $x > $y,"<br>"; Output:
  • 43.
    Increment /Decrement Operators •Increment operators are used to increment a variable's value. • Decrement operators are used to decrement a variable's value. Operator Name Description ++$x Pre-increment Increments $x by one, then returns $x $x++ Post-increment Returns $x, then increments $x by one --$x Pre-decrement Decrements $x by one, then returns $x $x-- Post-decrement Returns $x, then decrements $x by one
  • 44.
    Increment /Decrement Operators •Example $x = 10; echo $x ,"<br>"; echo ++$x ,"<br>"; echo $x ,"<br>"; echo $x++ ,"<br>"; Output:
  • 45.
    Logical Operators • Example OperatorName Example Result and And $x and $y True if both $x and $y are true or Or $x or $y True if either $x or $y is true xor Xor $x xor $y True if either $x or $y is true, but not both && And $x && $y True if both $x and $y are true || Or $x || $y True if either $x or $y is true ! Not !$x True if $x is not true
  • 46.
    Logical Operators • Example $x= 100; $y = 50; if ($x == 100 and $y == 50) { echo "Hello world!"; } Output:
  • 47.
    Quiz Solve the followingequations: Given $x = 10 and $y = 5 a. $x + $x + $y b. $x - $y + $x c. $x + $y * $x d. $x / $y + $x e. $x > $y f. $y != $x g. ++$x h. $y--