Transcript ppt

Creating PHP Pages
Chapter 6
PHP Variables, Constants and
Operators
Basic 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
PHP 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
PHP Variables
• Variables are not statically typed
• Integers can become floats can become strings
• Variable types include :
 Boolean
 Integer
 Float
 String
 Array
 Object
 Resource
 NULL
PHP 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
Displaying 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>";
Naming 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
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;
PHP 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
constant
define("CONSTANT_NAME", value);
• The value you pass to the define() function
can be a text string, number, or Boolean value
PHP 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
value
Less than < and greater than >
<= and >= as above but include equality
PHP Operators
• Assignment (=) and combined
assignment
$a
$a
$b
$b
= 3;
+= 5; // sets $a to 8;
= "Hello ";
.= "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.)
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
Data 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
Numerics 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)
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 Boolean values
• In other programming languages, you can
use integers such as 1 = TRUE, 0 = FALSE
References
References :
1.
2.
3.
4.
Anonymous.(n.d.). Apache HTTP Server Documentation
Version 2.2. Retrieved from
http://httpd.apache.org/docs/2.2/.
Achour, M., Betz, F. (n.d.), PHP Manual. Retrieved from
http://www.php.net/download-docs.php.
Anonymous. (n.d.). MySQL Reference Manual. Retrieved
from http://downloads.mysql.com/docs/.
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.