1 Introduction to PHP. 2 What is this “PHP” thing? Official description: “PHP, which stands for "PHP: Hypertext Preprocessor" is a widely-used Open Source.

Slides:



Advertisements
Similar presentations
Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.
Advertisements

Introducing JavaScript
1 Database Driven Web Application Clients Application Servers including web servers Database Server Traditional client-server (2-tier architecture): client:
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Introduction to PHP and PostgreSQL CSC 436 – Fall 2008 Slides based on PHP manual “Getting Started” from
1 CS428 Web Engineering Lecture 18 Introduction (PHP - I)
Introduction to scripting
8/17/2015CS346 PHP1 Module 1 Introduction to PHP.
PHP: Introduction By Trevor Adams.
PHP Workshop ‹#› PHP: The Basics. PHP Workshop ‹#› What is it? PHP is a scripting language commonly used on web servers. –Stands for “PHP: Hypertext Preprocessor”
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 03.
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
CSC 318 WEB APPLICATION DEVELOPMENT.  Introduction to Server Scripting language  Client VS Server  Introduction to PHP  PHP Files and Syntax  Function.
Javascript and the Web Whys and Hows of Javascript.
PHP Overview CS PHP PHP = PHP: Hypertext Preprocessor Server-side scripting language that may be embedded into HTML One goal is to get PHP files.
PHP : Hypertext Preprocessor
CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages.
PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators.
PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.
INTERNET APPLICATION DEVELOPMENT For More visit:
Nael Alian Introduction to PHP
Week 9 PHP Cookies and Session Introduction to JavaScript.
IST 210: PHP BASICS IST 210: Organization of Data IST210 1.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
Introduction to PHP Advanced Database System Lab no.1.
Fall 2004CSI University of Ottawa Introduction to PHP Basic principles and syntax.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
CS 4720 Dynamic Web Applications CS 4720 – Web & Mobile Systems.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
Introduction to PHP.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
IT ELECTIVE 2.  Web server Can refer to either the hardware (the computer) or the software (the computer application) that helps to deliver content that.
CHAPTER 6 Introduction to PHP5 Part I อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
1) PHP – Personal Home Page Scripting Language 2) JavaScript.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 2: Introduction to IS2803 Rob Gleasure
PHP Syntax You cannot view the PHP source code by selecting "View source" in the browser - you will only see the output from the PHP file, which is plain.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
PHP-Basic By- Tanvi Raval. Pre-requisites Before you continue you should have a basic understanding of the following: 1. HTML 2. JavaScript.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
CGS 3066: Web Programming and Design Spring 2016 PHP.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
PHP using MySQL Database for Web Development (part II)
Introduction to Dynamic Web Programming
CHAPTER 5 SERVER SIDE SCRIPTING
PHP Hypertext Preprocessor
PHP (PHP: Hypertext Preprocessor)
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
PHP Introduction.
Intro to PHP & Variables
WEB PROGRAMMING JavaScript.
PHP.
Tutorial 6 PHP & MySQL Li Xu
Tutorial 10: Programming with javascript
PHP an introduction.
Introduction to PHP.
Presentation transcript:

1 Introduction to PHP

2 What is this “PHP” thing? Official description: “PHP, which stands for "PHP: Hypertext Preprocessor" is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. Its syntax draws upon C, Java, and Perl, and is easy to learn.

3 What does PHP do? Most commonly: used inside a web server to parse pages and dynamically generate content. Differs from a language like Javascript in that it is Server Side (processed on the server)

4 Normal HTML Document Requires only a web browser to read Can be read off local disk, or transferred from server via HTTP

5 PHP Document In order to be correctly read and processed, must be parsed through a server capable of parsing PHP code. Server processes code: formats a standard HTML document out of the PHP code, then transfers via HTTP to client browser. No client software required (other than web browser).

6 ClientServer Simple Transfer of Normal HTML.HTML File Web Server Application (Apache) Client’s Browser (MSIE, Netscape, Etc) HTML

7 ClientServer Simple Transfer of PHP.PHP File Web Server Application (Apache) Client’s Browser (MSIE, Netscape, Etc) HTML PHP Parsing Module HTML PHP & HTML

8 What does a PHP file look like? Hello <?php echo “world!”; ?>

9 More… A PHP file is just a HTML file with PHP code inserted where needed! To designate PHP code, we use the tags to indicate the start and end of code to be parsed.

10 Parsing… Hello <?php echo “world!”; ?> Hello world

11 Our First Command! Echo! echo ; Example: echo “Hello!”; Result: Hello! Example: echo “I love PHP!”; Result: I love PHP!

12 What is this String? echo ; But what is a string? A string is any sequence of characters. This also includes a sequence of no characters.

13 These are Strings “Hi” “I love PHP!” “5” “” Note that saying “5” is NOT the same as saying just 5.

14 Processing Just like HTML is processed sequentially (in order), so is PHP code. Results in Hello world!.

15 General Syntax After typing a command, you should always put a semicolon (;) after it. It is good practice to indent your code one tab after the <?php tag Another good practice is to put each command on its own line. Ex:

16 More Fun With Echo echo ; We can put in numeric values in too! echo “5”; and echo 5; both display 5. This is because PHP can convert a number to a string automatically!

17 Data Types Data is categorized by Data Types PHP is not a strongly typed language, doesn’t require you to define types string – text data – “Hello world”, “My password is 12345”, “12345” double – number data – -5, 0, 11, 11.5, 13 int – number data, but won’t remember decimals - -5, 0, 11, 13 boolean – true or false? – true, false Categorize these: -5.51, “-5.51”, true, 5 double, string, bool, int OR double!

18 Simple Math We can do math! Displays If I add one to one it is 2

19 Example ”; echo “PHP can do math: “; echo (8*10); ?> Displays: I love PHP! PHP can do math: 80

20 String Concatenation We can add numbers: 2+2 How do we add strings? We use the. Operator (concatenation) Echo “5 plus 5 is “. (5+5); Displays 5 plus 5 is 10

21 Example ”. “PHP can do math: “. (8*10); ?> Displays: I love PHP! PHP can do math: 80

22 Comments You can add human-readable text to your programs: // will tell PHP to ignore the rest of the line <?php // I love PHP! :) echo (8*10); // I can’t add :( ?> Displays: 80

23 Comments Multiple lines are possible, just start a comment with /* and end with */ <?php /* I love PHP! :) PHP Programming is fun! */ echo (8*10); /* I can’t add */ ?> Displays: 80

24 Variables! Variables let you save a value to use/modify later on. In PHP to make a variable, assign it a value. Precede variable names with a $ sign to indicate that it’s a variable. $foo = 5+5; // $foo is 10 $bar = 5+$foo; // $bar is 15

25 Valid Variable Names Variable names start with a letter or underscore Then followed by any number of letters, numbers, or underscores Names are case-sensitive ($a is not $A) $asdf = valid $_5asdf = valid $5asdf = NOT valid $asdf != $aSdF

26 Reserved Names Some variable names are reserved by PHP $GLOBALS $_SERVER $_GET $_POST $_COOKIE $_FILES $_ENV $_REQUEST $_SESSION

27 Using Variables As shown, we can do assignment and such: $foo = 5; $bar = $foo + 1; Other ways of doing assignment: $foo++; // Increases foo by one $foo+=2; // Increases foo by two $foo*=2; // Multiples foo by two $foo /=2; // Divides foo by two $foo -=2; // Subtracts foo by two

28 More uses We can use variables in functions: $foo = 5; echo “Foo is $foo”; Same thing as: $foo = 5; echo “Foo is “. $foo; Echo automatically converts $foo to its value when using double quotes (“), when using single quotes, do string concatenation $foo = 5; echo ‘Foo is ‘. $foo;

29 What does this program do? $foo = 5; $foo++; echo “Foo is $foo”; Displays: Foo is 6

30 What does this program do? $_BAR = 1; $_bar = 2; echo “Bar is $_BAR”; Displays: Bar is 1

31 A little about ++ The ++ operator is neat in that not only does it increase the variable, it can be used inside of commands! ++ before a variable: increases variable, and function is given NEW value ++ after a variable: increases a variable, and function is given OLD value $foo = 2; $bar = 2; echo “Foo is: “. $foo++; echo “ - Bar is: “. ++$bar; // Now Foo and Bar are both 3 in memory Outputs Foo is: 2 – Bar is: 3

32 What does this program do? $1var = 1; $2var = $1var++; echo “2var is $2var”; Displays: PARSE ERROR! (Haha!) $a = 1; $b = $a++; echo “b is $b”; Displays: b is 1

33 Conditionals if (conditional) { result }; Evaluates conditional, and if TRUE, then does result commands. We use conditional operators, such as: –== for equals –!= for not equals –> for greater –< for less than –>= for greater than or equal –<= for less than or equal

34 Example $bar = 2; if ($bar == 2) { echo “Bar is 2, yay! ”; echo “w00t!”; } Displays: Bar is 2, yay! w00t!

35 Another Example $foo = 3; $bar = 300; if ($foo > $bar) { echo “foo is big!”; } Displays nothing.

36 Only one command? $foo = 3; $bar = 300; if ($foo > $bar) echo “foo is big!”; You may omit the { and } if you only have one command. Don’t forget to use them when you have two commands or more!

37 Another Example $foo = 2; if ($foo != 2) echo “Foo is not two ”; echo “Foo is still not two”; Displays Foo is still not two Be careful not to forget brackets. Tabbing your code will help you find these logic errors!

38 Else You can also specify what to do if the If conditional is FALSE if (condition) { result } else { result } $foo = 5; if ($foo != 5) { echo “Foo isn’t five!”; } else { echo “Foo IS five!”; } Displays: Foo IS five!

39 Another If/Then/Else example $a = 0; if ($a == 1) $a++; else $a--; echo $a; Displays -1

40 Arrays In PHP, Arrays are actually quite easy to use, and very powerful (you’ll see why in a minute!) We map keys to values. array( [key =>] value,... ) key may be an integer or string value may be any value

41 Array Example $arr = array("foo" => "bar", 12 => true); echo $arr["foo"]; // bar echo $arr[12]; // true So we see that arrays are just like variables, except that they also contain a collection of variables themselves. (Arrays can even have arrays in them!)

42 More Array Examples $arr = array(5 => 1); $arr[5]++; echo $arr[5]; // 2 $arr[5] = 10; echo $arr[5]; // 10 $arr[“foo”] = 5; // New entry! echo $arr[“foo”]; // 5

43 Deleting from Array Use unset() to delete from array $arr = array(0=>“foo”, 1=>“bar”); echo $arr[0]; // foo echo $arr[1]; // bar unset($arr[0]); // deletes 0=>”foo” unset($arr); // deletes entire array

44 Auto-numbering If you don’t specify a key, PHP assumes the largest key used + 1 (or zero if the largest + 1 is negative) Example: array(5 => 43, 6 => 32, 7 => 56, "b" => 12); array(5 => 43, 32, 56, "b" => 12); These are identical!

45 More Auto-Numbering $array = array(0, 1, 2, 3, 4); $array[] = 5; Now [0] => 0, [1] => 1, etc (including 5) Tip: Use print_r($array); command to display an array in human-readable form. Note: Even if you were to delete 0-5, the next key automatically assigned would still be 6. To fix this, use array_values($array)

46 Forms (User Input) Often times we want to get user input from HTML forms. Here’s an example of a simple form: What is your name: Should result in a form with a textbox to enter your name and a “Go!” button.

47 Post & Get Refresher Remember: POST method puts data into the browser’s request –Transparent to user GET method puts form data into browser string –Urls look like this: Pagename.php?key1=value1&key2=value2 –For our form, we use GET, so if a user enters “Palazzo” at the prompt: Test.php?username=Palazzo

48 Web Servers Introduction to Apache

49 What does a web server do? A web server serves files to clients (such as the browser) Files may be HTML, GIFs, video, PDF Serves multiple clients at the same time Transfer protocol: HTTP

50 Apache model Provides multiple processes to handle simultaneous requests Throttles the number of child processes Crashing a child process doesn’t crash the server; the parent is very stable Memory leaks don’t take down the machine; memory freed when child exits

51 Apache: Installation Your don’t need Linux See: /Tools/PHP/Installing PHP under Windows.doc /Tools/PHP/Installing PHP under Windows.doc CS-3520/Tools/PHP directory contains everything that you need for the install

52 End of Lecture Install Apache or get access to a system that has it. You can use Windows, Linux, Unix or whatever you want. Next time, more PHP coding…