CSE 154 LECTURE 6: 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
» PHP arrays are lists of values stored in key-value pairs. » Uses of arrays: Many built-in PHP environment variables. Database functions use arrays.
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.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
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.
PHP Introduction.
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.
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.
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 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.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
CMPT241 Web Programming Intro to PHP.
CSE 341 Lecture 24 JavaScript arrays and objects slides created by Marty Stepp
More on PHP: Arrays, Functions and Files
Lecture 6 – Form processing (Part 1) SFDV3011 – Advanced Web Development 1.
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Lecture 5 – Function and Array SFDV3011 – Advanced Web Development 1.
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.
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.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
PHP Constructs Advance Database Management Systems Lab no.3.
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.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
Strings, output, quotes and comments
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.
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.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
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.
NMD202 Web Scripting Week2. Web site
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
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.
CSE 154 LECTURE 15: EMBEDDED PHP. PHP syntax template HTML content HTML content HTML content... PHP any contents of.
PHP. What is PHP? PHP Hypertext Processor – Dynamic web development – Scripting language – Can be procedural or OOP(preferred) – PHP code can be embedded.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
PHP using MySQL Database for Web Development (part II)
String and Lists Dr. José M. Reyes Álamo.
PHP for Server-Side Programming
JavaScript.
JavaScript an introduction.
Web Systems Development (CSC-215)
More Selections BIS1523 – Lecture 9.
PHP.
String and Lists Dr. José M. Reyes Álamo.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
JavaScript CS 4640 Programming Languages for Web Applications
CMPT241 Web Programming Intro to PHP.
CSE 154 Lecture 15: MORE PHP.
Presentation transcript:

CSE 154 LECTURE 6: 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

String type $favorite_food = "Ethiopian"; print $favorite_food[2]; # h PHP zero-based indexing using bracket notation string concatenation operator is. (period), not "2 turtle doves" produces 7 5. "2 turtle doves" produces "52 turtle doves“ can be specified with "" or ' '

String functions # index $name = “Austin Weale"; $length = strlen($name); # 16 $cmp = strcmp($name, “Linda Guo"); # > 0 $index = strpos($name, “s"); # 2 $first = substr($name, 7, 4); # “Weal" $name = strtoupper($name); # “AUSTIN WEALE“ PHP NameJava Equivalent strlenlength strposindexOf substrsubstring strtolowerstrtolower, strtoupperstrtouppertoLowerCase, toUpperCase trim explodeexplode, implodeimplodesplit, join

Interpreted strings $age = 16; print "You are ". $age. " years old.\n"; print "You are $age years old.\n"; # You are 16 years old. PHP strings inside " " are interpreted variables that appear inside them will have their values inserted into the string strings inside ' ' are not interpreted: print 'You are $age years old.\n'; # You are $age years old.\n PHP if necessary to avoid ambiguity, can enclose variable in {}: print "Today is your $ageth birthday.\n"; # $ageth not found print "Today is your {$age}th birthday.\n"; PHP

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

bool (Boolean) type $feels_like_summer = FALSE; $php_is_rad = TRUE; $student_count = 217; $nonzero = (bool) $student_count; # TRUE PHP the following values are considered to be FALSE (all others are TRUE): 0 and 0.0 "", "0", and NULL (includes unset variables) arrays with 0 elements can cast to boolean using (bool) FALSE prints as an empty string (no output); TRUE prints as a 1

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