PHP (Session 1) INFO 257 Supplement.

Slides:



Advertisements
Similar presentations
PHP I.
Advertisements

Powerpoint Templates Page 1 Powerpoint Templates Server Side Scripting PHP.
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
CHAPTER 3 MORE ON FORM HANDLING INCLUDING MULTIPLE FILES WRITING FUNCTIONS.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
PHP (2) – Functions, Arrays, Databases, and sessions.
PHP Intro/Overview Squirrel Book pages Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a.
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
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.
Lecture 6 – Form processing (Part 1) SFDV3011 – Advanced Web Development 1.
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
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.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
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.
1 CS428 Web Engineering Lecture 20 Control Structures, Loops and Pointers File Uploading Function (PHP - III)
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
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.
PHP A very brief introduction PHP1. PHP = PHP: Hypertext Processor A recursive acronym! PHP scripts are executed on the server side Executed by (a plugin.
Creating Databases for Web applications Server side vs client side PHP basics Homework: Get your own versions of sending working: both html and Flash!
Since you’ll need a place for the user to enter a search query. Every form must have these basic components: – The submission type defined with the method.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
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
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 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.
PHP WORKSHOP (Session 1) INFO 257 Supplement. Outline What is PHP ? PHP Language Basics PHP Exercise.
Radoslav Georgiev Telerik Corporation
Simple PHP Web Applications Server Environment
PHP using MySQL Database for Web Development (part II)
Web Database Programming Using PHP
PHP (Session 2) INFO 257 Supplement.
CGS 3066: Web Programming and Design Spring 2017
Introduction to Dynamic Web Programming
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Web Database Programming Using PHP
DBW - PHP DBW2017.
PHP Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used repeatedly in.
Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
PHP Introduction.
Intro to PHP & Variables
Web Systems Development (CSC-215)
More Selections BIS1523 – Lecture 9.
Software Engineering for Internet Applications
PHP Intro/Overview Bird Book pages 1-11,
PHP.
Web DB Programming: PHP
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Lecture 5: Functions and Parameters
Web Programming Language
JavaScript CS 4640 Programming Languages for Web Applications
PHP an introduction.
PHP-II.
Web Programming and Design
PHP By Prof. B.A.Khivsara Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Intro to Programming (in JavaScript)
Presentation transcript:

PHP (Session 1) INFO 257 Supplement

Outline What is PHP PHP Language Basics Breadcrumb Example

Big Picture 2 Process Request 1 HTTP Request 3 HTTP Response Database Web Server 4 Render HTML/CSS/JS Response in Browser Build DOM Apply CSS Execute JS

The Request “Traditional Directory Mapping” http://www.berkeley.edu/academics/calendar.php Root academics calendar.php Note, we will not be covering MVC

HTML vs. PHP Requests DEMO http://www.berkeley.edu/academics/index.html Server will simply return the HTML file http://www.berkeley.edu/academics/index.php Server will Parse the php file, Execute any php code within the file, Then return a dynamically generated html file. DEMO

PHP Block <?php //PHP Code… ?> Can put this anywhere and any number of times within the html of your PHP file. The code in PHP Blocks merge together to form a single PHP execution script. So if you define a variable in an earlier block, it will be available in a latter block as well.

$variable_name = value; Variables $variable_name = value; Data Types String $a = “some string of characters”; Integer (whole #’s) $a = 1; Float (fractional #’s) $a = 1.1; Boolean $a = true; Arrays $a = array(1, 2, 3); Objects We may not have time to get to this, but lets see… Resources We won’t have time to get to this null $a = null; Special type that basically means “nothing”. Very common when working with databases

Functions Define the Function function my_function($param1, $param2){ //code to execute… //possibly “return” a value/object } Call the Function $a = 1; $b = 2; my_function($a, $b);

More about Functions Default Parameters function a($param1 = “defaultValue”){ } Reference Parameters function a(&$param1){ } A word about variable scope…

echo <?php echo “Some Text”; echo $variable_name; When you want to spit out some content into your html, use echo <?php echo “Some Text”; echo $variable_name; echo $someArray[1] . “ “ . $someArray[2]; ?> Note, there is also a “print” function that is very similar to echo but echo is more useful since it can print out multiple variables/objects with a single call

Super Globals There are predefined “Super Global” variables that are made available to you through the PHP runtime that you can use within your PHP code. Super Global Content $_SERVER Contains info about the web server environment such as Server Name, Request Method, Document Root, etc. $_GET Contains any GET variables in the URL query string $_POST Contains any POST variables submitted via a form post submission $_COOKIE Contains any HTTP Cookie Info $_FILES Contains information on POST file uploads $_SESSION Contains information about any variables registered in a session (if created) There are some other super globals but these are the main ones…

If… elseif… else if (conditional expression 1){ //code to execute if conditional expression 1 = true } elseif (conditional expression 2){ //code to execute if conditional expression 2 = true else{ //code to execute none of the conditional expressions are met The conditional expression needs to be of a boolean (true/false) type. If you provide a variable or function that is not of a boolean type, it will either try to convert it or it will give you an error. Common type conversions: 0 = false, “” = false, “0” = false, [] = false, null = false

Conditionals $a == $b $a === $b $a != $b $a <> $b $a !== $b Example Name Result $a == $b Equal TRUE if $a is equal to $b after type juggling. $a === $b Identical TRUE if $a is equal to $b, and they are of the same type. $a != $b Not equal TRUE if $a is not equal to $b after type juggling. $a <> $b $a !== $b Not identical TRUE if $a is not equal to $b, or they are not of the same type. $a < $b Less than TRUE if $a is strictly less than $b. $a > $b Greater than TRUE if $a is strictly greater than $b. $a <= $b Less than or equal to TRUE if $a is less than or equal to $b. $a >= $b Greater than or equal to TRUE if $a is greater than or equal to $b. If compare numbers to strings (i.e. 1 == “1”), then string is converted to number and then compared. Note, this does not happen when you do the “===“ or “!==“ comparison http://www.php.net/manual/en/language.operators.comparison.php

Logical Operators AND && OR || Examples ($a = 1, $b = 2, $c = 2) ($a > $b && $b == $c)  FALSE ($a < $b || $a == $c )  TRUE ($a == $c || $b == $c || somefunc($a))  TRUE

While Loops while (conditional expression){ //code to execute while conditional expression evaluates to true //commonly used to read row results from SQL query } do{ //code executes at least once, then continues to execute while conditional expression evaluates to true } while(conditional expression)

For Loops 1 2 1 2 4 for($i = 0; $i < 10; $i++){ //execute code //You can use $i in your code } 3 4 3 2 3 4 continue; Stop execution and jump to “4” break; Break out of loop Note, you can use “continue;” and “break” in other loops as well (like the “while” loop) 2 3 …

Demo Time DEMO

Working with Strings Escape Characters New line $a = “this is a new line \n”; Backspace $a = “C:\\Some\\Path\\To\\index.php”; Double Quotes $a = “<a href=\”http:\\\\www.google.com\”>Google</a>”; OR… $a = ‘<a href=“http:\\\\www.google.com”>Google</a>’; Concatenation echo “add” . “strings” . $a; $query = “SELECT *”; $query .= “FROM table1, table2”; $query .= “WHERE table1.id = table2.id;”; Useful functions ($string = “some random string”) ucwords($string)  “Some Random String” strtoupper($string)  “SOME RANDOM STRING” str_word_count($string)  3 strlen($string)  18 strpos($string, “random”)  5 (returns false if not found)

Working with Arrays Arrays can contain mixed data types $myarray = array(1, “string”, true); Associative Arrays (kind of like Dictionaries, except that the array maintains order) $myarray = array(“somekey” => 1, “anotherkey” => “value”) $myarray[“somekey”]  1 Note, can also access by index: $myarray[0]  1 Can add new arrays elements after array has been instantiated by $myarray[“someotherkey”] = “string”; Multidimensional Arrays $locations = array(); $locations[“sanfran”] = array(“lat” => 100, “long” => 100); $locations[“la”] = array(“lat” => 150, “long” => 150); $locations[“sanfran”][“lat”]  100

Traversing Arrays foreach ($array as $value){ //execute code for each value in the array ($value) } foreach ($array as $key => $value){ //execute code for each key/value in the array ($value, $key) There are MANY functions that operate on arrays, including sorting, merging, poping, pushing, shifting, splicing, etc. Check out php.net for a full list…

isset() vs. empty() isset() checks to see if a variable is “set,” meaning it is not null. Returns false if not set and true otherwise. empty() checks to see whether a variables value is empty, meaning whether or not it evaluates to false $a = 0; $b = “”; $c = null; $d = “string”; isset($a)  True empty($a)  True isset($b)  True empty($b)  True isset($c)  False empty($c)  True isset($d) True empty($d)  False These are used a lot to check whether form inputs sent empty values or whether database results contain null values…

<?php include(‘C:/Path/To/Your/nav.php’); ?> Index.php <?php include(‘C:/Path/To/Your/nav.php’); ?> Note, in your folder structure it is a good idea to create a specific folder that contains all your “include files” nav.php nav.php footer.php footer.php

Popular Uses of Include Including a set of PHP functions and/or classes Including any variables that are defined in other PHP files (like $document_root, $your_variable, etc.) Including HTML that appears on more than 1 of your pages (i.e. menu, header, footer, sidebars, widgets, etc.) Including objects that get instantiated in other PHP files (i.e. including a connection handle to your MySQL database)

Practical Example Building a Dynamic Breadcrumb Home > Academics > Courses > i257

Next Time PHP Classes Making SQL calls to your MySQL Database  Reading the results  Rendering HTML from the results Sending Data to the Server