1 Working with Data Types and Operators. 2 Using Variables and Constants The values stored in...
-
Upload
amber-rice -
Category
Documents
-
view
221 -
download
0
Transcript of 1 Working with Data Types and Operators. 2 Using Variables and Constants The values stored in...
![Page 1: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/1.jpg)
1
Working with Data Typesand Operators
![Page 2: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/2.jpg)
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
![Page 3: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/3.jpg)
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 sensitiveCan use numbers or an underscore ( _ ), but
not as 1st character after $
![Page 4: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/4.jpg)
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;
![Page 5: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/5.jpg)
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;
![Page 6: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/6.jpg)
6
To print both text strings and variables, send them to the echo() statement as individual arguments, separated by commas:
echo "<p>The legal voting age is ", $VotingAge, ".</p>";
![Page 7: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/7.jpg)
7
Defining Constants
A constant contains information that does not change during the course of program execution.
Example: pi = 3.141592 Constant names do not begin with a dollar
sign ($) Constant names use all UPPERCASE
letters.
![Page 8: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/8.jpg)
8
Use the define() function to create a constantdefine("CONSTANT_NAME", value);
The value you pass to the define() function can be a text string, number, or Boolean value
![Page 9: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/9.jpg)
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
![Page 10: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/10.jpg)
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
![Page 11: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/11.jpg)
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
![Page 12: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/12.jpg)
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
![Page 13: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/13.jpg)
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)
![Page 14: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/14.jpg)
14
A floating-point number is a number that contains decimal places or that is written in exponential notation (-6.16, 3.17, 2.7541)Exponential notation, or scientific notation,
is short for writing very large numbers or numbers with many decimal places (2.0e11)
![Page 15: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/15.jpg)
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
![Page 16: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/16.jpg)
16
Arrays
An array contains a set of data represented by a single variable name
Figure 3-7 Conceptual example of an array
![Page 17: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/17.jpg)
17
Declaring and Initializing Indexed Arrays An element refers to each piece of data
that is stored within an arrayBy default, it starts with the number zero (0)
An index is an element’s numeric position within the arrayReferenced by enclosing its index in brackets
at the end of the array name: $Provinces[1] second element.
![Page 18: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/18.jpg)
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" );
![Page 19: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/19.jpg)
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";
![Page 20: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/20.jpg)
20
Accessing Element Informationecho "<p>Canada's smallest province is
$Provinces[1].<br />";echo "Canada's largest province is $Provinces[4].</p>";
Figure 3-8 Output of elements in the $Provinces[ ] array
![Page 21: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/21.jpg)
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 "<p>Canada has ", count($Provinces), “
provinces and ", count($Territories), “ territories.</p>";
![Page 22: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/22.jpg)
22
count() Function (continued)
Figure 3-9 Output of the count() function
![Page 23: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/23.jpg)
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)
![Page 24: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/24.jpg)
24
To change the first array element in the $HospitalDepts[] array from “Anesthesia” to “Anesthesiology” use:
$HospitalDepts[0] = “Anesthesiology";
![Page 25: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/25.jpg)
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
![Page 26: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/26.jpg)
26
Building Expressions (continued)
Table 3-2 PHP Operator Types
![Page 27: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/27.jpg)
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
![Page 28: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/28.jpg)
28
Arithmetic Operators
Arithmetic operators are used in PHP to perform mathematical calculations (+ - x ÷)
Table 3-3 PHP arithmetic binary operators
![Page 29: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/29.jpg)
29
// ADDITION
$x = 100;
$y = 200;
$ReturnValue = $x + $y;
echo “<p>$ReturnValue after addition expression:”, $ReturnValue, </p>”;
.
.
.
.
![Page 30: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/30.jpg)
30
Arithmetic Operators (continued)
Figure 3-12 Results of arithmetic expressions
![Page 31: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/31.jpg)
31
Arithmetic Operators (continued)
$DivisionResult = 15 / 6;$ModulusResult = 15 % 6;echo "<p>15 divided by 6 is
$DivisionResult.</p>"; // prints '2.5'echo "The whole number 6 goes into 15 twice, with a remainder of $ModulusResult.</p>"; // prints '3'
Figure 3-13 Division and modulus expressions
![Page 32: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/32.jpg)
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
![Page 33: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/33.jpg)
33
Arithmetic Unary Operators (continued)
Table 3-4 PHP arithmetic unary operators
![Page 34: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/34.jpg)
34
Arithmetic Unary Operators (continued)
Figure 3-14 Script that uses the prefix increment operator
![Page 35: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/35.jpg)
35
Arithmetic Unary Operators (continued)
Figure 3-15 Output of the prefix version of the student
ID script
![Page 36: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/36.jpg)
36
Arithmetic Unary Operators (continued)
Figure 3-16 Script that uses the postfix increment operator
![Page 37: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/37.jpg)
37
Arithmetic Unary Operators (continued)
Figure 3-17 Output of the postfix version of the student ID script
![Page 38: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/38.jpg)
38
Assignment Operators
Assignment operators are used for assigning a value to a variable:
$MyFavoriteSuperHero = "Superman";
$MyFavoriteSuperHero = "Batman";
![Page 39: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/39.jpg)
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???????
![Page 40: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/40.jpg)
40
Assignment Operators (continued)
Table 3-5 PHP assignment operators
![Page 41: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/41.jpg)
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
![Page 42: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/42.jpg)
42
The comparison operator compares values, whereas the assignment operator assigns values
Comparison operators are used with conditional statements and looping statements
![Page 43: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/43.jpg)
43
Comparison and Conditional Operators (continued)
Table 3-6 PHP comparison operators
![Page 44: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/44.jpg)
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;
![Page 45: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/45.jpg)
45
If the conditional expression evaluates to true, expression1 executes
If the conditional expression evaluates to false, expression2 executes
![Page 46: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/46.jpg)
46
Comparison and Conditional Operators (continued)$BlackjackPlayer1 = 20;($BlackjackPlayer1 <= 21) ? $Result = "Player 1 is still in the game.“ : $Result = "Player 1 is out of the action.";echo "<p>", $Result, "</p>";
Figure 3-21 Output of a script with a conditional operator
![Page 47: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/47.jpg)
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
![Page 48: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/48.jpg)
48
1. $Gender = “male”;
$Age = 17;
$RiskFactor = $Gender == “male” && $Age <= 21;
Return TRUE or FALSE??
![Page 49: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/49.jpg)
49
2. $SpeedingTicket = 2;
$Age = 28;
$RiskFactor = $SpeedingTicket > 0 || $Age <= 21;
Return TRUE or FALSE??
![Page 50: 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,](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649ec05503460f94bcacf7/html5/thumbnails/50.jpg)
50
3. $TrafficViolations = true;
$SafeDriverDiscount = !$TrafficViolations;
Return TRUE or FALSE??