Lecture 2: Intro to PHP What is PHP?

Slides:



Advertisements
Similar presentations
PHP I.
Advertisements

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
Fundamentals of Python: From First Programs Through Data Structures
Overview of JSP Technology. The need of JSP With servlets, it is easy to – Read form data – Read HTTP request headers – Set HTTP status codes and response.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft.
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 405: Web Application And Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming.
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators.
Lecture Note 3: ASP Syntax.  ASP Syntax  ASP Syntax ASP Code is Browser-Independent. You cannot view the ASP source code by selecting "View source"
AIT 616 Fall 2002 PHP. AIT 616 Fall 2002 PHP  Special scripting language used to dynamically generate web documents  Open source – Free!!!  Performs.
Nael Alian Introduction to PHP
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.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
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.
Introduction to PHP Advanced Database System Lab no.1.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
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.
CSC 405: Web Application Engineering II 2.1 Web Programming with PHP Introduction to Web programming Introduction to Web programming The programming language.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 2: Introduction to IS2803 Rob Gleasure
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.
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,
IST 210: PHP Basics IST 210: Organization of Data IST2101.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
PHP using MySQL Database for Web Development (part II)
COSC405 Web Application Engineering II Slides adopted from here
Web Database Programming Using PHP
PHP (Session 1) INFO 257 Supplement.
Introduction to Dynamic Web Programming
Chapter 6 JavaScript: Introduction to Scripting
Chapter 5 Scripting Language
CHAPTER 5 SERVER SIDE SCRIPTING
Web Database Programming Using PHP
Java Primer 1: Types, Classes and Operators
DBW - PHP DBW2017.
Arrays in PHP are quite versatile
PHP (PHP: Hypertext Preprocessor)
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
JavaScript Syntax and Semantics
Chapter 5 Scripting Language
ITM 352 Expressions, Precedence, Working with Strings Class #5
PHP / MySQL Introduction
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
PHP Introduction.
Intro to PHP & Variables
MATLAB: Structures and File I/O
PHP.
Web DB Programming: PHP
Python Primer 1: Types and Operators
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Lecture 5: Functions and Parameters
Intro to PHP.
JavaScript CS 4640 Programming Languages for Web Applications
PHP: Hypertext Preprocessor
PHP an introduction.
Lecture 6: Processing Forms with PHP
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

Lecture 2: Intro to PHP What is PHP? Language developed by Rasmus Lerdorf from the Apache Group Its primary use is for server-side scripting Ex: To process HTML forms Ex: To perform a DB query and pass on results Ex: To dynamically generate HTML PHP scripts are often embedded within HTML documents The server processes the HTML document, executing the PHP segments and substituting the output within the HTML document

The modified document is then sent to the client Lecture 2: Intro to PHP The modified document is then sent to the client As mentioned previously, the client never sees the PHP code The only reason the client even knows PHP is involved is due to the file extension  .php But even this is not required if the server is configured correctly The server can be configured to run PHP scripts even if the file does not have a .php extension By default XAMPP will only execute PHP if the .php extension is provided See what happens if you have PHP code without the .php extension

PHP is a HUGE language It is a fully functional language Lecture 2: Intro to PHP PHP is a HUGE language It is a fully functional language It has an incredible amount of built-in features Form processing Output / generate various types of data (not just text) Database access Allows for various DBs and DB formats Object-oriented features Somewhat of a loose hybrid of C++ and Java Huge function / class library

We will look at only a small part of PHP Lecture 2: Intro to PHP We will look at only a small part of PHP There are also many tools that are already pre-written in / for PHP If you are building a substantial project you may want to use some of these Ex: http://github.com/pear Also see http://www.php.net/sites.php There are also content management systems written in PHP Ex: http://drupal.org/ Ex: http://wordpress.org/ However, we may not be covering them here We will focus on the straight PHP language

PHP Program Structure Or really lack thereof Lecture 2: Intro. to PHP PHP Program Structure Or really lack thereof PHP, as with many scripting languages, does not have nearly the same structural requirements as a language like Java A script can be just a few lines of code or a very large, structured program with classes and objects The complexity depends on the task at hand However, there are some guidelines for incorporating PHP scripts into HTML files

Lecture 2: Intro. to PHP When a PHP file is requested, the PHP interpreter parses the entire file Any content within PHP delimiter tags is interpreted, and the output substituted Any other content (i.e. not within PHP delimiter tags) is simply passed on unchanged This allows us to easily mix PHP and other content (ex: HTML) See: http://us3.php.net/manual/en/language.basic-syntax.phptags.php http://us3.php.net/manual/en/language.basic-syntax.phpmode.php

Consider the following PHP file Lecture 2: Intro to PHP Consider the following PHP file <!DOCTYPE html> <html> <head> <title>Simple PHP Example</title> </head> <body> <?php echo "<p><h1>Output</h1>"; echo "<h2>Output</h2>"; echo "<h3>Output</h3></p>"; ?> <script language="PHP"> echo "\n<b>More PHP Output</b>\n"; echo "New line in source but not rendered"; echo "<br/>"; echo "New line rendered but not in source"; </script> </body> </html> HTML 5 Document Root HTML Tag Document Head D O C B Y PHP Code

Now consider the resulting HTML Lecture 2: Intro. to PHP Now consider the resulting HTML <!DOCTYPE html> <html> <head> <title>Simple PHP Example</title> </head> <body> <p><h1>Output</h1><h2>Output</h2><h3>Output</h3></p> <b>More PHP Output</b> New line in source but not rendered<br/>New line rendered but not in source </body> </html> How will it look in the browser? Look at it in the browser! See ex2.php

Lecture 2: Intro to PHP If we prefer to separate a PHP code segment from the rest of our script, we can write it in another file and include it Sometimes it is easier if we "separate" the PHP code from the straight html We also may be using several different files, esp. if we are using classes Remember to tag it even if it is already within a PHP tagged segment Included files are not interpreted by default Don’t necessarily have to be PHP If we want PHP, include PHP tags within the included file See ex3.php See http://us3.php.net/manual/en/function.include.php

Simple types boolean integer float Lecture 2: Intro to PHP Simple types See: http://us3.php.net/manual/en/language.types.php boolean TRUE or FALSE integer Platform dependent – size of one machine word typically 32 or 64 bits float Double precision We could call it a double, but since we don't declare variables (we will discuss shortly) float works

string We have single-quoted and double-quoted string literals Lecture 2: Intro to PHP string We have single-quoted and double-quoted string literals Double quoted allows for more escape sequences and allows variables to be interpolated into the string What does that mean? Rather than outputting the name of the variable, we output its contents, even within a quote We'll see an example once we define variables Note that this is NOT done in Java See example Length can be arbitrary Grows as necessary

Easy conversion back and forth between strings and numbers Lecture 2: Intro to PHP Easy conversion back and forth between strings and numbers In Web applications these are mixed a lot, so PHP will implicitly cast between the types when appropriate This is another clear difference between PHP and Java Java requires explicit casting PHP allows explicit casting if desired See: http://us3.php.net/manual/en/language.types.type-juggling.php Can be indexed – the preferred way is using square brackets $mystring = "hello"; echo $mystring[1]; Output here is 'e'

PHP variables All PHP variables begin with the $ Lecture 2: Intro to PHP PHP variables All PHP variables begin with the $ Variable names can begin with an underscore Otherwise rules are similar to most other languages Variables are dynamically typed No type declarations Variables are BOUND or UNBOUND Unbound variables have the value NULL Type information for a variable is obtained from the current bound value Compare this to Java

Implications of dynamic typing: Lecture 2: Intro. to PHP Implications of dynamic typing: No “type clash” errors like in Java int x = 3.5; // oh no! String s = 100; // argh! Instead we have in PHP $x = 3.5; // no problem! $s = 100; // a-ok! A variable’s type may change throughout program execution $x = 5; // integer $x = $x + 1.5; // float $x = $x . “ dollars”; // string

Perhaps intentionally but perhaps by mistake Lecture 2: Intro. to PHP Perhaps intentionally but perhaps by mistake We have to be careful and to test types during execution gettype() function returns a string representation of variable’s type $x = 5; echo(gettype($x)); // integer $x = $x + 1.5; echo (gettype($x)); // float $x = $x . “ dollars”; echo(gettype($x)); // string is_<type>() function returns a boolean to test for a given <type> $x = 5; $check = is_int($x); // true $check = is_float($x); // false Can use these tests to make decisions within a script See ex4.php

PHP programs have access to a large number of predefined variables Lecture 2: Intro to PHP PHP programs have access to a large number of predefined variables These variables allow the script access to server information, form parameters, environment information, etc. Very helpful (as we will see) for determining and maintaining state information Ex: $_SERVER is an array containing much information about the server $_POST is an array containing variables passed to a script via HTTP POST $_COOKIE is an array containing cookies See ex5.php

PHP Expressions and Operators Lecture 2: Intro to PHP PHP Expressions and Operators Similar to those in C++ / Java / Perl Be careful with a few operators / in PHP is always floating point division To get integer division, we must cast to int $x = 15; $y = 6; echo ($x/$y), (int) ($x/$y), "<BR />"; Output is 2.5 2 Mixed operands can produce odd results Values may be cast before comparing

Even the == operator has odd behavior in PHP when operands are mixed Lecture 2: Intro to PHP To compare strings, it is better to use the C-like string comparison function, strcmp() Other string functions are listed in Sebesta Table 9.3 Even the == operator has odd behavior in PHP when operands are mixed Ex: Consider comparing a string and an int Any non-numeric PHP string value will “equal” 0 Any numeric PHP string will equal the number it represents Ex: Consider comparing a string and a boolean Regular PHP string value will “equal” true “0” string will equal false This behavior is consistent but confusing to the programmer and is probably best avoided

Precedence and associativity are similar to C++/Java Lecture 2: Intro to PHP An additional equality operator and inequality operator are defined === returns true only if the variables have the same value and are of the same type If casting occurred to compare, the result is false !== returns true if the operands differ in value or in type Precedence and associativity are similar to C++/Java See http://us2.php.net/manual/en/language.operators.precedence.php

PHP Control Structures Lecture 3: Intro to PHP PHP Control Structures Again, these are similar to those in C++ / Java if, while, do, for, switch are virtually identical to those in C++ and Java PHP allows for an alternative syntax to designate a block in the if, while, for and switch statements Open the block with : rather than { Close the block with endif, endwhile, endfor, endswitch Advantage to this syntax is readability Now instead of seeing a number of close braces, we see different keywords to close different types of control structures

PHP also has the foreach loop See ex6.php Lecture 3: Intro to PHP A nice feature of PHP is that the "control" resulting from a control structure is maintained even when you exit back to html mode Thus, in <?php you can branch / loop etc. You can then exit php ?> and format in straight html PHP also has the foreach loop We will look at this when we discuss arrays See ex6.php