0% found this document useful (0 votes)
63 views

Web Programming With PHP

This document discusses working with data types and operators in PHP. It covers PHP variables and constants, data types like integers, floats, Booleans, and arrays. It also discusses expressions and operators like arithmetic, assignment, comparison, logical, and special operators. Finally, it covers type casting and operator precedence.

Uploaded by

Prashant Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Web Programming With PHP

This document discusses working with data types and operators in PHP. It covers PHP variables and constants, data types like integers, floats, Booleans, and arrays. It also discusses expressions and operators like arithmetic, assignment, comparison, logical, and special operators. Finally, it covers type casting and operator precedence.

Uploaded by

Prashant Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 58

Web Programming

with PHP
Chapter 3

Working with Data Types


and Operators

PHP Programming with MySQL


Objectives

Work with variables and constants


Study data types
Use expressions and operators
Cast the data types of variables
Learn about operator precedence
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
Using Variables and Constants

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 not start with a numeric value
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;
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;
Displaying Variables

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>";
Defining Constants

A constant contains information that


does not change during the course of
program execution
Constant names do not begin with a
dollar sign ($)
Constant names use all uppercase
letters
Defining Constants

#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
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
Working with Data Types

Table 3-1 Primitive PHP data types


Page 115
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
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
Working with Data Types
(continued)

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
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)
Numeric Data Types

PHP supports two numeric data types:


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)
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
Boolean Values

In PHP programming, you can only use


true or false
In other programming languages, you
can use integers such as 1 = true, 0 =
false
Arrays

An array contains a set of data


represented by a single variable name

Figure 3-7 Conceptual example of an array


PHP EXPRESSIONS

Building Expressions
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
Building Expressions

A literal is a value such as a literal


string or a number
Operators are symbols (+) (*) that
are used in expressions to manipulate
operands
Building Expressions (cont’d)

Table 3-2 PHP Operator Types


Building Expressions (cont’d)

A binary operator requires an


operand before and after the
operator
A unary operator requires a single
operand either before or after the
operator
Arithmetic Operators

Arithmetic operators are used in PHP


to perform mathematical calculations
(+ - x ÷)
Table 3-3 PHP arithmetic binary operators
Arithmetic Operators(cont’d)

Figure 3-12 Results of arithmetic expressions


Page 127
Arithmetic Operators (cont’d)

$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


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
Arithmetic Unary Operators
(continued)

Table 3-4 PHP arithmetic unary operators


Arithmetic Unary Operators (cont’d)

Figure 3-14 Script that uses the prefix


increment operator Page 131
Arithmetic Unary Operators (cont’d)

Figure 3-15 Output of the prefix version of the


student ID script Page 131
Arithmetic Unary Operators (cont’d)

Figure 3-16 Script that uses the postfix increment


operator Page 132
Arithmetic Unary Operators (cont’d)

Figure 3-17 Output of the postfix version of the


student ID script Page 132
Assignment Operators

Assignment operators are used for


assigning
a value to a variable:
$MyFavoriteSuperHero =
"Superman";
$MyFavoriteSuperHero = "Batman";
Assignment Operators

Compound assignment operators


perform mathematical calculations on
variables and literal values in an
expression, and then assign
a new value to the left operand
Assignment Operators (cont’d)

Table 3-5 PHP assignment operators Page 134


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
Comparison and
Conditional Operators

The comparison operator compares


values, whereas the assignment operator
assigns values
Comparison operators are used with
conditional statements and looping
statements
Comparison and Conditional
Operators (continued)

Table 3-6 PHP comparison operators Page137


Comparison and Conditional
Operators (contd)

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;
Comparison and Conditional
Operators (contd)

If the conditional expression


evaluates to true, expression1
executes
If the conditional expression
evaluates to false, expression2
executes
Comparison and Conditional
Operators (cont’d)

$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 139
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
Logical Operators

A Boolean value of true or false is


returned after two operands are
compared
Table 3-7 PHP logical operators Page 141
Special Operators

Table 3-8 PHP special operators


Type Casting

Casting or type casting copies the


value contained in a variable of one
data type into a variable of another
data type
The PHP syntax for casting variables
is:
$NewVariable = (new_type)
$OldVariable;
Type Casting

(new_type) refers to the type-


casting operator representing the
type to which you want to cast the
variable
gettype() function

Returns one of the following strings,


depending on the data type:
Boolean
Integer
Double
String
Array
Object
Resource
NULL
Unknown type
Order of Precedence
Understanding Operator
Precedence

Operator precedence refers to the


order in which operations in an
expression are evaluated
Associativity is the order in which
operators of equal precedence
execute
Associativity is evaluated on a left-
to-right or a right-to-left basis
Understanding Operator
Precedence (cont’d)

Table 3-9 Operator precedence in PHP Page 147


Summary

The values a program stores in


computer memory are called variables
A data type is the specific category
of information that a variable
contains
PHP is a loosely typed programming
language
Summary

An integer is a positive or negative


number with no decimal places
A Boolean value is a logical value of
true or false
Summary (continued)

An array contains a set of data


represented by a single variable name
Operands are variables and literals
contained in an expression
A binary operator requires an
operand before and after the
operator
Summary (continued)

A unary operator requires a single


operand either before or after the
operator
Assignment operators are used for
assigning a value to a variable
Summary (continued)

The conditional operator executes


one of two expressions, based on the
results of a conditional expression
Logical operators are used for
comparing two Boolean operands for
equality
Summary (continued)

Casting or type casting copies the


value contained in a variable of one
data type into a variable of another
data type
Operator precedence is the order in
which operations in an expression are
evaluated

You might also like