Let's Look Back. We talked about how to create a form in HTML. Forms are one way to interact with users

Similar documents
What is PHP? [1] Figure 1 [1]

The PHP language. Teaching you everything about PHP? Not exactly Goal: teach you how to interact with a database via web

PHP Introduction. Some info on MySQL which we will cover in the next workshop...

CSC Web Programming. Introduction to JavaScript

PHP. Introduction. PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server

PHP 5 Introduction. What You Should Already Know. What is PHP? What is a PHP File? What Can PHP Do? Why PHP?

BEGINNER PHP Table of Contents

Ex: If you use a program to record sales, you will want to remember data:

Server side basics CS380

PHP 1. Introduction Temasek Polytechnic

Web Engineering (Lecture 08) WAMP

PHP and MySQL for Dynamic Web Sites. Intro Ed Crowley

CS 106 Introduction to Computer Science I

PHP by Pearson Education, Inc. All Rights Reserved.

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

Basics of Java Programming

Lecture 12. PHP. cp476 PHP

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

URLs and web servers. Server side basics. URLs and web servers (cont.) URLs and web servers (cont.) Usually when you type a URL in your browser:

Important Points about PHP:

Chapter 17. Fundamental Concepts Expressed in JavaScript

INFS 2150 Introduction to Web Development and e-commerce Technology. Programming with JavaScript

Objectives. Introduction to JavaScript. Introduction to JavaScript INFS Peter Y. Wu, RMU 1

PHP. Interactive Web Systems

Lecture 7 PHP Basics. Web Engineering CC 552

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

3 The Building Blocks: Data Types, Literals, and Variables

M275 - Web Development using PHP and MySQL

IDM 232. Scripting for Interactive Digital Media II. IDM 232: Scripting for IDM II 1

Client-Side Web Technologies. JavaScript Part I

Add Subtract Multiply Divide

Chapter 2: Functions and Control Structures

Exercise 1 Using Boolean variables, incorporating JavaScript code into your HTML webpage and using the document object

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Introduction of PHP Created By: Umar Farooque Khan

PHP can also increase your productivity enormously, both in development time and maintenance time.

PHP: The Basics CISC 282. October 18, Approach Thus Far

C: How to Program. Week /Mar/05

JME Language Reference Manual

1/22/2017. Chapter 2. Functions and Control Structures. Calling Functions. Objectives. Defining Functions (continued) Defining Functions

php Mr. Amit Patel Hypertext Preprocessor Dept. of I.T.

Reviewing all Topics this term

COMS 469: Interactive Media II

Object-Oriented Programming

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

Chapter 2 - Introduction to C Programming

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Decisions, Decisions. Testing, testing C H A P T E R 7

CGS 3066: Spring 2015 JavaScript Reference

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

COMP519 Web Programming Lecture 11: JavaScript (Part 2) Handouts

Course Outline. Introduction to java

Instructor s Notes Web Data Management PHP Sequential Processing Syntax. Web Data Management PHP Sequential Processing Syntax

Object Oriented Programming with Java

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

COMS 469: Interactive Media II

JavaScript I Language Basics

PHP Personal Home Page PHP: Hypertext Preprocessor (Lecture 35-37)

PHP INTERVIEW QUESTION-ANSWERS

CS1101: Lecture 9 The Shell as a Programming Language

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

HTML5 and CSS3 More JavaScript Page 1

INTERNET PROGRAMMING. Software Engineering Branch / 4 th Class Computer Engineering Department University of Technology

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Expressions and Variables

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

PHP Hypertext Preprocessor

PhpT Point, Simply Easy Learning

Elementary Programming

Programming language components

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

introjs.notebook March 02, 2014

In addition to the primary macro syntax, the system also supports several special macro types:

PHP 5 if...else...elseif Statements

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Scripting Languages. Diana Trandabăț

Bourne Shell Reference

CS11 Java. Fall Lecture 1

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

JavaScript: The Basics

Flow Control. Copyleft 2005, Binnur Kurt

Topic 4 Expressions and variables

Working with JavaScript

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Hello everyone! Page 1. Your folder should look like this. To start with Run your XAMPP app and start your Apache and MySQL.

CSC Software I: Utilities and Internals. Programming in Python

COSC 122 Computer Fluency. Programming Basics. Dr. Ramon Lawrence University of British Columbia Okanagan

Python for Non-programmers

Princess Nourah bint Abdulrahman University. Computer Sciences Department

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

REVIEW. The C++ Programming Language. CS 151 Review #2

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Transcription:

Introduction to PHP

Let's Look Back We talked about how to create a form in HTML Forms are one way to interact with users Users can enter information into forms which can be used by you (programmer) We also talked about how to validate these input data by using javascript on client-side But, what about storing and using data?

PHP Originally, PHP stood for Personal Home Page. The acronym doesn't mean much anymore, but officially it is called PHP: Hypertext Processor PHP is a server-side processing language. The server reads the PHP code and outputs browser-friendly HTML With PHP you can store data on the server as files or in databases like MySQL. With PHP, you can write code once and use it everywhere. Remember, you want DRY code (Don't Repeat Yourself). PHP lets you build dynamic webpages that respond to input from users.

PHP PHP runs on different platforms (Windows, Linux, Unix, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP is FREE to download and use. PHP is easy to learn and runs efficiently on the server side

How to use PHP In order to use PHP or any other server-side tool, you have two options: - Find a web host with PHP or other server-side tool support - Install a web server on your own PC, and then install PHP and other tools

How to use PHP In order to use PHP or any other server-side tool, you have two options: - Find a web host with PHP or other server-side tool support - Install a web server on your own PC, and then install PHP and other tools CS department has PHP installed on ww2 server and you can use that. I have posted a guide on how to do that on the course website.

How to use PHP In order to use PHP or any other server-side tool, you have two options: - Find a web host with PHP or other server-side tool support - Install a web server on your own PC, and then install PHP and other tools On windows, you can download and install WAMP. With one installation and you get an Apache webserver, database server and php. On mac, you can download and install MAMP.

PHP Basics The PHP code is enclosed in special start and end processing instructions <?php and?> that allow you to jump into and out of "PHP mode." PHP code is executed on the server, generating HTML which is then sent to the client. The client would receive the results of running that script, but would not know what the underlying code was.

PHP: Hello World index.php on the server <!doctype html> <html> <head> <title>php Hello World!</title> </head> <body> <?php echo "Hello World!"; print "<br>"; echo "Welcome!";?> </body> </html> What shows up in the browser Hello World! Welcome! Client sees this as source of the page <!doctype html> <html> <head> <title>php Hello World!</title> </head> <body> Hello World!<br>Welcome! </body> </html>

PHP: Hello World This program is extremely simple and you really did not need to use PHP to create a page like this. All it does is display: Hello World and Welcome using the PHP echo and print statements. Think of this as a normal HTML file which happens to have a set of special tags available to you that do a lot of interesting things.

PHP Comments In PHP, we use // to make a single-line comment or /* and */ to make a large comment block. Just similar to javascript <!doctype html> <html> <body> <?php // This is a comment /* This is a comment block */?> </body> </html>

PHP Variables A variable is a place to store values When you first create a variable, it does not have a value (it is null). You can set a value for a variable. Variables can hold different types of information, like words, numbers, and collections of data. The value of a variable can change over time.

PHP Variables In PHP, you write a variable with a dollar sign followed by the name of the variable. The variable name is case-sensitive. A new variable needs to have a unique name. Variable names need to start with a letter or underscore. Variable names can only be made of letters and numbers.

PHP Variables To create a variable, just type a dollar sign and the variable name. PHP will create the variable for you. $age; It is a good idea to give your variable a starting value. This is called initializing the variable. $age = 5; Once you have created a variable, you can use it in your code. Just type a dollar sign and the name of the variable $age = 5; echo $age;

PHP Variable Values: Numbers Variables can be numbers, either integers or floats (decimals). $numberofcoffees = 4; $coffeeprice = 2.3; PHP will automatically convert integers to floats if needed. Once you have numbers, you can do math with them! $totalprice = $numberofcoffees * $coffeeprice;

PHP Arithmetic Operators Example Name Result -$a Negation Opposite of $a $a + $b Addition Sum of $a and $b $a - $b Subtraction Difference of $a and $b $a * $b Multiplication Product of $a and $b $a / $b Division Quotient of $a and $b $a % $b Modulus Remainder of $a divided by $b

PHP Variable Values: Strings Variables can be strings(groups of characters). You put your string in quotes. $name = 'Fluffy'; If you want to use a quote in your string, you'll need to "escape" it with a backslash. echo 'I\'d like to use an apostrophe';

PHP String Operators You can put strings together with a period, the concatenation operator. $firstname = 'Fluffy'; $fullname = $firstname. ' McDougle'; echo $fullname; //Outputs 'Fluffy McDougle' You can also use.= to add things to the end of a string. $name = 'Fluffy'; $name.= ' McDougle'; echo $name; //Outputs 'Fluffy McDougle'

PHP Arrays Let's assume we have a list of items (car names). If we wanted to store these values in single variables we could do something like this: $car1 = 'BMW'; $car2 = 'Nissan'; $car3 = 'Honda'; But what if you had 300 values to be stored? Is there a better way to do this?

PHP Arrays The best solution here is to use an array. An array can hold all your variable values under a single name. And you can access the values by referring to the array name. Each element in the array has its own index so that it can be easily accessed. You can define the array of cars in two ways

PHP Numeric Arrays In the following example, the index is automatically assigned(starting from 0) $cars = array("bmw","nissan","honda","toyota"); In the following example we assign the index manually: $cars[0] = "BMW"; $cars[1] = "Nissan"; $cars[2] = "Honda"; $cars[3] = "Toyota";

PHP Numeric Arrays Regardless of the way we create the array, we can access individual elements by using their index: <!doctype html> <html> <body> <?php $cars = array("bmw","nissan","honda","toyota"); echo $cars[1]. " and ". $cars[2]. " are Japanese cars";?> </body> </html> Output: Nissan and Honda are Japanese cars.

PHP Associative Arrays With an associative array, each ID key is associated with a value. When storing data about specific named values, a numerical array is not always the best way to do it. With associative arrays we can use the values as keys and assign values to them.

PHP Associative Arrays In the following example, we use an array to assign ages to different people: $ages = array("peter"=>32,"nancy"=>30,"joe"=>44); We could also do the same thing as above, in this way: $ages['peter'] = 32; $ages['nancy'] = 30; $ages['joe'] = 44;

PHP Associative Arrays The ID keys can be used to access the values associated with them: <!doctype html> <html> <body> <?php $ages['peter'] = 32; $ages['nancy'] = 30; $ages['joe'] = 44; echo "Peter is ". $ages['peter']. " years old.";?> </body> </html> Output: Peter is 32 years old.

PHP Operators

PHP Operators

PHP Operators

PHP Operators

PHP Conditional Statements Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. In PHP we have the following conditional statements...

PHP Conditional Statements if statement - use this statement to execute some code only if a specified condition is true <!doctype html> <html> <body> <?php $d = date("d"); if ($d == "Fri") echo "Have a nice weekend!";?> </body> </html>

PHP Conditional Statements if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false <!doctype html> <html> <body> <?php $d = date("d"); if ($d == "Fri") echo "Have a nice weekend!"; else echo "Have a nice day!";?> </body> </html>

PHP Conditional Statements if...elseif...else statement - use this statement to select one of several blocks of code to be executed <!doctype html> <html> <body> <?php $d = date("d"); if ($d == "Fri") echo "Have a nice weekend!"; elseif($d == "Sun") echo "Have a nice Sunday!"; else echo "Have a nice day!";?> </body> </html>

PHP Conditional Statements If more than one line should be executed if a condition is true/false, the lines should be enclosed within curly braces { } <!doctype html> <html> <body> <?php $d = date("d"); if ($d == "Fri") { echo "Have a nice weekend!"; echo "See you on Monday!"; }?> </body> </html>

PHP Loops Often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we can use loops to perform a task like this. In PHP and other languages, we have several looping statements

PHP Loops while - loops through a block of code while a specified condition is true <!doctype html> <html> <body> <?php $i = 1; while ($i <= 5) { echo "The number is ". $i. "<br>"; $i++; }?> </body> </html> The loop starts with i=1. It will continue to run as long as i is less than, or equal to 5. It will increase by 1 each time the loop runs. Output: The number is 1 The number is 2 The number is 3 The number is 4 The number is 5

PHP Loops for - loops through a block of code a specified number of times for (init; condition; increment) { code to be executed; } init: Mostly used to set a counter (but can be any code to be executed once at the beginning of the loop) condition: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends. increment: Mostly used to increment a counter (but can be any code to be executed at the end of the loop)

PHP Loops for - loops through a block of code a specified number of times <!doctype html> <html> <body> <?php for ($i=1; $i<=5; $i++) { echo "The number is ". $i. "<br>"; }?> </body> </html> The loop starts with i=1. It will continue to run as long as i is less than, or equal to 5. It will increase by 1 each time the loop runs. Output: The number is 1 The number is 2 The number is 3 The number is 4 The number is 5

PHP Loops foreach - loops through a block of code for each element in an array <!doctype html> <html> <body> <?php $cars = array("bmw","nissan","honda","toyota"); foreach ($cars as $c) { echo $c. "<br>"; }?> </body> </html> This code will loop through values in the array and print them out. Output: BMW Nissan Honda Toyota

PHP Loops foreach - loops through a block of code for each element in an array <!doctype html> <html> <body> <?php $ages = array("peter"=>32,"nancy"=>30,"joe"=>44); foreach ($ages as $person => $age) { echo $person. " is ". $age. " years old." echo "<br>"; }?> </body> </html> This code will loop through keys and values in an associative array and print them out. Output: Peter is 32 years old. Nancy is 30 years old. Joe is 44 years old.

PHP Functions Functions are separable, reusable pieces of code. To use a function: - first declare the function function callmebymyname() { echo 'Hey Peyman!'; } - Then call it as many times as you want callmebymyname();

PHP Function Parameters To add more functionality to a function, we can add parameters. A parameter is just like a variable. Parameters are specified after the function name, inside the parentheses. Output: Hey Peter Hey Donald <!doctype html> <html> <body> <?php function callmebymyname($name) { echo 'Hey '. $name; } callmebymyname('peter'); $somename = 'Donald'; callmebymyname($somename);?> </body> </html>

PHP Function Return Values You can have a function give you back a value, to use later. Return will immediately end a function. <!doctype html> <html> <body> <?php function square($num) { return $num * $num; } echo square(4); Output: 16 625 $squareoffive = square(5); echo square($squareoffive);?> </body> </html>