Introduction to PHP.

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:
 2005 Pearson Education, Inc. All rights reserved Introduction.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
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”
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
Javascript and the Web Whys and Hows of Javascript.
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.
PHP : Hypertext Preprocessor
CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages.
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.
Nael Alian Introduction to PHP
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
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.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
CHAPTER 6 Introduction to PHP5 Part I อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
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,
CGS 3066: Web Programming and Design Spring 2016 PHP.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
PHP using MySQL Database for Web Development (part II)
Web Database Programming Using PHP
CGS 3066: Web Programming and Design Spring 2017
Session 2 Basics of PHP.
Whatcha doin'? Aims: To start using Python. To understand loops.
Introduction to Dynamic Web Programming
Chapter 6 JavaScript: Introduction to Scripting
Chapter 5 Scripting Language
CHAPTER 5 SERVER SIDE SCRIPTING
Python Let’s get started!.
JavaScript is a programming language designed for Web pages.
Web Database Programming Using PHP
PHP Hypertext Preprocessor
Introduction to Web programming
PHP (PHP: Hypertext Preprocessor)
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
Variables, Expressions, and IO
Chapter 5 Scripting Language
BASIC PHP and MYSQL Edward S. Flores.
PHP Introduction.
Intro to PHP & Variables
Exercises on JavaScript & Revision
Number and String Operations
WEB PROGRAMMING JavaScript.
Module 1 Introduction to PHP 11/30/2018 CS346 PHP.
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Intro to PHP.
Tutorial 6 PHP & MySQL Li Xu
Tutorial 10: Programming with javascript
JavaScript Basics What is JavaScript?
JavaScript is a scripting language designed for Web pages by Netscape.
PHP an introduction.
23 PHP.
PHP By Prof. B.A.Khivsara Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and.
Presentation transcript:

Introduction to PHP

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.

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)

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

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).

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

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

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

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

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

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

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

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

Processing Just like HTML is processed sequentially (in order), so is PHP code. <?php echo “Hello “; echo “world!”; ?> Results in Hello world!.

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: <?php echo “Hello “; echo “world!”; ?>

More Fun With Echo echo <string>; 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!

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!

Simple Math We can do math! <?php echo “If I add one to one, it is “; echo (1+1); ?> Displays If I add one to one it is 2

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

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

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

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

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

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

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

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

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

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;

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

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

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

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

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

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

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

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!

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!

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!

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

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

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!)

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

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

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!

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)

Forms (User Input) Often times we want to get user input from HTML forms. Here’s an example of a simple form: <form name=“testform” method=“get” action=“test.php”> What is your name: <input type=“text” name=“username”> <input type=“submit” value=“Go!”></form> Should result in a form with a textbox to enter your name and a “Go!” button.

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

Web Servers Introduction to Apache

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

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

Apache: Installation Your don’t need Linux See: http://www.mcs.csuhayward.edu/~bhecker/CS-3520/Tools/PHP/Installing PHP under Windows.doc CS-3520/Tools/PHP directory contains everything that you need for the install

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…