Introduction to PHP and MySQL (Creating Database-Driven Websites)

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Data types and variables
JavaScript, Fourth Edition
Chapter 2: Introduction to C++.
JavaScript, Third Edition
Introduction to C Programming
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
PHP Using Variables and Operators Mohammed M. Hassoun 2012.
Objectives You should be able to describe: Data Types
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Input, Output, and Processing
1 How do you indicate the PHP sections of your HTML document ? Canionical Php Tags   IF you use this.
Chapter 2: Using Data.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Strings, output, quotes and comments
Chapter 2 Variables.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
Chapter 3 Introduction to PHP. Incorporating PHP Within HTML By default, PHP documents end with the extension.php files ending with.htm or.html to also.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
CHAPTER 5 SERVER SIDE SCRIPTING
The Selection Structure
PHP Introduction.
Intro to PHP & Variables
Java Programming: From Problem Analysis to Program Design, 4e
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
T. Jumana Abu Shmais – AOU - Riyadh
elementary programming
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2 Variables.
Presentation transcript:

Introduction to PHP and MySQL (Creating Database-Driven Websites)

 Write and execute a simple PHP script  Create statements and comments, and name variables  Use variables to store values  Choose between PHP’s data types  Understand the special NULL data type  Read GET and POST form input and store it in variables  Perform calculations and comparisons using operators  Use and override operator precedence rules

<?php PHP code... ?> You can also use the short version which looks like this: <? PHP code ?>

Q: This creature can change colour to blend in with its surroundings. What is its name? <?php //Print output echo ' A: Chameleon '; ?> Compile or Interpret??

 A PHP script consists of one or more statements with each statement ending in a semicolon. Everything outside the tags is also ignored by the parser, and returned ‘as is’. Only the code between the tags is read and executed.  You can omit the semicolon on the last line of a PHP block since the closing ?> includes an implicit semicolon.  is perfectly valid PHP code.

<?php //This is a single-line comment #So is this /*And this is a multiline comment */ ?>

Variables are:  the building blocks of any programming language.  used to store both numeric and nonnumeric data.  contents of a variable can be altered during program execution  variables can be compared and manipulated using operators.

 PHP supports a number of different variable types - Booleans, integers, floating point numbers, strings, arrays, objects, resources, and NULLs  PHP can automatically determine variable type by the context in which it is being used.

 Every variable has a name which is preceded by a dollar ($) symbol  A variable name must begin with a letter or underscore character, optionally followed by more letters, numbers and underscores.  VALID $firstname, $one_day, $INCOME  INVALID $123 and $48hrs

Q: This creature has tusks made of ivory. What is its name? <?php //Define variable $answer = 'Elephant'; //Print output echo " $answer "; ?> Can use print() function instead

<?php $age = $dob + 15; ?> assignment operator variable Value - need not be fixed. Could be another variable, an expression or an expression involving other variables

<?php $today = "Feb "; echo "Today is $today"; ?>

Enter your message: Most critical line !

<?php //Retrieve form data in a variable $input = $_POST['msg']; //Print it echo "You said: $input "; ?> $_POST and $_GET are special PHP variables - special types of predefined variables called global arrays.

Data TypeDescriptionExample BooleanThe simplest variable type in PHP, a Boolean variable simply specifies a true or false value. $auth = true; IntegerAn integer is simply a whole number like 75, -95, 2000 or 1. $age = 99; Floating-pointA floating-point number is typically a fractional number such as 12.5 or Floating point numbers may be specified using either decimal or scientific notation. $temperature = 56.89; StringA string is a sequence of characters, like 'hello' or 'abracadabra'. String values may be enclosed in either double quotes ("") or single quotes (''). $name = 'Harry';

<?php $auth = true; $age = 27; $name = 'Bobby'; $temp = 98.6; echo gettype($name). " "; echo gettype($auth). " "; echo gettype($age). " "; echo gettype($temp). " "; ?>

 To explicitly mark a variable as numeric or string, use the settype() function.  PHP also supports a number of specialized functions to check if a variable or value belongs to a specific type: Function What It Does is_bool() Checks if a variable or value is Boolean is_string() Checks if a variable or value is a string is_numeric() Checks if a variable or value is a numeric string is_float() Checks if a variable or value is a floating point number is_int() Checks if a variable or value is an integer is_null() Checks if a variable or value is NULL is_array() Checks if a variable is an array is_object() Checks if a variable is an object

 String values enclosed in double quotes are automatically parsed for variable names. If variable names are found they are automatically replaced with the appropriate variable value.  cf. to what happens when string values are enclosed in single quotes………

<?php $identity = 'James Bond'; $car = 'BMW'; //This would contain the string "James Bond drives a BMW" $sentence = "$identity drives a $car"; echo $sentence. “ ”; //This would contain the string "$identity drives a $car" $sentence = '$identity drives a $car'; echo $sentence; ?>

<?php //Will cause an error due to mismatched quotes $statement = 'It's hot outside'; echo $statement; //Will be fine $statement = 'It\'s hot outside'; echo $statement; ?> If your string contains quotes, carriage returns or backslashes, it is necessary to escape these special characters with a backslash.

<?php //Will cause an error due to mismatched quotes //$statement = 'It's hot outside'; //echo $statement; //Will be fine $statement = 'It\'s hot outside'; echo $statement; ?>

 The NULL data type means that a variable has no value.  A NULL is typically seen when a variable is initialized but not yet assigned a value, or when a variable has been de-initialized with the unset() function.

<?php //Check type of uninitialized variable - returns NULL echo gettype($me). " "; //Assign a value $me = 'David'; //Check type again - returns STRING echo gettype($me). " "; //Deinitialize variable unset($me); //Check type again - returns NULL echo gettype($me). " "; ?> Using unset(); Using gettype();

 PHP comes with over 15 operators - including operators for assignment, arithmetic, string, comparison and logical operations.

OperatorWhat It Does =Assignment +Addition -Subtraction *Multiplication /Division; returns quotient %Division; returns modulus.String concatenation

OperatorWhat It Does ==Equal to ===Equal to and of the same type !==Not equal to or not of the same type <> aka !=Not equal to <Less than <=Less than or equal to >Greater than

OperatorWhat It Does >=Greater than or equal to &&Logical AND ||Logical OR xorLogical XOR !Logical NOT ++Addition by 1 --Subtraction by 1

<?php $num1 = 101; $num2 = 5; $sum = $num1 + $num2; echo $sum." "; $diff = $num1 - $num2; echo $diff." "; $product = $num1 * $num2; echo $product." "; $quotient = $num1 / $num2; echo $quotient." "; $remainder = $num1 % $num2; echo $remainder." "; ?>

The following two code snippets are equivalent: <?php $a = $a + 10; ?> <?php $a += 10; ?> Addition and assignment simultaneously

<?php $username = 'jsmith'; $domain = 'bedford.ac.uk'; //Combine them using the concatenation operator $ = $username. $domain; echo $ ; ?> Concatenation

<?php //Define string $str = 'the'; //Add and assign $str.= 'n'; //$str now contains "then" echo $str; ?> Concatenation and assignment simultaneously

<?php //Define some variables $mean = 29; $median = 40; $mode = 29; //Less-than operator returns true if left side is less than right //Returns true here $result = ($mean < $median); echo "Answer: ".$result." ";

 IMPORTANT - The result of a comparison test is always a Boolean value (either true or false).  This makes comparison operators a critical part of your toolkit since you can use them in combination with a conditional statement to send a script down any one of many action paths.

The === Operator  An important comparison operator in PHP 4.0 is the === operator which enables you to test both for equality and type.

<?php $user = 'joe'; $pass = 'trym3'; $saveCookie = 1; $status = 1; //Logical AND returns true if all conditions are true //Returns true here $result = (($user == 'joe') && ($pass == 'trym3')); echo "Answer: ".$result." ";

//Logical OR returns true if any condition is true //Returns false here $result = (($status < 1) || ($saveCookie == 0)); echo "Answer: ".$result." "; //Logical NOT returns true if the condition is false and vice-versa //Returns false here $result = !($saveCookie == 1); echo "Answer: ".$result." ";

//Logical XOR returns true if any of the two conditions are true //Returns false if both conditions are true //Returns false here $result = (($status == 1) XOR ($saveCookie == 1)); echo "Answer: ".$result." "; ?>

<?php //Define $total as 10 $total = 10; //Increment it $total++; //$total is now 11 echo "Total now is: ".$total; ?>

Note: is functionally equivalent to. These operators are frequently used in loops to update the value of the loop counter.

You can override these rules with parentheses. This reduces ambiguity and ensures that operators are evaluated in the order that you specify. For example, the expression 10 * would return 101 while the expression 10 * (10 + 1) would return 110. '!' '++' '--' '*' '/' '%' '+' '−' '.' ' ' '>=' '==' '!=' '===' '!==' '&&' '||' '?' ':'