Tải bản đầy đủ (.pdf) (5 trang)

PHP Developer''''s Dictionary- P5 ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (385.59 KB, 5 trang )

PHP Developer’s Dictionary
IT-SC book
20
This section describes PHP's use of arrays, strings, type conversions, variables, and
constants. No programming language would be complete without these necessary
items. If you are familiar with other programming languages, these should be very
intuitive. The syntax for most of these items is similar to the C programming
language and their functionality is as you would expect. The following sections are
not intended to be an exhaustive review of arrays, variables, and type conversions,
but rather a basic review.
Arrays
As with every programming language, PHP would be incomplete if it did not include
the capability to define and manipulate arrays. PHP supports arrays that are indexed
by number and associative arrays. PHP also supports multidimensional arrays.
Arrays are variables that contain multiple elements indexed by numbers or names.
This means that a variable called
FirstName[1]
could contain the value
Kaitlin
,
whereas another variable called
FirstName[2]
could contain the value
Austin
. The
variable name is
FirstName
, and the index is the [1] or [2].
The
array()
function in PHP is used to define an array and assign values to it. This


function would be used like this:

$CustomerName = array ("Jean", "Loren", "Ted", "Gladys" );


The elements in the
$CustomerName
array can now be accessed by using the variable
name and the index in the following way:

print "$CustomerName[3]";


This would print the name
Gladys
, which is actually the fourth item in the array.
Remember that array indexes begin at 0.
Associative arrays are indexed with strings instead of numbers. This is useful when
describing the contents of the element using the index.
To define an associative array, we use the
array()
function and we must define both
the key and value for each element. In the following example, we create an
associative array called
$Contacts
with three elements:
FirstName
,
Phone
, and

Email.

$Contacts = array(Name=>"Eric", Phone=>"289-
9272",Email=>"");


We can now access any of the fields of
$Contacts
like this:

print $Contacts[Email];


This would print the value

.
PHP also supports multidimensional arrays. A multidimensional array can be thought
of as an array of arrays. In other words, we could define the
$Contacts
array to
contain multiple entries for different contacts. That definition would look like this:

PHP Developer’s Dictionary
IT-SC book
21
$Contacts[1] = array(Name=>"Eric",Phone=>"289-
9272",Email=>"");
$Contacts[2] = array(Name=>"Ryan",Phone=>"289-
9446",Email=>"");



Now, if we want to retrieve Ryan's email address, the code looks like this:

print $Contacts[2][Email];


PHP also has many functions that enable the programmer to manipulate, sort, and
return information about the array. These functions make it easy to access your
array information quickly and easily. Details about these functions are described in
Chapter 5,
"PHP Language Extensions."
Strings
PHP has many functions that enable you to manipulate and format strings. These
functions provide methods of determining the length of a string, finding a substring
in a string, removing white space from a string, replacing substrings, and changing
the case of a string. This section does not discuss all these cases, but it does provide
a general overview of these functions.
If you are familiar with the C programming language, you recognize the
printf()

function. This function enables you to output a string and variables in a multitude of
different ways. Consider the following example:

printf("This is a formatted number: %d\ n", 1957 );


This prints out the number, 1957, as a decimal value, which is defined by the
%d

specifier. There are other specifiers available to the

printf()
function. These include
specifiers that will print the value as a hexadecimal, ASCII, octal, binary, or floating-
point number. Chapter 5
discusses this function in detail and describes all the
available options.
The
strlen()
function returns the length of the string. This function is useful for
error checking and formatting. The following is an example of how to use this
function and what information is returned:

$Test = "This is a test string";
print strlen($Test); // prints out 21


The
strstr()
function returns part of the entire string. The portion of the string that
is returned is defined by an integer number. This integer number defines the number
of characters to return. A positive number starts counting characters from the front
of the string; a negative number counts from the end of the string. An example of
this function follows:

$date = "03-October-1957";
$Test = substr( $date, 10 )
print $Test; // Prints out the string "03-October";


PHP Developer’s Dictionary

IT-SC book
22
The
trim()
function is useful when a variable that is to be used has white space or
padding that needs to be removed. This function removes the spaces from the
beginning and the end of the string. An example of this is

$Test = " A String with leading and trailing spaces ";
$Test = trim($Test);
print $Test; // prints out the string without the spaces


The
strtoupper()
and the
strtolower()
functions convert the specified string to all
uppercase or all lowercase, respectively. Often when you are doing string
comparisons, it is a problem to test all possible cases. These functions convert the
string to one case, and thus make it easier to evaluate comparisons. The following is
an example of the
strtoupper()
and
strtolower()
functions in use:

$Test = "ThIs Is A mIxEd Up StRiNg";
$Upper = strtoupper( $Test );
print $Upper; // prints out the string "THIS IS A MIXED UP STRING"

$Lower = strtolower( $Test );
print $Lower; // prints out the string "this is a mixed up string"


This is by no means the entire list of string functions that are available in PHP. The
entire list of functions, along with their complete descriptions, is in Chapter 5
.
Type Conversion
In many programming languages, it is necessary to declare the variable as some
type. Different types of variables are handled in different ways and take different
amounts of memory. PHP does not require you to declare a variable's type when the
variable is initialized. PHP calculates the variable's type based on what value the
variable has. For instance, if a variable has the value of
3
, the variable is typed as
integer. If the variable has a value of
3.1415927
, the variable is typed as a double.
Data types in PHP can be integer, double, string, boolean, object, or array.
PHP has some functions to enable you to detect what a variable's type is. The
function
gettype()
returns a variable's type based on the value that is passed into
the function. PHP also gives you the ability to set an integer's type by using the
settype()
function.
Type casting is also supported in PHP. To cast a variable as a certain type, you place
the name of the data type in parentheses in front of the variable. For example, to
cast a copy of the variable named
$pi

to a double, the code is

$pi = 3.1415927
$copyofpi = (double)$pi;
print $copyofpi;


This example prints out the value
3.1415927
. If the same variable were cast to an
integer, the code would be

$pi = 3.1415927
$copyofpi = (integer)$pi;
print $copyofpi;
PHP Developer’s Dictionary
IT-SC book
23


The output of this example would be
3
because the variable would contain no
decimal point.
Variables
Variables in PHP come in many forms, but they are always preceded with a dollar
sign (
$
). Variables can contain letters, numbers, and the underscore (
_

) character.
Variables must contain only alphanumeric characters and must not contain any
spaces.
PHP also enables you to use something known as
dynamic variables
. A dynamic
variable is one in which the variable name can be stored in a variable. This can be
quite confusing, but quite useful at the same time. Please consider the following
example:

$FirstName = "Kaitlin";


This variable declaration is the same as

$Variable="FirstName";
$$Variable = "Kaitlin";


The
$Variable
variable contains the string
"FirstName"
. You can think of the
variable
$$Variable
as a single
$
followed by the value of
$Variable

or
"FirstName"
.
Constants
Constants are values that are defined in your PHP script and that do not change.
Constants are defined by using the
define()
function. After the constant is defined,
it cannot be changed. Constants are accessed by using the name of the constant
only. There is no dollar sign preceding the name of the constant. For example, the
constant
PI
is defined like this:

define( "PI",3.1415927);


To access the value of
PI
, the code would look like this:

$Circumference = PI*($Radius*$Radius);


Note that constants are usually defined using all capital letters. PHP also has some
internal constants. For instance, the constant
__FILE__
returns the name of the file
that PHP is currently reading. The constant
__LINE__

returns the line number of the
file. These constants are normally used when you are generating error messages.
Operators and Mathematical Functions
PHP Developer’s Dictionary
IT-SC book
24
PHP, like other programming languages, provides functions for performing many
types of mathematical functions. Details of these mathematical functions are
described in Chapter 5
. This section gives you an overview of the mathematical
operators and functions that are available in PHP and examples of how to use them.
Expressions and Operators
Mathematical expressions consist of operators and operands. Usually, two operands
are connected by an operator to create the expression. Consider this example:

6=2+4


The numbers 2 and 4 are operands, the operator is the plus (+) sign, and the result
is 6. This entire example is referred to as an expression.
PHP has four different types of operators: assignment operators, mathematical
operators, comparison operators, and logical operators. Assignment operators assign
a specific value to a variable. Assignment operators are described in Table 1.1
.
Table 1.1. Assignment Operators
Operator

Example

Description


=

$x = 2

Assign the number
2 to $x

+=

$x += 2

Assign the value of
$x + 2
to
$x

-=

$x -= 2

Assign the value of
$x - 2
to
$x


/=

$x /= 2


Assign the value of
$x
divided by
2
to
$x


*=

$x *= 2

Assign the value of
$x
multiplied by
2
to
$x


%=

$x %= 2

Assign the value of
$x
to the modulus
2
of

$x


.=

$x .= "
value"

Assign the value of
$x
to the value of
$x
concatenated with
"
value"


The mathematical operators in PHP do exactly what you would expect. They include
the basic addition, subtraction, division, and multiplication operators. PHP also uses
the modulus operator. This operator divides the left operand by the right operand,
and returns the remainder. Table 1.2
lists the mathematical operators available in
PHP.
Table 1.2. Mathematical Operators
Operator

Description

+


Addition

-

Subtraction

/

Division

*

Multiplication

%

Modulus

Comparison operators compare the left and right operands and return either a
Boolean true or Boolean false. The comparison operators are listed in Table 1.3
.
Table 1.3. Comparison Operators
Operator

Description

==

Equivalent to


!=

Not equivalent to

===

Identical to

×