Thursday, September 30, 2010

Operators and Expressions in PHP

You can now assign data to variables. You can even investigate and change the data
type of a variable. A programming language isn't very useful, though, unless you
can manipulate the data you can store. Operators are symbols that make it possible
to use one or more values to produce a new value. A value that is operated on by an
operator is referred to as an operand.
NEW
TERM
An operator is a symbol or series of symbols that, when used in
conjunction with values, performs an action and usually produces a
new value.
NEW
TERM
An operand is a value used in conjunction with an operator. There
are usually two operands to one operator.
Let's combine two operands with an operator to produce a new value:
4 + 5
4 and 5 are operands. They are operated on by the addition operator (+) to produce
9. Operators almost always sit between two operands, though you will see a few
exceptions later in this hour.
The combination of operands with an operator to manufacture a result is called an
expression. Although most operators form the basis of expressions, an expression
need not contain an operator. In fact in PHP, an expression is defined as anything
that resolves to a value. This includes integer constants such as 654, variables such
as $user, and function calls such as gettype(). The expression (4 + 5), therefore is
an expression that consists of two further expressions and an operator.
NEW
TERM
An expression is any combination of functions, values, and
operators that resolve to a value. As a rule of thumb, if you can use
it as if it were a value, it is an expression.

Now that we have the principles out of the way, it's time to take a tour of PHP4's
more common operators.

Wednesday, September 29, 2010

Changing Data Type Type by Casting in Php

By placing the name of a data type in brackets in front of a variable, you create a
copy of that variable's value converted to the data type specified. The principal
difference between settype() and a cast is the fact that casting produces a copy,
leaving the original variable untouched. Listing 4.6 illustrates this.

1: <html>
2: <head>
3: <title>Listing 4.6 Casting a variable</title>
4: </head>
5: <body>
6: <?php
7: $undecided = 3.14;
8: $holder = ( double ) $undecided;
9: print gettype( $holder ) ; // double
10: print " -- $holder<br>"; // 3.14
11: $holder = ( string ) $undecided;
12: print gettype( $holder ); // string
13: print " -- $holder<br>"; // 3.14
14: $holder = ( integer ) $undecided;
15: print gettype( $holder ); // integer
16: print " -- $holder<br>"; // 3
17: $holder = ( double ) $undecided;
18: print gettype( $holder ); // double
19: print " -- $holder<br>"; // 3.14
20: $holder = ( boolean ) $undecided;
21: print gettype( $holder ); // boolean
22: print " -- $holder<br>"; // 1
23: ?>
24: </body>
25: </html>
We never actually change the type of $undecided, which remains a double
throughout. In fact, by casting $undecided, we create a copy that is then converted
to the type we specify. This new value is then stored in the variable $holder.
Because we are working with a copy of $undecided, we never discard any
information from it as we did in Listing 4.5.

Changing Data Type with settype() in php

PHP provides the function settype() to change the type of a variable. To use
settype(), you must place the variable to change (and the type to change it to)
between the parentheses and separated by commas. Listing 4.5 converts 3.14 (a
double) to the four types that we are covering in this hour.

1: <html>
2: <head>
3: <title>Listing 4.5 Changing the type of a variable with settype()</title>
4: </head>
5: <body>
6: <?php
7: $undecided = 3.14;
8: print gettype( $undecided ); // double
9: print " -- $undecided<br>"; // 3.14
10: settype( $undecided, string );
11: print gettype( $undecided ); // string
12: print " -- $undecided<br>"; // 3.14
13: settype( $undecided, integer );
14: print gettype( $undecided ); // integer
15: print " -- $undecided<br>"; // 3
16: settype( $undecided, double );
17: print gettype( $undecided ); // double
18: print " -- $undecided<br>"; // 3.0
19: settype( $undecided, boolean );
20: print gettype( $undecided ); // boolean
21: print " -- $undecided<br>"; // 1
22: ?>
23: </body>
24: </html>
In each case, we use gettype() to confirm that the type change worked and then
print the value of the variable $undecided to the browser. When we convert the
string "3.14" to an integer, any information beyond the decimal point is lost forever.
That's why $undecided still contains 3 after we have changed it back to a double.
Finally, we convert $undecided to a boolean. Any number other than 0 becomes true
when converted to a boolean. When printing a boolean in PHP, true is represented
as 1 and false as an empty string, so $undecided is printed as 1.

Data Types in php

Different types of data take up different amounts of memory and may be treated
differently when they are manipulated in a script. Some programming languages
therefore demand that the programmer declare in advance which type of data a
variable will contain. PHP4 is loosely typed, which means that it will calculate data
types as data is assigned to each variable. This is a mixed blessing. On the one hand,
it means that variables can be used flexibly, holding a string at one point and an
integer at another. On the other hand, this can lead to confusion in larger scripts if
you expect a variable to hold one data type when in fact it holds something
completely different.

Type                        Example                    Description
Integer                        5                          A whole number
Double                    3.234                     A floating-point
number
String                     "hello"                  A collection ofcharacters
Boolean                   true                          One of the special values true or false
Object                 "Objects"
Array See                "Arrays"

You can use PHP4's built-in function gettype() to test the type of any variable. If you
place a variable between the parentheses of the function call, gettype() returns a
string representing the relevant type. Listing 4.4 assigns four different data types to
a single variable, testing it with gettype() each time.
Note You can read more about calling functions in Hour 6, "Functions."

1: <html>
2: <head>
3: <title>Listing 4.3 Testing the type of a variable</title>
4: </head>
5: <body>
6: <?php
7: $testing = 5;
8: print gettype( $testing ); // integer
9: print "<br>";
10: $testing = "five";
11: print gettype( $testing ); // string
12: print("<br>");
13: $testing = 5.0;
14: print gettype( $testing ); // double
15: print("<br>");
16: $testing = true;
17: print gettype( $testing ); // boolean
18: print "<br>";
19: ?>
20: </body>
21: </html>
This script produces the following:
integer
string
double
boolean
An integer is a whole or real number. In simple terms, it can be said to be a number
without a decimal point. A string is a collection of characters. When you work with
strings in your scripts, they should always be surrounded by double (") or single (')
quotation marks. A double is a floating-point number. That is, a number that
includes a decimal point. A boolean can be one of two special values, true or false.
Note Prior to PHP4, there was no boolean type. Although true was used, it
actually resolved to the integer 1.

References to Variables in php

By default, variables are assigned by value. In other words, if you were to assign
$aVariable to $anotherVariable, a copy of the value held in $aVariable would be
stored in $anotherVariable. Subsequently changing the value of $aVariable would
have no effect on the contents of $anotherVariable. Listing 4.2 illustrates this.
Listing 4.2: Variables Are Assigned by Value
1: <html>
2: <head>
3: <title>Listing 4.2 Variables are assigned by value</title>
4: </head>
5: <body>
6: <?php
7: $aVariable = 42;
8: $anotherVariable = $aVariable;
9: // a copy of the contents of $aVariable is placed in $anotherVariable
10: $aVariable = 325;
11: print $anotherVariable; // prints 42
12: ?>
13: </body>
14: </html>
This example initializes $aVariable, assigning the value 42 to it. $aVariable is then
assigned to $anotherVariable. A copy of the value of $aVariable is placed in
$anotherVariable. Changing the value of $aVariable to 325 has no effect on the
contents of $anotherVariable. The print statement demonstrates this by outputting
42 to the browser.
In PHP4, you can change this behavior, forcing a reference to $aVariable to be
assigned to $anotherVariable, rather than a copy of its contents. This is illustrated in

1: <html>
2: <head>
3: <title>Listing 4.3 Assigning a variable by reference</title>
4: </head>
5: <body>
6: <?php
7: $aVariable = 42;
8: $anotherVariable = &$aVariable;
9: // a copy of the contents of $aVariable is placed in $anotherVariable
10: $aVariable= 325;
11: print $anotherVariable; // prints 325
12: ?>
13: </body>
14: </html>
We have added only a single character to the code in Listing 4.2. Placing an
ampersand (&) in front of the $aVariable variable ensures that a reference to this
variable, rather than a copy of its contents, is assigned to $anotherVariable. Now
any changes made to $aVariable are seen when accessing $anotherVariable. In
other words, both $aVariable and $anotherVariable now point to the same value.
Because this technique avoids the overhead of copying values from one variable to
another, it can result in a small increase in performance. Unless your script assigns
variables intensively, however, this performance gain will be barely measurable.
Note References to variables were introduced with PHP4.

Monday, September 27, 2010

Dynamic Variables in Php

As you know, you create a variable with a dollar sign followed by a variable name.
Unusually, the variable name can itself be stored in a variable. So, when assigning
a value to a variable
$user = "bob";
is equivalent to
$holder="user";
$$holder = "bob";
The $holder variable contains the string "user", so you can think of $$holder as a
dollar sign followed by the value of $holder. PHP interprets this as $user.
Note You can use a string constant to define a dynamic variable instead of a
variable. To do so, you must wrap the string you want to use for the variable
name in braces:
${"user"} = "bob";
This might not seem useful at first glance. However, by using the
concatenation operator and a loop (see Hour 5, "Going with the Flow"), you
can use this technique to create tens of variables dynamically.
When accessing a dynamic variable, the syntax is exactly the same:
$user ="bob";
print $user;
is equivalent to
$user ="bob";
$holder="user";
print $$holder;
If you want to print a dynamic variable within a string, however, you need to give
the interpreter some help. The following print statement:
$user="bob";
$holder="user";
print "$$holder";
does not print "bob" to the browser as you might expect. Instead it prints the strings
"$" and "user" together to make "$user". When you place a variable within
quotation marks, PHP helpfully inserts its value. In this case, PHP replaces $holder
with the string "user". The first dollar sign is left in place. To make it clear to PHP
that a variable within a string is part of a dynamic variable, you must wrap it in
braces. The print statement in the following fragment:
$user="bob";
$holder="user";
print "${$holder}";
now prints "bob", which is the value contained in $user.
Listing 4.1 brings some of the previous code fragments together into a single script
using a string stored in a variable to initialize and access a variable called $user.


Dynamically Setting and Accessing Variables
1: <html>
2: <head>
3: <title>
Dynamically setting and accessing variables</title>
4: </head>
5: <body>
6: <?php
7: $holder = "user";
8: $$holder = "bob";
9:
10: // could have been:
11: // $user = "bob";
12: // ${"user"} = "bob";
13:
14: print "$user<br>"; // prints "bob"
15: print $$holder; // prints "bob"
16: print "<br>";
17: print "${$holder}<br>"; // prints "bob"
18: print "${'user'}<br>"; // prints "bob"
19: ?>
20: </body>
21: </html>

Variables in Php

A variable is a special container that you can define to "hold" a value. A variable
consists of a name that you can choose, preceded by a dollar ($) sign. The variable
name can include letters, numbers, and the underscore character (_). Variable
names cannot include spaces or characters that are not alphanumeric. The following
code defines some legal variables:
$a;
$a_longish_variable_name;
$2453;
$sleepyZZZZ
Remember that a semicolon (;) is used to end a PHP statement. The semicolons in
the previous fragment of code are not part of the variable names.
NEW TERM
A variable is a holder for a type of data. It can hold numbers, strings of
characters, objects, arrays, or booleans. The contents of a variable can
be changed at any time . 
As you can see, you have plenty of choices about naming, although it is unusual to
see a variable name that consists exclusively of numbers. To declare a variable, you
need only to include it in your script. You usually declare a variable and assign a
value to it in the same statement.
$num1 = 8;
$num2 = 23;
The preceding lines declare two variables, using the assignment operator (=) to
give them values. You will learn about assignment in more detail in the Operators
and Expressions section later in the hour. After you give your variables values, you
can treat them exactly as if they were the values themselves. In other words
print $num1;
is equivalent to
print 8;
as long as $num1 contains 8.