Intro to PHP – Page 1 of 43CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: Intro to PHP Reading: Chapters 1.

Slides:



Advertisements
Similar presentations
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
Advertisements

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.
PHP Intro/Overview Squirrel Book pages Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
ASP.NET Programming with C# and SQL Server First Edition
JavaScript, Third Edition
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
Basic Elements of C++ Chapter 2.
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
PHP == ‘ Hypertext Preprocessor ’ Open-source, server-side scripting language Used to generate dynamic web-pages PHP scripts reside between reserved PHP.
AIT 616 Fall 2002 PHP. AIT 616 Fall 2002 PHP  Special scripting language used to dynamically generate web documents  Open source – Free!!!  Performs.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Writing Web Pages By Shyam Gurram. Agenda Writing Web Pages Delimiting PHP Program Units. Displaying Output to Web Pages Putting Comments in PHP Programs.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
Week 9 PHP Cookies and Session Introduction to JavaScript.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
IST 210: PHP BASICS IST 210: Organization of Data IST210 1.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
November 2003Bent Thomsen - FIT 6-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet.
Input, Output, and Processing
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.
Introduction to PHP A user navigates in her browser to a page that ends with a.php extension The request is sent to a web server, which directs the request.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
CSCI 2910 Client/Server-Side Programming
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 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.
Chapter 2 Variables.
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.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
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.
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
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 Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
REEM ALMOTIRI Information Technology Department Majmaah University.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
PHP using MySQL Database for Web Development (part II)
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 6 JavaScript: Introduction to Scripting
Basic Elements of C++.
Introduction to Scripting
PHP Introduction.
Basic Elements of C++ Chapter 2.
Chapter 8 JavaScript: Control Statements, Part 2
PHP Intro/Overview Bird Book pages 1-11,
PHP.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
PHP an introduction.
Chapter 2 Variables.
Presentation transcript:

Intro to PHP – Page 1 of 43CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: Intro to PHP Reading: Chapters 1 and 2

Intro to PHP – Page 2 of 43CSCI 2910 – Client/Server-Side Programming Today’s Goals Today’s lecture will cover: Differences between server-side and client- side operation Format of a PHP file Syntax of PHP code and similarities between PHP code and JavaScript code Data types

Intro to PHP – Page 3 of 43CSCI 2910 – Client/Server-Side Programming Web Scripting with PHP PHP has many benefits: Open Source Flexible for integration with HTML – HTML easily supports the use of the PHP scripting tags Suited to complex projects – database support, vast libraries, and the power of the server allow PHP to satisfy very complex programming needs. Fast at running scripts – Even though PHP is a scripting language, the architecture of the scripting engine provides for fast execution. Platform and O/S portable – PHP runs on a variety of different platforms and operating systems

Intro to PHP – Page 4 of 43CSCI 2910 – Client/Server-Side Programming General Format of a PHP File A block of PHP script is embedded within an HTML file using the tags. Can also use and The PHP script engine does not like the tag –Engine is interpreting as executable script –Remove them from your XML template to create a PHP template. –Could use PHP script to generate tag

Intro to PHP – Page 5 of 43CSCI 2910 – Client/Server-Side Programming General Format of a PHP File (continued) Just like JavaScript, whitespace is ignored. Just like JavaScript, end lines with semicolon (;). Unlike JavaScript, PHP code is executed at server and replaced with resulting output. The file must have the extension ".php". Server needs this in order to know to run the file through the PHP script engine before sending output to client.

Intro to PHP – Page 6 of 43CSCI 2910 – Client/Server-Side Programming PHP "Hello, World!" <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" " Simple XHTML Document <?php print "Hello, World!"; ?>

Intro to PHP – Page 7 of 43CSCI 2910 – Client/Server-Side Programming JavaScript "Hello, World!" <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" " Simple XHTML Document document.writeln("Hello, World!");

Intro to PHP – Page 8 of 43CSCI 2910 – Client/Server-Side Programming The Difference – "View Source"

Intro to PHP – Page 9 of 43CSCI 2910 – Client/Server-Side Programming PHP Comments As in JavaScript, there are two methods for inserting comments into your code. They are: –Block comments –End of line comments We don't need to comment out code for browsers since code is executed on server.

Intro to PHP – Page 10 of 43CSCI 2910 – Client/Server-Side Programming Block Comments /* This is a block comment. It is surrounded by the slash/asterisk and asterisk/slash that indicate the beginning and ending of a comment. A block comment may span multiple lines. */

Intro to PHP – Page 11 of 43CSCI 2910 – Client/Server-Side Programming End of Line Comments // This is an end of line comment. // Everything from the double // slashes to the end of the line // is ignored. // To use this method over // multiple lines, each line must // have its own set of double // slashes. # This is also an end of line # comment

Intro to PHP – Page 12 of 43CSCI 2910 – Client/Server-Side Programming Outputting Results Just like JavaScript, there are a number of ways to output results that are to become part of the HTML file. The earlier example uses the print command to output a string. print can also be used to output values or variables. The following slide presents examples of valid print statements.

Intro to PHP – Page 13 of 43CSCI 2910 – Client/Server-Side Programming print Examples print "Hello, World"; print 123; // Outputs "123" $outputString = "Hello, World!"; print $outputString;

Intro to PHP – Page 14 of 43CSCI 2910 – Client/Server-Side Programming echo Statement The echo statement similar to print echo, however, can take on a sequence of arguments separated by commas. Example: $outputString = "The answer is "; echo $outputString, 42, "!"; This outputs "The answer is 42!" print cannot combine elements like this.

Intro to PHP – Page 15 of 43CSCI 2910 – Client/Server-Side Programming Escape Characters Because of the number of reserved characters in PHP, escaping is necessary. Characters are escaped by preceding them with a backslash (\). Characters that need escaped include ', ", \, $, and ?. Whitespace including carriage returns are allowed as part of a string, but they are then output as part of the string. Of course, in HTML, carriage returns are considered whitespace and are ignored. As in JavaScript, single quotes can be used without escaping within double quoted strings and vice versa.

Intro to PHP – Page 16 of 43CSCI 2910 – Client/Server-Side Programming Printing Characters Not Available on Keyboard Escaping can also be used to display ISO characters that are not present on the keyboard. This is done by taking the ISO hex value and placing it after "\x". The ISO hex values can be found using the Character Map application found in Accessories  System Tools. For example, the character "¼" has the hexadecimal ISO value bc 16. This can be represented with \xbc. print "\xbc tsp" prints the string "¼ tsp"

Intro to PHP – Page 17 of 43CSCI 2910 – Client/Server-Side Programming In-Class Exercise Earlier it was stated that the PHP script engine does not like the tag. How might we still incorporate this tag in the file we send to the browser without causing problems for the PHP engine?

Intro to PHP – Page 18 of 43CSCI 2910 – Client/Server-Side Programming Variable Declarations PHP interprets the dollar sign ($) followed by a letter or an underscore (_) to be a variable name. Variables do not need to be declared before you use them. Example: $var1 = 25; To help set off a variable identifier within a string, you can surround it with curly brackets. This will become helpful when we start discussing arrays and objects. Example: echo "The value is {$var1}." will display "The value is 25."

Intro to PHP – Page 19 of 43CSCI 2910 – Client/Server-Side Programming Data Types Scalar types –boolean –float –integer –string Compound types –array –object

Intro to PHP – Page 20 of 43CSCI 2910 – Client/Server-Side Programming Using Scalar Types A boolean variable can be assigned only values of true or false. $answer = false; $finished = true; An integer is a whole number (no decimal point) $age = 31;

Intro to PHP – Page 21 of 43CSCI 2910 – Client/Server-Side Programming Using Scalar Types (continued) A float has a decimal point and may or may not have an exponent $price = 12.34; $avog_num = 6.02e23; //6.02x10^23 A string is identified as a sequence of characters $name = "John Smith"; Strings can be concatenated using a dot (.) $name = "John". " Smith";

Intro to PHP – Page 22 of 43CSCI 2910 – Client/Server-Side Programming Constants Constants associate a name with a scalar value. Constants are defined using the function define(). define("PI", ); There are a number of predefined constants. These include: –M_E = –M_PI = –M_2_SQRTPI = (Square root of pi) –M_1_PI = (Square root of 1/pi) –M_SQRT2 = (Square root of 2) –M_SQRT1_2 = (Square root of ½)

Intro to PHP – Page 23 of 43CSCI 2910 – Client/Server-Side Programming Arithmetic Operators OperatorOperationExampleResult +Addition $y = 2 + 2; $y will contain 4 –Subtraction $y = 3; $y = $y – 1; $y will contain 2 /Division $y = 14 / 2; $y will contain 7 *Multiplication $z = 4; $y = $z * 4; $y will contain 16 %Modulo $y = 14 % 3; $y will contain 2 ++Increment $y = 7; $y++; $y will contain 8 --Decrement $y = 7; $y--; $y will contain 6

Intro to PHP – Page 24 of 43CSCI 2910 – Client/Server-Side Programming Bitwise Logical Operations ~Bitwise NOT operator: Inverts each bit of the single operand placed to the right of the symbol &Bitwise AND: Takes the logical-bitwise AND of two values |Bitwise OR operator: Takes the logical- bitwise OR of two values ^Bitwise XOR: Takes the logical-bitwise exclusive-OR of two values

Intro to PHP – Page 25 of 43CSCI 2910 – Client/Server-Side Programming Bitwise Shift Operations <<Left shift: Shifts the left operand left by the number of places specified by the right operand filling in with zeros on the right side. >>Sign-propagating right shift: Shifts the left operand right by the number of places specified by the right operand filling in with the sign bit on the left side. >>>Zero-fill right shift operator: Shifts the left operand right by the number of places specified by the right operand filling in with zeros on the left side.

Intro to PHP – Page 26 of 43CSCI 2910 – Client/Server-Side Programming Flow Control As in JavaScript, flow control consists of a number of reserved words combined with syntax to allow the computer to decide which parts of code to execute, which to jump over, and which to execute multiple times. For the most part, the flow control that you learned for JavaScript is the same for PHP.

Intro to PHP – Page 27 of 43CSCI 2910 – Client/Server-Side Programming If-Statement The code below represents the syntax of a typical if-statement: if ($grade > 93) print "Student's grade is A"; If grade was 93 or below, the computer would simply skip this instruction.

Intro to PHP – Page 28 of 43CSCI 2910 – Client/Server-Side Programming If-Statement (continued) Just like JavaScript, multiple instructions may be grouped using curly brackets. For example: if ($grade > 93) { print "Student's grade is A"; $honor_roll_value = true; }

Intro to PHP – Page 29 of 43CSCI 2910 – Client/Server-Side Programming If-Statement (continued) As in JavaScript, the programmer can string together if-statements to allow the computer to select from one of a number of cases using elseif and else. (Note that JavaScript allows else if while PHP uses elseif.) For example: if ($grade > 93) print "Student's grade is an A"; elseif ($grade > 89) print Student's grade is an A-"; else print "Student did not get an A";

Intro to PHP – Page 30 of 43CSCI 2910 – Client/Server-Side Programming Comparison Operators >Returns true if the first value is greater than the second >=Returns true if the first value is greater than or equal to the second <Returns true the first value is less than the second <=Returns true if the first value is less than or equal to the second ==Returns true if first value is equal to second !=Returns true if first value is not equal to second

Intro to PHP – Page 31 of 43CSCI 2910 – Client/Server-Side Programming Logical Operators !Returns true if its operand is zero or false &&Returns false if either operand is zero or false ||Returns false if both operands are zero or false

Intro to PHP – Page 32 of 43CSCI 2910 – Client/Server-Side Programming Switch-Statement The switch statement can be used as an alternative to the if, elseif, else method. switch($menu) { case 1: print "You picked one"; break; case 2: print "You picked two"; break; default: print "You did not pick one or two"; break; }

Intro to PHP – Page 33 of 43CSCI 2910 – Client/Server-Side Programming Switch-Statement (continued) Note that if a break is not encountered at the end of a case, the processor continues through to the next case. Example: If $var1=1, it will print both lines. switch($var1) { case 1: print "The value was 1"; default: print "Pick another option"; break; }

Intro to PHP – Page 34 of 43CSCI 2910 – Client/Server-Side Programming While-loop PHP uses the while-loop just like JavaScript. Like the if-statement, this format also uses a condition placed between two parenthesis As long as the condition evaluates to true, the program continues to execute the code between the curly brackets in a round-robin fashion.

Intro to PHP – Page 35 of 43CSCI 2910 – Client/Server-Side Programming While-loop (continued) Format: while(condition) { statements to execute } Example: $count = 1; while($count < 72) { print "$count "; $count++; }

Intro to PHP – Page 36 of 43CSCI 2910 – Client/Server-Side Programming do … while loop The do … while loop works the same as a while loop except that the condition is evaluated at the end of the loop rather than the beginning Example: $count = 1; do { print "$count "; $count++; }while($count < 72);

Intro to PHP – Page 37 of 43CSCI 2910 – Client/Server-Side Programming for-loop In the two previous cases, a counter was used to count our way through a loop. This task is much better suited to a for-loop. for ($count = 1; $count < 72; $count++) { print "$count "; } A "break" can be used to break out of a loop earlier.

Intro to PHP – Page 38 of 43CSCI 2910 – Client/Server-Side Programming In-Class Exercise Convert the JavaScript shown below to PHP? <!-- value = 34.5; for(i = 0; i < 10; i++) { document.writeln("34.5/(2^" + i + ") is " + value); document.writeln(" "); value = value/2; } //-->

Intro to PHP – Page 39 of 43CSCI 2910 – Client/Server-Side Programming Type Conversion Different programming languages deal with variable types in different ways. Some are strict enforcing rules such as not allowing an integer value to be assigned to a float. The process of converting from one data type to another is called "casting". To convert from one type to another, place the type name in parenthesis in front of the variable to convert from. In some cases, there are functions that perform the type conversion too.

Intro to PHP – Page 40 of 43CSCI 2910 – Client/Server-Side Programming Some Examples of Type Conversion $ivar = (int) $var; $ivar = (integer) $var; $ivar = intval($var); $bvar = (bool) $var; $bvar = (boolean) $var; $fvar = (float) $var; $fvar = floatval($var); $svar = (string) $var; $svar = stringval($var);

Intro to PHP – Page 41 of 43CSCI 2910 – Client/Server-Side Programming Examples of Type Conversion (continued) ValueCast to intCast to boolCast to stringCast to float null0false""0 true1 "1"1 false0 ""0 00false"0"0 3.83true"3.8"3.8 "0"0false"0"0 "10"10true"10"10 "6 feet"6true"6 feet"6 "foo"0true"foo"0

Intro to PHP – Page 42 of 43CSCI 2910 – Client/Server-Side Programming Type Conversion (continued) PHP can automatically convert types too. If a variable is used as if it were a different type, the PHP script engine assumes a type conversion is needed and does it for you. Examples: $var = "100" + 15; // var$ set to integer = 115 $var = "100" ; // var$ set to float = 115 $var = 15 + " bugs"; // var$ set to integer = 15 $var = 15. " bugs"; // var$ set to string = "15 bugs"

Intro to PHP – Page 43 of 43CSCI 2910 – Client/Server-Side Programming In-Class Exercise Identify the errors in the following PHP script. <?php strvar1 = " Integer Squares from 0 to 9 " prints strvar prints " " for(i = 0; i < 9; i+) { isquared = i * i prints " Square root of " + i + " is " + isquared + " " } prints " " ?>