DAY 4. Review: Strings Strings can be concatenated (joined together) using the. (period) operator: $fullName = “Thomas ”. “Bombach”; This works when outputting.

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.
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
Everyday Italian Giada De Laurentiis Harry Potter J K. Rowling Learning XML Erik T. Ray CSCI 305 Introduction to Database.
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.
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
Introduction to JavaScript for Python Programmers
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.
ALBERT WAVERING BOBBY SENG. Week Whatever: PHP  Announcements/questions/complaints.
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 Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
PHP == ‘ Hypertext Preprocessor ’ Open-source, server-side scripting language Used to generate dynamic web-pages PHP scripts reside between reserved PHP.
Web forms in PHP Forms Recap  Way of allowing user interaction  Allows users to input data that can then be processed by a program / stored in a back-end.
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.
(c) Manzur Ashraf, Short course, KFUPM PHP & MySQL 1 Basic PHP Class 2.
INTERNET APPLICATION DEVELOPMENT For More visit:
Lecture 6 – Form processing (Part 1) SFDV3011 – Advanced Web Development 1.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators.
Lists in Python.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Includes and Dates.
Nael Alian Introduction to PHP
Week 9 PHP Cookies and Session Introduction to JavaScript.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
IST 210: PHP BASICS IST 210: Organization of Data IST210 1.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
PHP - Basic Language Constructs CSCI 297 Scripting Languages - Day Two.
Website Development with PHP and MySQL Saving Data.
VB and C# Programming Basics. Overview Basic operations String processing Date processing Control structures Functions and subroutines.
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.
PHP. 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.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
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 - 1h. How it works Client requests document Server loads document in memory Server processes document with relevant module (PHP) Server sends XHTML.
Introduction to PHP.
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.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
CHAPTER 8 PHP Advanced อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
CST336, Dr. Krzysztof Pietroszek Week 2: PHP. 1.Introduction to PHP 2.Embed PHP code into an HTML web page 3.Generate (output HTML) web page using PHP.
Fall 2004CSI University of Ottawa Introduction to PHP Basic principles and syntax.
8 th Semester, Batch 2009 Department Of Computer Science SSUET.
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,
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
11 – Introduction to PHP(1) Informatics Department Parahyangan Catholic University.
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.
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.
Radoslav Georgiev Telerik Corporation
PHP using MySQL Database for Web Development (part II)
Session 2 Basics of PHP.
19.10 Using Cookies A cookie is a piece of information that’s stored by a server in a text file on a client’s computer to maintain information about.
PHP Introduction.
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
Basics.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
PHP: Hypertext Preprocessor
PHP an introduction.
CIS 136 Building Mobile Apps
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Intro to Programming (in JavaScript)
Presentation transcript:

DAY 4

Review: Strings Strings can be concatenated (joined together) using the. (period) operator: $fullName = “Thomas ”. “Bombach”; This works when outputting text as well: echo “Camp ”. “CAEN”; // Output: Camp CAEN  String variables can also be concatenated: $first = “PHP ”; $second = “rocks”; echo $first. $second; // Output: PHP rocks

Review: String Functions  PHP has many functions that operate on strings  Usually we store the output of the function in a variable (since the function doesn’t change the original value) $newVariable = functionName($stringVariable);

Review: String Functions  String length: strlen($stringVariable)  Counts the number of characters (including spaces) in a string $name = “Thomas”; $nameLength = strlen($name); // $nameLength is equal to 6 // $name is unchanged

Review: String Functions (continued)  String position: strpos($searchWithin, $searchFor)  Finds the first occurrence of a string within a string  Returns the index (position) of the string (like arrays, indexing starts at 0 – the first character in a string is at position 0) $game = “Counterstrike”; $strikePos = strpos($game, “strike”); // $strikePos == 7 $counterPos = strpos($game, “Counter”); // $counterPos == 0

Review: String Functions (continued)  Sub strings: substr($string, $start, $length)  Returns a part of the string. $start is the position in the string to start from, and $length is the number of characters to use $word = “hyperbole”; $partial = substr($word, 0, 5); // $partial == “hyper”  If the $length parameter is left out, the sub string goes to the end of the original string $word = “antithesis”; $partial = substr($word, 4); // $partial == “thesis”

Review: String Functions (continued)  Replacing parts of a string: str_replace($find, $replace, $string, $count)  $find is the string to replace  $replace is the string to use wherever the $find string is found  $string is the string to be searched  $count is an optional variable that counts the number of replacements $word = “metonymy”; $newString = str_replace(“tony”, “bill”, $word); // $newString == “mebillmy”

Review: Programming in PHP  Operator-Assignment Shorthand: += -=/=*=.=%=  Example 1: $myName = “Thomas”; $myName.= “ Bombach”; // $myName == “Thomas Bombach”  Example 2: $number = 6; $number += 5; // $number == 11

Review: Date  Date – utility in PHP to get the date and time  Getting the current date: date($format) function $format is a string that contains information on what parts of the date or time to display, based on certain characters echo date(“h:m:s m/d/Y”); // Outputs 01:23:45 07/08/2009 d - The day of the month (from 01 to 31) m - A numeric representation of a month (from 01 to 12) Y - A four digit representation of a year G - 24-hour format of an hour (0 to 23) h - 12-hour format of an hour (01 to 12) i - Minutes with leading zeros (00 to 59) s - Seconds, with leading zeros (00 to 59)

PHP Arrays  2 types of arrays  Numeric Conventional arrays, with the keys being numbers: $myArray = array(“First”, “Second”, “Third”); /* $myArray[0] == “First”; $myArray[1] == “Second”; $myArray[2] == “Third”; */  Associative Keys are not numbers, but strings $myArray = array(“first” => “Thomas”, “last” => “Bombach”); /* $myArray[“first”] == “Thomas”; $myArray[“last”] == “Bombach”; */

Array Functions  Similar to strings, there are functions that operate on arrays  count($array)  Returns the number of entries in an array  Works on both numeric and associative arrays  Example: $myArray = array(“first”, “second”, “third”); echo count($myArray); // Outputs 3

Numeric Arrays  Created with array() syntax  Passing in a series of values creates indexes starting from 0, increasing by 1  Example: $myArray = array(5, 6, 7); // Creates an array of length 3 /* myArray[0] == 5 myArray[1] == 6 myArray[2] == 7 */

Numeric Arrays (continued)  Indexes (positions in the array) can also be specifically chosen by passing in key/value pairs  Key/value pairs are in the form key => value  Example: $myArray = array(5 => “a”, 6 => “b”, 19 => “c”); /* $myArray[5] == “a” $myArray[6] == “b” $myArray[19] == “c” */

Numeric Arrays (continued)  Accessing numeric arrays  Same as C++  Example: $myArray = array(“first”, “second”, “third”, 42=> “Answer to the Ultimate Question of Life, the Universe, and Everything”); echo myArray[0]; // Outputs: first echo myArray[1]; // Outputs: second echo myArray[2]; // Outputs: third echo myArray[42]; // Outputs: Answer to the Ultimate Question of Life, the Universe and Everything myArray[3] = “fourth”;

Looping Over Numeric Arrays  Using a for loop  Same as C++  Example: $myArray = array(0, 1, 1, 2, 3, 5, 8); for($i = 0; $i < 7; $i++) { echo myArray[$i]. “ ”; } /* Outputs: etc. */

Associative Arrays  Uses strings instead of numbers as keys in the array  Example: $employee = array(“title” => “Programmer”, “salaryPerYear” => 80000, “expendable” => true); /* $employee[“title”] == “Programmer” $employee[“salaryPerYear”] == $employee[“expendable”] == true */

Associative Arrays (continued)  Accessing associative arrays  Similar to numeric arrays, use keys  Example: $players = array(“qb” => “Rick Leach”, “wr” => “Anthony Carter”, “rb” => “Tom Harmon”); echo $players[“qb”] // Outputs: Rick Leach echo $players[“wr”] // Outputs: Anthony Carter echo $players[“rb”] // Outputs: Tom Harmon

Looping over Associative Arrays  Use the foreach syntax  Different from C++  Example: $players = array(“qb” => “Rick Leach”, “wr” => “Anthony Carter”, “rb” => “Tom Harmon”); foreach ($players as $player) { echo $player. “ ”; } /* Output: Rick Leach Anthony Carter Tom Harmon */

Project: Employee Card  In an attempt to find a web-developer job, you’ve decided to put some information about yourself online for employers to find. You decide to make an associative array with all your information, then loop through and print it out, nicely formatted (use CSS & HTML!).  You should include your name, location, skill set, whether you want a full-time or part time position, and any other relevant information  The card should look like this: Name: Thomas BombachLocation: Ann Arbor Skills: Knowledgeable in C++, Java, HTML & XHTML, Javascript (including AJAX), CSS, PHP, MySQL Engineering background Web design experience (using Illustrator, Photoshop, etc.) Desired position: Full-time

foreach (continued)  Sometimes you need to use the key name from an associative array  Use the other type of foreach loop: $myArray = array(“idempotent” => “(Adj.) Multiple applications of the operation do not change the result”); foreach($myArray as $key => $value) { echo $key. “: ”. $value. “ ”; } /* Output: idempotent: (Adj.) Multiple applications of the operation do not change the result */

Case & Switch  Used instead of if statements to reduce the amount of code  Syntax: switch($variable) { case value1: break; case value2: break; default: }

If/else if/else vs. Case & Switch $age = //some number if($age == 65) { /* Run code (flag them as eligible for Medicare) */ } else if($age == 18) { /* Run code (flag them as eligible for the draft) */ } else { // Don’t do anything } $age = //some number switch ($age) { case 65: /* Run code (flag them as eligible for Medicare) */ break; case 18: /* Run code (flag them as eligible for the draft) */ break; default: // Don’t do anything } If/ else if/elseCase & Switch

Functions  Similar syntax as Javascript: function functionName() { // Code goes here }  To call a function, you simply use the function name, followed by parenthesis: functionName(); // Calls a function called functionName

Functions (continued)  Functions can return variables too whomever called the function  Example: $myName = getName(); function getName() { return “Thomas”; }

Functions - Arguments  Arguments (or parameters)  Functions can use variables that are given to them (called arguments). The number of arguments passed to a function must match the number that the function expects  Example: foo(95, “My number: ”); function foo($bar, $desc) { echo $desc. $bar; } /* Output: My number: 95 */

Project: Mad Libs  Using associative arrays & functions, make a Mad Libs program  Have different functions for each sentence  Each function should receive only 1 argument, an associative array with keys for nouns, adjectives, etc.  Example: function printSentence($words) { // Use $words[“noun”], $words[“verb”], $words[“noun2”], etc. }

Forms & PHP  HTML Form Tag  action is a path to a PHP file that handles the form submission from the user  method specifies how to send the form submission POST GET

POST vs. GET  Will not be cached by the browser  Use this method when the request will change the state of data (such as posting a comment on a blog)  Will be cached by the browser  Use this method when the request will not change the data on the server (such as a search) POSTGET