creating php pages chapter 6 php variables, constants and operators

16
Creating PHP Pages Creating PHP Pages Chapter 6 Chapter 6 PHP Variables, Constants PHP Variables, Constants and Operators and Operators

Upload: mervyn-lamb

Post on 18-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators

Creating PHP PagesCreating PHP Pages

Chapter 6Chapter 6PHP Variables, Constants and PHP Variables, Constants and

OperatorsOperators

Page 2: Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators

Basic PHP VariablesBasic PHP Variables

• The values stored in computer memory are called variables

• The values, or data, contained in variables are classified into categories known as data types

• The name you assign to a variable is called an identifier

Page 3: Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators

PHP VariablesPHP Variables

• Prefixed with a $• Assign values with = operator• Example: $name = “John Doe”;• No need to define type• Variable names are case sensitive• $name and $Name are different

Page 4: Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators

PHP VariablesPHP Variables

• Variables are not statically typed• Integers can become floats can become strings• Variable types include : Boolean Integer Float String Array Object Resource NULL

Page 5: Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators

PHP VariablesPHP Variables

• Assigned by value$foo = "Bob"; $bar = $foo;

• Assigned by reference, this links vars$bar = &$foo;

• Some are preassigned, server and env vars

For example, there are PHP vars, eg. PHP_SELF, HTTP_GET_VARS

Page 6: Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators

Displaying VariablesDisplaying Variables

• To display a variable with the echo statement, pass the variable name to the echo statement without enclosing it in quotation marks :$VotingAge = 18;echo $VotingAge;

• To display 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: Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators

Naming VariablesNaming Variables

• The name you assign to a variable is called an identifier

• The following rules and conventions must be followed when naming a variable:• Identifiers must begin with a dollar sign ($)• Identifiers may contain uppercase and

lowercase letters, numbers, or underscores (_). The first character after the dollar sign must be a letter.

• Identifiers cannot contain spaces• Identifiers are case sensitive

Page 8: Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators

Declaring and Initializing VariablesDeclaring 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 9: Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators

PHP ConstantsPHP Constants

• Constants are special variables that cannot be changed

• Constant names do not begin with a dollar sign ($)

• Use them for named items that will not change• Constant names use all uppercase letters • 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 10: Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators

PHP OperatorsPHP Operators

• Standard Arithmetic operators+, -, *, / and % (modulus)

• String concatenation with a period (.)$car = “SEAT” . “ Altea”;echo $car; would output “SEAT Altea”

• Basic Boolean comparison with “==”Using only = will overwrite a variable

valueLess than < and greater than ><= and >= as above but include equality

Page 11: Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators

PHP OperatorsPHP Operators

• Assignment (=) and combined assignment

$a = 3;$a += 5; // sets $a to 8;$b = "Hello ";$b .= "There!"; // sets $b to "Hello There!";

• Bitwise (&, |, ^, ~, <<, >>)$a ^ $b(Xor: Bits that are set in $a or $b

but not both are set.)~ $a (Not: Bits that are set in $a are not

set, and vice versa.)

Page 12: Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators

Data TypesData 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

Page 13: Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators

Data TypesData Types

• PHP is not strictly typed• A data type is either text or numeric• PHP decides what type a variable is• PHP can use variables in an appropriate way

automatically• E.g.• $vat_rate = 0.175; /* VAT Rate is numeric */• echo $vat_rate * 100 . “%”; //outputs “17.5%”• $vat_rate is converted to a string for the

purpose of the echo statement• Object and Array also exist as types

Page 14: Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators

Numerics Data TypesNumerics Data Types

• PHP supports two numeric data types:• An integer is a positive or negative number

and 0 with no decimal places (-250, 2, 100, 10,000)

• 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 a shortened format for writing very large numbers or numbers with many decimal places (2.0e11)

Page 15: Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators

Boolean ValuesBoolean 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 Boolean values

• In other programming languages, you can use integers such as 1 = TRUE, 0 = FALSE

Page 16: Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators

ReferencesReferences

References :

1.Anonymous.(n.d.). Apache HTTP Server Documentation Version 2.2. Retrieved from http://httpd.apache.org/docs/2.2/.2.Achour, M., Betz, F. (n.d.), PHP Manual. Retrieved from http://www.php.net/download-docs.php.3.Anonymous. (n.d.). MySQL Reference Manual. Retrieved from http://downloads.mysql.com/docs/. 4.Naramore, E., Gerner, J., Le Scouarnec, Y., Stolz, J., Glass, M. K. (2005). Beginning PHP5, Apache, and MySQL® Web Development. Indianapolis, IN: Wiley Publishing, Inc.