1 Working with Data Types and Operators. 2 Using Variables and Constants The values stored in computer memory are called variables The values, or data,

Slides:



Advertisements
Similar presentations
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Advertisements

Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
Objectives Install and configure a Web server
PHP Introduction.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
ASP.NET Programming with C# and SQL Server First Edition
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
British Columbia Immigration Source: Citizenship and Immigration Canada Facts and Figures Immigration Overview Annual Number of Immigrants to British.
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.
Lecture 2 Introduction to PHP MIS 3501, Spring 2014 Jeremy Shafer Department of MIS Fox School of Business Temple University January 30, 2014.
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.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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,
Numeric Types, Expressions, and Output ROBERT REAVES.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CONFEDERATION of Canada.
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators.
PHP Programming with MySQL Slide 3-1 CHAPTER 3 Working with Data Types and Operators.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
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.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
PHP using MySQL Database for Web Development (part II)
JavaScript, Sixth Edition
Objectives In this chapter you will: Create PHP scripts
BASIC ELEMENTS OF A COMPUTER PROGRAM
Web Programming– UFCFB Lecture 19-20
PHP Introduction.
Java Programming: From Problem Analysis to Program Design, 4e
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
2.1 Parts of a C++ Program.
JavaScript Data Concepts
PHP.
C Operators, Operands, Expressions & Statements
Objectives Install and configure a Web server
Chapter 2: Introduction to C++.
Primitive Types and Expressions
Presentation transcript:

1 Working with Data Types and Operators

2 Using Variables and Constants The values stored in computer memory are called variables The values, or data, contained in variables are classified into categories known as data types

3 The name you assign to a variable is called an identifier and it:  Must begin with a dollar sign ($)  Cannot include spaces  Is case sensitive  Can use numbers or an underscore ( _ ), but not as 1 st character after $

4 Declaring and Initializing Variables Specifying and creating a variable name is called declaring the variable Assigning a first value to a variable is called initializing the variable In PHP, you must declare and initialize a variable in the same statement: $variable_name = value;

5 Displaying Variables To print a variable with the echo() statement, pass the variable name to the echo() statement without enclosing it in quotation marks: $VotingAge = 18; Echo $VotingAge;

6 To print both text strings and variables, send them to the echo() statement as individual arguments, separated by commas: echo " The legal voting age is ", $VotingAge, ". ";

7 Defining Constants A constant contains information that does not change during the course of program execution. Example: pi = Constant names do not begin with a dollar sign ($) Constant names use all UPPERCASE letters.

8 Use the define() function to create a constant define("CONSTANT_NAME", value); The value you pass to the define() function can be a text string, number, or Boolean value

9 Working with Data Types A data type is the specific category of information that a variable contains Data types that can be assigned only a single value are called primitive types Table 3-1 Primitive PHP data types

10 Working with Data Types (continued) The PHP language supports:  A resource data type – a special variable that holds a reference to an external resource such as a database or XML file  Reference or composite data types, which contain multiple values or complex types of information  Two reference data types: arrays and objects

11 Working with Data Types (continued) Strongly typed programming languages require you to declare the data types of variables Static or strong typing refers to data types that do not change after they have been declared

12 Loosely typed programming languages do not require you to declare the data types of variables Dynamic or loose typing refers to data types that can change after they have been declared

13 Numeric Data Types PHP supports two numeric data types: An integer is a positive or negative number with no decimal places (-250, 2, 100, 10,000)

14 A floating-point number is a number that contains decimal places or that is written in exponential notation (-6.16, 3.17, )  Exponential notation, or scientific notation, is short for writing very large numbers or numbers with many decimal places (2.0e11)

15 Boolean Values A Boolean value is a value of true or false It decides which part of a program should execute and which part should compare data In PHP programming, you can only use true or false In other programming languages, you can use integers such as 1 = true, 0 = false

16 Arrays An array contains a set of data represented by a single variable name Figure 3-7 Conceptual example of an array

17 Declaring and Initializing Indexed Arrays An element refers to each piece of data that is stored within an array  By default, it starts with the number zero (0) An index is an element’s numeric position within the array  Referenced by enclosing its index in brackets at the end of the array name: $Provinces[1]  second element.

18 Creating an Array The array() construct syntax is: $array_name = array(values); $Provinces = array( "Newfoundland and Labrador", "Prince Edward Island", "Nova Scotia", "New Brunswick", "Quebec", "Ontario", "Manitoba", "Saskatchewan", "Alberta", "British Columbia" );

19 Creating an Array (continued) Array name and brackets syntax is: $array_name[ ] $Provinces[] = "Newfoundland and Labrador"; $Provinces[] = "Prince Edward Island"; $Provinces[] = "Nova Scotia"; $Provinces[] = "New Brunswick"; $Provinces[] = "Quebec"; $Provinces[] = "Ontario"; $Provinces[] = "Manitoba"; $Provinces[] = "Saskatchewan"; $Provinces[] = "Alberta"; $Provinces[] = "British Columbia";

20 Accessing Element Information echo " Canada's smallest province is $Provinces[1]. "; echo "Canada's largest province is $Provinces[4]. "; Figure 3-8 Output of elements in the $Provinces[ ] array

21 count() Function Use the count() function to find the total number of elements in an array  $Provinces = array("Newfoundland and Labrador", "Prince Edward Island", "Nova Scotia", "New Brunswick", "Quebec", "Ontario", " Manitoba", "Saskatchewan", "Alberta", "British Columbia"); $Territories = array("Nunavut", "Northwest Territories", "Yukon Territory"); echo " Canada has ", count($Provinces), “ provinces and ", count($Territories), “ territories. ";

22 count() Function (continued) Figure 3-9 Output of the count() function

23 Modifying Elements Include the index for an individual element of the array: $HospitalDepts = array( "Anesthesia",// first element(0) "Molecular Biology",// second element(1) "Neurology");// third element(2)

24 To change the first array element in the $HospitalDepts[] array from “Anesthesia” to “Anesthesiology” use : $HospitalDepts[0] = “Anesthesiology";

25 Building Expressions An expression is a literal value or variable that can be evaluated by the PHP scripting engine to produce a result Operands are variables and literals contained in an expression A literal is a value such as a literal string or a number Operators are (+) (*) that are used in expressions to manipulate operands

26 Building Expressions (continued) Table 3-2 PHP Operator Types

27 Building Expressions (continued) A binary operator requires an operand before and after the operator A unary operator requires a single operand either before or after the operator

28 Arithmetic Operators Arithmetic operators are used in PHP to perform mathematical calculations (+ - x ÷) Table 3-3 PHP arithmetic binary operators

29 // ADDITION $x = 100; $y = 200; $ReturnValue = $x + $y; echo “ $ReturnValue after addition expression:”, $ReturnValue, ”;.

30 Arithmetic Operators (continued) Figure 3-12 Results of arithmetic expressions

31 Arithmetic Operators (continued) $DivisionResult = 15 / 6; $ModulusResult = 15 % 6; echo " 15 divided by 6 is $DivisionResult. "; // prints '2.5' echo "The whole number 6 goes into 15 twice, with a remainder of $ModulusResult. "; // prints '3' Figure 3-13 Division and modulus expressions

32 Arithmetic Unary Operators The increment (++) and decrement (--) unary operators can be used as prefix or postfix operators A prefix operator is placed before a variable A postfix operator is placed after a variable

33 Arithmetic Unary Operators (continued) Table 3-4 PHP arithmetic unary operators

34 Arithmetic Unary Operators (continued) Figure 3-14 Script that uses the prefix increment operator

35 Arithmetic Unary Operators (continued) Figure 3-15 Output of the prefix version of the student ID script

36 Arithmetic Unary Operators (continued) Figure 3-16 Script that uses the postfix increment operator

37 Arithmetic Unary Operators (continued) Figure 3-17 Output of the postfix version of the student ID script

38 Assignment Operators Assignment operators are used for assigning a value to a variable: $MyFavoriteSuperHero = "Superman"; $MyFavoriteSuperHero = "Batman";

39 Compound assignment operators perform mathematical calculations on variables and literal values in an expression, and then assign a new value to the left operand Example : $x = 100; $y = 200; $x += $y; Answer???????

40 Assignment Operators (continued) Table 3-5 PHP assignment operators

41 Comparison and Conditional Operators Comparison operators are used to compare two operands and determine how one operand compares to another A Boolean value of true or false is returned after two operands are compared

42 The comparison operator compares values, whereas the assignment operator assigns values Comparison operators are used with conditional statements and looping statements

43 Comparison and Conditional Operators (continued) Table 3-6 PHP comparison operators

44 Comparison and Conditional Operators (continued) The conditional operator executes one of two expressions, based on the results of a conditional expression The syntax for the conditional operator is: conditional expression ? expression1 : expression2;

45 If the conditional expression evaluates to true, expression1 executes If the conditional expression evaluates to false, expression2 executes

46 Comparison and Conditional Operators (continued) $BlackjackPlayer1 = 20; ($BlackjackPlayer1 ", $Result, " "; Figure 3-21 Output of a script with a conditional operator

47 Logical Operators Logical operators are used for comparing two Boolean operands for equality A Boolean value of true or false is returned after two operands are compared Table 3-7 PHP logical operators

48 1. $Gender = “male”; $Age = 17; $RiskFactor = $Gender == “male” && $Age <= 21; Return TRUE or FALSE??

49 2. $SpeedingTicket = 2; $Age = 28; $RiskFactor = $SpeedingTicket > 0 || $Age <= 21; Return TRUE or FALSE??

50 3. $TrafficViolations = true; $SafeDriverDiscount = !$TrafficViolations; Return TRUE or FALSE??