CSE 154 LECTURE 15: EMBEDDED PHP. PHP syntax template HTML content <?php PHP code ?> HTML content <?php PHP code ?> HTML content... PHP any contents of.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Intro to Javascript CS Client Side Scripting CS380 2.
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
Ruby (on Rails) CSE 190M, Spring 2009 Week 3. Web Programming in Ruby Ruby can be used to write dynamic web pages Similar to PHP, chunks of Ruby begins.
Arrays Strings and regular expressions Basic PHP Syntax CS380 1.
Objectives Using functions to organize PHP code
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
Server side basics CS URLs and web servers  Usually when you type a URL in your browser:  Your computer looks up the server's IP address using.
Server side basics.
Server side basics CS URLs and web servers  Usually when you type a URL in your browser:  Your computer looks up the server's IP address using.
CSE 341 Lecture 24 JavaScript arrays and objects slides created by Marty Stepp
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
More on PHP: Arrays, Functions and Files
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
CSE 154 LECTURE 6: EMBEDDED PHP. PHP syntax template HTML content HTML content HTML content... PHP any contents of.
CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad GM-IT CIIT Islamabad CIIT Virtual Campus, CIIT COMSATS Institute.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Lecture 5 – Function and Array SFDV3011 – Advanced Web Development 1.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
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.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
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.
Introduction to PHP – Part 2 Sudeshna Dey. Arrays A series of homogeneous elements Elements have values in form of stored data Has a key associated with.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
CSE 154 LECTURE 5: INTRO TO PHP. URLs and web servers usually when you type a URL in your browser: your computer looks up the.
Introduction to Programming JScript Six Scripting functions Discuss functions Password Example.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
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.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
NMD202 Web Scripting Week2. Web site
Arrays Strings and regular expressions Basic PHP Syntax CS380 1.
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,
CSE 154 LECTURE 14: INTRO TO PHP. URLs and web servers usually when you type a URL in your browser: your computer looks up the.
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.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
PHP. What is PHP? PHP Hypertext Processor – Dynamic web development – Scripting language – Can be procedural or OOP(preferred) – PHP code can be embedded.
CSE 154 LECTURE 16: FILE I/O; FUNCTIONS. Query strings and parameters URL?name=value&name=value...
PHP using MySQL Database for Web Development (part II)
Test 2 Review Outline.
PHP for Server-Side Programming
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript an introduction.
PHP.
Review for Final Exam.
JavaScript CS 4640 Programming Languages for Web Applications
PHP an introduction.
CSE 154 Lecture 15: MORE PHP.
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

CSE 154 LECTURE 15: EMBEDDED PHP

PHP syntax template HTML content <?php PHP code ?> HTML content <?php PHP code ?> HTML content... PHP any contents of a.php file between are executed as PHP code all other contents are output as pure HTML

Arrays $name = array(); # create $name = array(value0, value1,..., valueN); $name[index] # get element value $name[index] = value; # set element value $name[] = value; # append PHP $a = array(); # empty array (length 0) $a[0] = 23; # stores 23 at index 0 (length 1) $a2 = array("some", "strings", "in", "an", "array"); $a2[] = "Ooh!"; # add string to end (at index 5) PHP to append, use bracket notation without specifying an index element type is not specified; can mix types

Array functions function name(s)description countnumber of elements in the array print_rprint array's contents array_poparray_pop, array_push, array_shift, array_unshiftarray_push array_shiftarray_unshift using array as a stack/queue in_arrayin_array, array_search, array_reverse, sort, rsort, shufflearray_searcharray_reverse sortrsortshuffle searching and reordering array_fillarray_fill, array_merge, array_intersect, array_diff, array_slice, rangearray_mergearray_intersect array_diffarray_slicerange creating, filling, filtering array_sumarray_sum, array_product, array_unique, array_filter, array_reducearray_productarray_unique array_filterarray_reduce processing elements

Array function example $tas = array("MD", "BH", "KK", "HM", "JP"); for ($i = 0; $i < count($tas); $i++) { $tas[$i] = strtolower($tas[$i]); } # ("md", "bh", "kk", "hm", "jp") $morgan = array_shift($tas); # ("bh", "kk", "hm", "jp") array_pop($tas); # ("bh", "kk", "hm") array_push($tas, "ms"); # ("bh", "kk", "hm", "ms") array_reverse($tas); # ("ms", "hm", "kk", "bh") sort($tas); # ("bh", "hm", "kk", "ms") $best = array_slice($tas, 1, 2); # ("hm", "kk") the array in PHP replaces many other collections in Java list, stack, queue, set, map,...

The foreach loop foreach ($array as $variableName) {... } PHP $stooges = array("Larry", "Moe", "Curly", "Shemp"); for ($i = 0; $i < count($stooges); $i++) { print "Moe slaps {$stooges[$i]}\n"; } foreach ($stooges as $stooge) { print "Moe slaps $stooge\n"; # even himself! } a convenient way to loop over each element of an array without indexes

Math operations $a = 3; $b = 4; $c = sqrt(pow($a, 2) + pow($b, 2)); PHP absceilcosfloorloglog10max minpowrandroundsinsqrttan math functions M_PIM_EM_LN2 math constants the syntax for method calls, parameters, returns is the same as Java

NULL $name = "Victoria"; $name = NULL; if (isset($name)) { print "This line isn't going to be reached.\n"; } a variable is NULL if it has not been set to any value (undefined variables) it has been assigned the constant NULL it has been deleted using the unset function can test if a variable is NULL using the isset function NULL prints as an empty string (no output)

Printing HTML tags in PHP = bad style <?php print " \n"; print " Geneva's web page \n";... for ($i = 1; $i <= 10; $i++) { print " I can count to $i! \n"; } ?> PHP printing HTML tags with print statements is bad style and error-prone: must quote the HTML and escape special characters, e.g. \" but without print, how do we insert dynamic content into the page?

PHP expression blocks PHP The answer is PHP The answer is 42 output PHP expression block: evaluates and embeds an expression's value into HTML is equivalent to

Expression block example CSE 154: Embedded PHP = 1; $i--) { ?> bottles of beer on the wall, bottles of beer. Take one down, pass it around, bottles of beer on the wall. PHP

Common errors: unclosed braces, missing = sign Watch how high I can count: PHP and above are inside the for loop, which is never closed if you forget to close your braces, you'll see an error about 'unexpected $end‘ if you forget = in <?=, the expression does not produce any output

Complex expression blocks >This is a level heading. > PHP This is a level 1 heading. This is a level 2 heading. This is a level 3 heading. output expression blocks can even go inside HTML tags and attributes

Functions function name(parameterName,..., parameterName) { statements; } PHP function bmi($weight, $height) { $result = 703 * $weight / $height / $height; return $result; } PHP parameter types and return types are not written a function with no return statements is implicitly "void" can be declared in any PHP block, at start/end/middle of code

Calling functions name(expression,..., expression); PHP $w = 163; # pounds $h = 70; # inches $my_bmi = bmi($w, $h); PHP if the wrong number of parameters are passed, it's an error

Variable scope: global and local vars $school = "UW"; # global... function downgrade() { global $school; $suffix = "(Wisconsin)"; # local $school = "$school $suffix"; print "$school\n"; } PHP variables declared in a function are local to that function; others are global if a function wants to use a global variable, it must have a global statement but don't abuse this; mostly you should use parameters

Default parameter values function name(parameterName = value,..., parameterName = value) { statements; } PHP function print_separated($str, $separator = ", ") { if (strlen($str) > 0) { print $str[0]; for ($i = 1; $i < strlen($str); $i++) { print $separator. $str[$i]; } } PHP print_separated("hello"); # h, e, l, l, o print_separated("hello", "-"); # h-e-l-l-o PHP if no value is passed, the default will be used (defaults must come last)