School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University

Similar documents
PHP Personal Home Page PHP: Hypertext Preprocessor (Lecture 35-37)

B. V. Patel Institute of BMC & IT 2014

Lecture 12. PHP. cp476 PHP

Web Scripting using PHP

Web Scripting using PHP

M275 - Web Development using PHP and MySQL

PHP. Interactive Web Systems

Suppose for instance, that a client demands the following page (example1.php).

CSC Web Programming. Introduction to JavaScript

Introduction of PHP Created By: Umar Farooque Khan

PHP 1. Introduction Temasek Polytechnic

Alternatively, use : after else and use a final endif. Blocks of statements are grouped by { } Sys.Prog & Scripting - HW Univ 2

PHPoC Language Reference > Overview. Overview

Language Reference Manual

School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University

CSCB20 Week 8. Introduction to Database and Web Application Programming. Anna Bretscher* Winter 2017

School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University

COMP519 Web Programming Lecture 11: JavaScript (Part 2) Handouts

The PHP language. Teaching you everything about PHP? Not exactly Goal: teach you how to interact with a database via web

VLC : Language Reference Manual

Let's Look Back. We talked about how to create a form in HTML. Forms are one way to interact with users

WEBD 236 Web Information Systems Programming

PHP and MySQL for Dynamic Web Sites. Intro Ed Crowley

URLs and web servers. Server side basics. URLs and web servers (cont.) URLs and web servers (cont.) Usually when you type a URL in your browser:

Quick Introduction to PHP

Options. Real SQL Programming 1. Stored Procedures. Embedded SQL

ITS331 Information Technology I Laboratory

Learning Language. Reference Manual. George Liao (gkl2104) Joseanibal Colon Ramos (jc2373) Stephen Robinson (sar2120) Huabiao Xu(hx2104)

PHP INTERVIEW QUESTION-ANSWERS

COMS 469: Interactive Media II

Flow Control. Copyleft 2005, Binnur Kurt

Server side basics CS380

PHP by Pearson Education, Inc. All Rights Reserved.

PHP: The Basics CISC 282. October 18, Approach Thus Far

Full file at C How to Program, 6/e Multiple Choice Test Bank

School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University

Course Syllabus. Course Title. Who should attend? Course Description. PHP ( Level 1 (

3 The Building Blocks: Data Types, Literals, and Variables

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

COMP284 Scripting Languages Lecture 15: JavaScript (Part 2) Handouts

Python Class-Lesson1 Instructor: Yao

FSASIM: A Simulator for Finite-State Automata

Agenda. 1. Brief History of PHP. 2. Getting started. 3. Examples

Chapter 7:- PHP. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

What is PHP? [1] Figure 1 [1]

Title:[ Variables Comparison Operators If Else Statements ]

EZ- ASCII: Language Reference Manual

Regular Expressions. Regular expressions are a powerful search-and-replace technique that is widely used in other environments (such as Unix and Perl)

BoredGames Language Reference Manual A Language for Board Games. Brandon Kessler (bpk2107) and Kristen Wise (kew2132)

String Computation Program

COMP284 Scripting Languages Lecture 10: PHP (Part 2) Handouts

CS 374 Fall 2014 Homework 2 Due Tuesday, September 16, 2014 at noon

Lecture 7 PHP Basics. Web Engineering CC 552

PHP. Introduction. PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server

Homework 1 Due Tuesday, January 30, 2018 at 8pm

CHIL CSS HTML Integrated Language

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Programming Languages & Translators. XML Document Manipulation Language (XDML) Language Reference Manual

JavaScript: Introduction to Scripting

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

Creating HTML files using Notepad

corgi Language Reference Manual COMS W4115

COMP519 Web Programming Lecture 27: PHP (Part 3) Handouts

PHP: Hypertext Preprocessor. A tutorial Introduction

Dr. Sarah Abraham University of Texas at Austin Computer Science Department. Regular Expressions. Elements of Graphics CS324e Spring 2017

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

COMP519 Practical 15 PHP (1)

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Instructor s Notes Web Data Management PHP Sequential Processing Syntax. Web Data Management PHP Sequential Processing Syntax

WEBD 236 Web Information Systems Programming

Such JavaScript Very Wow

Pathologically Eclectic Rubbish Lister

1/22/2017. Chapter 2. Functions and Control Structures. Calling Functions. Objectives. Defining Functions (continued) Defining Functions

EECS1012. Net-centric Introduction to Computing. Crash Course on PHP

QUark Language Reference Manual

Running SQL in Java and PHP

ASML Language Reference Manual

Princeton University COS 333: Advanced Programming Techniques A Subset of PHP

By the end of this chapter, you should be comfortable writing basic PHP that will allow you to create, store, manipulate, and output data.

CS/ECE 374 Fall Homework 1. Due Tuesday, September 6, 2016 at 8pm

Language Reference Manual

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

- Origins - Rasmus Lerdorf Developed to allow him to track visitors to his Web site

CSCB20 Week 8. Introduction to Database and Web Application Programming. Anna Bretscher* Winter 2016

Zend PHP Certification Study Guide. Copyright 2005 by Sams Publishing. International Standard Book Number: Warning and Disclaimer

Getting Started Values, Expressions, and Statements CS GMU

CHAD Language Reference Manual

COMP284 Scripting Languages Lecture 11: PHP (Part 3) Handouts

Flow Control 10. Flow Control. Content. if Statement. Conditional Statements. if Statement. Example. if, if/else, switch for, while, do/while

Computing with Strings. Learning Outcomes. Python s String Type 9/23/2012

Well-formed XML Documents

JavaScript Functions, Objects and Array

recall: a Web page is a text document that contains additional formatting information in the HyperText Markup Language (HTML)

Language Reference Manual simplicity

PhpT Point, Simply Easy Learning

GNU ccscript Scripting Guide IV

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

Transcription:

School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University ITS351 Database Programming Laboratory Laboratory #2: PHP Objective: - To introduce basic syntax and constructs of PHP scripting language. - To study how to feed input data to PHP scripts. - To provide hand-on practices on PHP programming. Introduction to PHP 1. Basics PHP is a scripting language designed for web application development. PHP interpreter resides on the server side with the web server. PHP code can be embedded into HTML to generate dynamic contents. PHP statements are enclosed in and tag. Most of PHP syntax is inspired from C programming language. Here is an example of a snippet of PHP code embedded into a HTML page. <head> <title>basic PHP</title> </head> echo "<h1>hello, World</h1>"; echo "10 + 20 = "; echo 10+20; echo "\n"; On a client s request, the above HTML page with embedded PHP code is passed to the PHP interpreter. The interpreter then interprets part of the code surrounded with and (there can be many of these), generates the output according to what the code does Copyright ICT Program, Sirindhorn International Institute of Technology, Thammasat University 1/8

(printing Hello, World in this case), and puts the result back to the HTML page. In the end, this PHP code yields the following HTML code. <head> <title>basic PHP</title> </head> <h1>hello, World</h1>10 + 20 = 30 2. Variables Variables in PHP are denoted by a dollar sign followed by the variable name. The variable name is case-sensitive, and may compose of alphabets, digits or underscore, but it must not start with a digit. $x = 10; $y = 'Hello'; $z = $x.$y; //. is used to concatenate strings echo $z; //output: 10Hello 3. Strings We can use both single quotes and double quotes to enclose string literals in PHP. However, they yield different results. A single-quoted string is used for representing a simple string constant. It does not recognize escape sequences such as \r, \n, except \' for a single quote, and \\ for a backslash. A double-quoted string recognizes all types of escape sequences, as well as variable substituting. Strings can be accessed the same way as in the case of arrays. $name = 'John'; $age = 20; $txt = "His name is $name. He is \"{$age}\" years old."; echo $txt; //output: His name is John. He is "20" years old. $str = "SIIT, TU"; echo $str[0].$str[1].$str[2].$str[3].$str[strlen($str)-1]; //output: SIITU Notice the use of strlen($str) to find the length of the string $str. Copyright ICT Program, Sirindhorn International Institute of Technology, Thammasat University 2/8

4. Arrays An array in PHP is an ordered map associating key and value. That is, an array in PHP can act as both usual array and a hash table. In addition to being dynamic in its size, PHP arrays are inhomogeneous i.e., values of different types can be stored in the same array. $arr = array('foo' => 'bar', 12 => true); $arr[] = 20; //$arr[13] = 20; $arr['hello'] = 'World'; //$arr['hello'] = 'World'; $arr[] = "PHP"; //$arr[14] = "PHP"; print_r($arr); echo "<br>"; unset($arr[13]); print_r($arr); //print_r displays the content of the array //remove the element from the array The following code demonstrates how PHP arrays can be used in an index-based manner. $arr = array('a', 2,'b', 'c', 10); for($i=0; $i < count($arr); ++$i){ echo $arr[$i]. '<br>'; } This prints out a, 2, b, c, 10 on a separate line. Note that index of arrays in PHP starts from 0. The function count($arr) can be used to find the size of array $arr. 5. If Statement PHP s if statements work the same way as in C. $a = 10; if ($a == 10) { <p>the condition is true.</p> } else { <p>the condition is false.</p> } Notice that PHP also allows a dynamic opening and closing of and. In the code above, <p>the condition is true.</p> will be printed out. Copyright ICT Program, Sirindhorn International Institute of Technology, Thammasat University 3/8

We can write an if statement in an alternative version which is different from C syntax. This alternative syntax somehow makes the PHP code become clearer and easier to read when many PHP statements are embedded in HTML page. <? $a = 10; <? if ($a == 10): <p>the condition is true.</p> <? else: <p>the condition is false.</p> <? endif; In multiple nested if-else statements, you may use both else if and elseif in the C syntax above. However, in the latter one (i.e. colon syntax), only elseif is accepted. 6. While Statement <table border="1"> $i = 1; while($i <= 10) { <tr><td><?= $i</td><td><?= str_repeat("*", $i) </td></tr> $i++; } This gives There is also another version of the while statement using : which gives the same result. Copyright ICT Program, Sirindhorn International Institute of Technology, Thammasat University 4/8

<table border="1"> <? $i = 1; <? while($i <= 10): <tr><td><?= $i</td><td><?= str_repeat("*", $i) </td></tr> <? $i++; <? endwhile; 7. For Statement For statements also have a similar syntax as in C. <table border="1"> <? for($i=1; $i<=10; $i++): <tr><td><?= $i</td><td><?= str_repeat("*", $i) </td></tr> <? endfor; Additionally, PHP comes with a foreach statement. <? $arr = array('peter' => 10, 'John' => 15, 'Somsak' => 22, 'Sarah' => 12); <table border="1"> <? foreach($arr as $key => $value): <tr><td><?= $key</td><td> <?= str_repeat("*", $value) </td></tr> <? endforeach; In each iteration, $key and $value are respectively assigned to the key and the value of an element in the array. Thus, the code gives the following output. Copyright ICT Program, Sirindhorn International Institute of Technology, Thammasat University 5/8

8. Query String Query string is a part of URL that comes after the file name. It is used to pass parameters to a PHP script file. For example, http://localhost/test.php calls 'test.php' without passing any parameter. But, http://localhost/test.php?a=10&b=20 passes two parameters: a=10 and b=20. Passing parameters to the script in this way is referred to as a GET request. These two parameters can be accessed in PHP script through a global variable $_GET. <? if (isset($_get['a']) && isset($_get['b'])): <?= $_GET['a'] + <?= $_GET['b'] = <?= $_GET['a']+$_GET['b'] <? else: Please specify the values of a and b. <? endif; isset() is used to check if a variable is available, or if a value has been passed into the script. When passing a=10 and b=20 to the code above, the output is 10 + 20 = 30 9. Useful Functions str_ireplace($target, $replacement, $text) Replace all occurrences of a substring in an input string with a specified replacement For example, str_ireplace("red", "black", "Change Red to Black"); This replaces red with black (case-insensitive) and thus gives a new string Change black to Black. str_replace($target, $replacement, $text) Same as str_ireplace() but case-sensitively searches for the target string. trim($str) Strip whitespaces (including newlines and tabs) from the beginning and end of a string. rand($min, $max) Return a random integer from $min to $max (inclusive). rand() is automatically seeded. Copyright ICT Program, Sirindhorn International Institute of Technology, Thammasat University 6/8

Worksheet 1. Create a PHP file named "multiply.php" that shows a multiplication table sized 12x12 as shown below x 1 2 3 4 5 6 7 8 9 10 11 12 1 1 2 3 4 5 6 7 8 9 10 11 12 2 2 4 6 8 10 12 14 16 18 20 22 24 3 3 6 9 12 15 18 21 24 27 30 33 36 4 4 8 12 16 20 24 28 32 36 40 44 48 5 5 10 15 20 25 30 35 40 45 50 55 60 6 6 12 18 24 30 36 42 48 54 60 66 72 7 7 14 21 28 35 42 49 56 63 70 77 84 8 8 16 24 32 40 48 56 64 72 80 88 96 9 9 18 27 36 45 54 63 72 81 90 99 108 10 10 20 30 40 50 60 70 80 90 100 110 120 11 11 22 33 44 55 66 77 88 99 110 121 132 12 12 24 36 48 60 72 84 96 108 120 132 144 The following code prints the first heading row. You may use the code as the starting point. <head> <title>multiplication Table</title> </head> <table style="text-align:center;" border="1"> <tr> <th>x</th> <? for($i=1; $i<=12; $i++): <th><?= $i </th> <? endfor; </tr> Add a nested for loop to make the full table. Copyright ICT Program, Sirindhorn International Institute of Technology, Thammasat University 7/8

2. Write a PHP code to display a table of logarithm as shown. Use an array to store x, and loop through the array to compute log. Use log($x, 10) for log base 10. DO NOT hard code the whole table in HTML. 3. Display the following shape with loops in PHP Exercises To be announced. Copyright ICT Program, Sirindhorn International Institute of Technology, Thammasat University 8/8