Lecture 12 PHP Basics Boriana Koleva Room: C54

Slides:



Advertisements
Similar presentations
Chapter 25 Perl and CGI (Common Gateway Interface)
Advertisements

PHP I.
Chapter 12 Introduction to PHP. © 2006 Pearson Addison-Wesley. All rights reserved Origins and Uses of PHP - Origins - Rasmus Lerdorf
IST 221 Internet Concepts and Applications Introduction to PHP.
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  Originally Personal Home Page  PHP is interpreted  PHP code is embedded.
Guide To UNIX Using Linux Third Edition
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
Server side basics.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
PHP: Introduction By Trevor Adams.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11 Introduction to PHP.
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.
PHP: Hypertext Processor Fred Durao
1 Ivan Marsic Rutgers University LECTURE: PHP Scripting Language.
PHP1-1 PHP Xingquan (Hill) Zhu
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
UFCEKG-20-2 Data, Schemas & Applications Lecture 4 Server Side Scripting & PHP.
Chapter 11 Introduction to PHP. It is a Web development language written by and for Web developers PHP stands for PHP: Hypertext Preprocessor Originally.
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 MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.
Introduction to PHP and Server Side Technology. Slide 2 PHP History Created in 1995 PHP 5.0 is the current version It’s been around since 2004.
Chapter 9 © 2010 by Addison Wesley Longman, Inc Origins and Uses of PHP - Origins - Rasmus Lerdorf Developed to allow him to track visitors.
Copyright © Curt Hill PhP History and Introduction.
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.
School of Computing and Information Systems CS 371 Web Application Programming PHP - Basics Serving up web pages.
CP476 Internet Computing JavaScript Client-Side Programming 1 1. What is JavaScript –Another script language for both client-side and sever-side programming.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
November 2003Bent Thomsen - FIT 6-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet.
PHP By Sergio Rodriguez By Sergio Rodriguez. PHP G PHP: Hypertext Preprocessor G Scripting language G PHP: Hypertext Preprocessor G Scripting language.
Lecture 9 The Basics of JavaScript Boriana Koleva Room: C54
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.
Web Database Programming Week 3 PHP (2). Functions Group related statements together to serve “a” specific purpose –Avoid duplicated code –Easy maintenance.
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
CS320 Web and Internet Programming Introduction to PHP Chengyu Sun California State University, Los Angeles.
CS 174: Web Programming September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Chapter 12 © 2003 by Addison Wesley Longman, Inc Origins and Uses of PHP - Origins - Rasmus Lerdorf Developed to allow him to track visitors.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
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.
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.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Chapter 9 © 2012 by Addison Wesley Longman, Inc Origins and Uses of PHP - Origins - Rasmus Lerdorf Developed to allow him to track visitors.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
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. What is PHP? PHP Hypertext Processor – Dynamic web development – Scripting language – Can be procedural or OOP(preferred) – PHP code can be embedded.
By bscshelp.com 1.  It is a group assignment.  Complete Website design Using Html and Css.  Due date: 10 th December, 2014 (Hard Deadline) 2.
CHAPTER 9 INTRODUCTION TO PHP 1. ORIGINS AND USES OF PHP Origins –Rasmus Lerdorf – 1994 –Developed to allow him to track visitors to his Web site PHP.
PHP using MySQL Database for Web Development (part II)
12.1 Origins and Uses of PHP 12.2 Overview of PHP - Origins
CHAPTER 9 INTRODUCTION TO PHP
Session 2 Basics of PHP.
CS 371 Web Application Programming
9.1 Origins and Uses of PHP 9.2 Overview of PHP - Origins
DBW - PHP DBW2017.
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
PHP.
Web DB Programming: PHP
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Lecture 5: Functions and Parameters
Chapter 12 Introduction to PHP.
9.1 Origins and Uses of PHP 9.2 Overview of PHP - Origins
PHP an introduction.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

Lecture 12 PHP Basics Boriana Koleva Room: C54

Overview Overview of PHP Syntactic Characteristics Primitives Output Control statements Arrays Functions PHP on CS servers

Origins and uses of PHP Origins - Rasmus Lerdorf Developed to allow him to track visitors to his Web site PHP is an open-source product PHP is an acronym for Personal Home Page, or PHP: Hypertext Preprocessor PHP is used for form handling, file processing, and database access

PHP Overview PHP is a server-side scripting language whose scripts are embedded in HTML documents Similar to JavaScript, but on the server side PHP is an alternative to CGI, ASP.NET and Java servlets The PHP processor has two modes: copy (HTML) and interpret (PHP) PHP syntax is similar to that of JavaScript PHP is dynamically typed PHP is purely interpreted

Syntactic Characteristics PHP code can be specified in an HTML document internally or externally: Internally: Externally: include ("myScript.inc") the file can have both PHP and HTML If the file has PHP, the PHP must be in, even if the include is already in

Syntactic Characteristics 2 Every variable name begins with a $ Case sensitive Comments - three different kinds (Java and Perl) //... #... /*... */ PHP statements terminated with ; Compound statements are formed with braces Compound statements cannot define locally scoped variables (except functions)

Reserved words of PHP These are not case sensitive!

Variables There are no type declarations An unassigned (unbound) variable has the value NULL The unset function sets a variable to NULL The IsSet function is used to determine whether a variable is NULL error_reporting(15); the interpreter will report when an unbound variable is referenced

Primitives Four scalar types: Boolean, integer, double, and string Two compound types: array and object Two special types: resource and NULL Integer & double are like those of other languages Boolean - values are true and false (case insensitive)

Strings Characters are single bytes String literals use single or double quotes Single-quoted string literals Embedded variables are NOT interpolated and embedded escape sequences are NOT recognized Double-quoted string literals Embedded variables ARE interpolated and embedded escape sequences ARE recognized

Predefined functions Arithmetic functions floor, ceil, round, abs, min, max, rand, etc. String functions strlen, strcmp, strpos, substr chop – remove whitespace from the right end trim – remove whitespace from both ends ltrim – remove whitespace from the left en strtolower, strtoupper

Scalar type conversions Implicit type conversion – coercion String to numeric If the string contains an e or an E, it is converted to double; otherwise to integer If the string does not begin with a sign or a digit, zero is used Explicit conversions – casts e.g., (int)$total or intval($total) or settype($total, "integer") The type of a variable can be determined with gettype or is_ type function e.g.: gettype($total) may return " unknown " is_integer($total) returns Boolean value

Output Output from a PHP script is HTML that is sent to the browser HTML is sent to the browser through standard output There are three ways to produce output: echo, print, and printf echo and print take a string, but will coerce other values to strings echo “Hello there!"; echo(“Hello there!”); echo $sum; print "Welcome!"; print(“Wellcome”); print (46); (too see actual PHP script)

Control statements Selection if, else, else if switch The switch expression type must be integer, double, or string Loops while, do-while, for (stored as.pdf file to show PHP script)

Arrays Not like the arrays of any other programming language A PHP array is a generalization of the arrays of other languages A PHP array is really a mapping of keys to values, where the keys can be numbers (to get a traditional array) or strings (to get a hash)

Array creation Use the array() construct, which takes one or more key => value pairs as parameters and returns an array of them The keys are non-negative integer literals or string literals The values can be anything $list1 = array(); $list2 = array (17, 24, 45, 90); $list3 = array(0 => "apples", 1 => "oranges", 2 => "grapes") $list4 = array(“Joe” => 42, “Mary” => 41, “Jan” => 17);

Accessing array elements Individual array elements can be accessed through subscripting $list[4] = 7; $list["day"] = "Tuesday"; $list[] = 17; If an element with the specified key does not exist, it is created If the array does not exist, the array is created The keys or values can be extracted from an array $highs = array("Mon" => 74, "Tue" => 70, "Wed" => 67, "Thu" => 62, "Fri" => 65); $days = array_keys($highs); $temps = array_values($highs);

Dealing with arrays An array can be deleted with unset unset($list); unset($list[4]); # No index 4 element now is_array($list) returns true if $list is an array in_array(17, $list) returns true if 17 is an element of $list explode(" ", $str) creates an array with the values of the words from $str, split on a space implode(" ", $list) creates a string of the elements from $list, separated by a space

Sequential access to array elements next and prev functions $citites array(“London”, “Paris”, “Chicago”); $city = next($cities) foreach statement – to build loops that process all of the elements in an array foreach ( array as scalar_variable ) loop body E.g. foreach ($list as $temp) print (“$temp ”); foreach ( array as key => value ) loop body E.g. foreach ($lows as $day=>$temp) print(“The low temperature on $day was $temp ”);

Sorting arrays sort - to sort the values of an array, leaving the keys in their present order - intended for traditional arrays e.g., sort($list); Works for both strings and numbers, even mixed strings and numbers $list = ('h', 100, 'c', 20, 'a'); sort($list); // Produces ('a', 'c', 'h‘, 20, 100) asort - to sort the values of an array, but keeping the key/value relationships - intended for hashes ksort – to sort given array by keys rather than values

Functions function function_name([formal_parameters]) { … } Functions need not be defined before they are called Function overloading is not supported If you try to redefine a function, it is an error Function names are NOT case sensitive The return statement is used to return a value If there is no return, there is no returned value

Function parameters If the caller sends too many actual parameters, the function ignores the extra ones If the caller does not send enough parameters, the unmatched formal parameters are unbound The default parameter passing method is pass by value (one-way communication) To specify pass-by-reference, prepend an ampersand to the formal parameter function addOne(&$param) { $param++; } $it = 16; addOne($it); // $it is now 17

Scope and Lifetime of Variables Variables defined in a function have local scope To access a non-local variable in a function, it must be declared to be global (within the function code) global $sum ; Normally, the lifetime of a variable in a function is from its first appearance to the end of the function’s execution To support history sensitivity a function must have static local variables static $sum = 0; Its lifetime ends when the browser leaves the document in which the PHP script is embedded

PHP on CS servers Put files in public_html directory of your Linux server HTML files with embedded php script need to have.php extension You should then be able to access such a file from a web browser with the url: Eg: PHP scripts are executed under your own user account so generally they do not need to be globally readable It is important you ensure they are not if they contain database connection passwords or similar info

Summary Overview of PHP Syntactic Characteristics Primitives Output Control statements Arrays Functions PHP on CS servers