The concatenation operator is a single dot. Treating both operands as strings, it
appends the right-hand operand to the left. So
"hello"." world"
returns
"hello world"
Regardless of the data types of the operands, they are treated as strings, and the
result always is a string.
Php is the most popular script language worldwide.Learn php script language in this site. Thousands of real life solutions and source code of php in this site.
Showing posts with label Php. Show all posts
Showing posts with label Php. Show all posts
Friday, October 1, 2010
Arithmetic Operators in Php
The arithmetic operators do exactly what you would expect. Table 4.2 lists these
operators. The addition operator adds the right operand to the left operand. The
subtraction operator subtracts the right-hand operand from the left. The division
operator divides the left-hand operand by the right. The multiplication operator
multiplies the left-hand operand by the right. The modulus operator returns the
remainder of the left operand divided by the right.
Operator Name Example Example Result
+ Addition 10+3 13
− Subtraction 10− 3 7
/ Division 10/3 3.3333333333333
* Multiplication 10*3 30
% Modulus 10%3 1
operators. The addition operator adds the right operand to the left operand. The
subtraction operator subtracts the right-hand operand from the left. The division
operator divides the left-hand operand by the right. The multiplication operator
multiplies the left-hand operand by the right. The modulus operator returns the
remainder of the left operand divided by the right.
Operator Name Example Example Result
+ Addition 10+3 13
− Subtraction 10− 3 7
/ Division 10/3 3.3333333333333
* Multiplication 10*3 30
% Modulus 10%3 1
The Assignment Operator in Php
You have met the assignment operator each time we have initialized a variable. It
consists of the single character =. The assignment operator takes the value of its
right-hand operand and assigns it to its left-hand operand:
$name ="matt";
The variable $name now contains the string "matt". Interestingly, this construct is
an expression. It might look at first glance that the assignment operator simply
changes the variable $name without producing a value, but in fact, a statement that
uses the assignment operator always resolves to a copy of the value of the right
operand. Thus
print ( $name = "matt" );
prints the string "matt" to the browser in addition to assigning "matt" to $name.
Arithmetic Operators
consists of the single character =. The assignment operator takes the value of its
right-hand operand and assigns it to its left-hand operand:
$name ="matt";
The variable $name now contains the string "matt". Interestingly, this construct is
an expression. It might look at first glance that the assignment operator simply
changes the variable $name without producing a value, but in fact, a statement that
uses the assignment operator always resolves to a copy of the value of the right
operand. Thus
print ( $name = "matt" );
prints the string "matt" to the browser in addition to assigning "matt" to $name.
Arithmetic Operators
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.
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: ?>
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.
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>
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.
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
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"
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;
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.
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>
$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.
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";
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.
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>
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: ?>
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>
21: </html>
Adding Comments to PHP Code
Code that seems clear at the time of writing, can seem like a hopeless tangle when
you come to amend it six months later. Adding comments to your code as you write
can save you time later on and make it easier for other programmers to work with
your code.
NEW TERM
A comment is text in a script that is ignored by the interpreter.
Comments can be used to make code more readable, or to annotate a
script.
Single line comments begin with two forward slashes (/ /) or a single hash sign (#).
All text from either of these marks until either the end of the line or the PHP close tag
is ignored.
// this is a comment
# this is another comment
Multiline comments begin with a forward slash followed by an asterisk (/*) and end
with an asterisk followed by a forward slash (*/).
/*
this is a comment
none of this will
be parsed by the
interpreter
*/
you come to amend it six months later. Adding comments to your code as you write
can save you time later on and make it easier for other programmers to work with
your code.
NEW TERM
A comment is text in a script that is ignored by the interpreter.
Comments can be used to make code more readable, or to annotate a
script.
Single line comments begin with two forward slashes (/ /) or a single hash sign (#).
All text from either of these marks until either the end of the line or the PHP close tag
is ignored.
// this is a comment
# this is another comment
Multiline comments begin with a forward slash followed by an asterisk (/*) and end
with an asterisk followed by a forward slash (*/).
/*
this is a comment
none of this will
be parsed by the
interpreter
*/
Combining HTML and PHP
The script in Listing 3.1 is pure PHP. You can incorporate this into an HTML
document simply by adding HTML outside the PHP start and end tags, as shown in
1: <html>
2: <head>
document simply by adding HTML outside the PHP start and end tags, as shown in
1: <html>
2: <head>
3: <title>Listing 3.2 A PHP script including HTML</title>
4: </head>
5: <body>
6: <b>
7: <?php
8: print "hello world";
9: ?>
10: </b>
11: </body>
12: </html>
As you can see, incorporating HTML into a PHP document is simply a matter of
typing in the code. The PHP interpreter ignores everything outside PHP open and
close tags. If you were to view Listing 3.2 with a browser, as shown in Figure 3.4,
you would see the string "hello world" in bold. If you were to view the document
source, as shown in Figure 3.5, the listing would look exactly like a normal HTML
document.
You can include as many blocks of PHP code as you need in a single document,
interspersing them with HTML as required. Although you can have multiple blocks of
code in a single document, they combine to form a single script. Anything defined in
the first block (variables, functions, or classes, for example) usually will be available
to subsequent blocks.
4: </head>
5: <body>
6: <b>
7: <?php
8: print "hello world";
9: ?>
10: </b>
11: </body>
12: </html>
As you can see, incorporating HTML into a PHP document is simply a matter of
typing in the code. The PHP interpreter ignores everything outside PHP open and
close tags. If you were to view Listing 3.2 with a browser, as shown in Figure 3.4,
you would see the string "hello world" in bold. If you were to view the document
source, as shown in Figure 3.5, the listing would look exactly like a normal HTML
document.
You can include as many blocks of PHP code as you need in a single document,
interspersing them with HTML as required. Although you can have multiple blocks of
code in a single document, they combine to form a single script. Anything defined in
the first block (variables, functions, or classes, for example) usually will be available
to subsequent blocks.
Beginning and Ending a Block of PHP Statements
When writing PHP, you need to inform the interpreter that you want it to execute
your commands. If you don't do this, the code you write will be mistaken for HTML
and will be output to the browser. Table 3.1 shows the four ways of enclosing PHP
code.
your commands. If you don't do this, the code you write will be mistaken for HTML
and will be output to the browser. Table 3.1 shows the four ways of enclosing PHP
code.
PHP Start and End Tags
Tag Style Start Tag End Tag
Standard tags <?php ?>
Short tags <? ?>
ASP tags <% %>
Script tags <SCRIPTLANGUAGE="php"> </script>
only the standard and the script tags can be guaranteed to
work on any configuration. The short and ASP style tags must be explicitly enabled
in your php.ini. You examined the php.ini file in Hour 2.
To activate recognition for short tags, you must make sure that the short_open_tag
switch is set to "On" in php.ini:
short_open_tag = On;
Short tags are enabled by default, so you would only need to edit php.ini if you want
to disable these.
To activate recognition for the ASP style tags, you must enable the asp_tags
setting:
asp_tags = On;
After you have edited php.ini, you should be able to choose from any of the four
styles for use in your scripts. This is largely a matter of preference, although if you
intend to work with XML, you should disable the short tags (<? ?>) and work with
the standard tags (<?php ?>).
Let's run through some of the ways in which you can legally write the code in Listing
3.1. You could use any of the four PHP start and end tags that you have seen:
<?
print("Hello Web!");
?>
<?php
print("Hello Web!");
?>
<%
print("Hello Web!");
%>
<SCRIPT LANGUAGE="php">
print("Hello Web!");
</SCRIPT>
Single lines of code in PHP also can be presented on the same line as the PHP start
and end tags: <? print("Hello Web!"); ?>
Now that you know how to define a block of PHP code, take a closer look at the code
in Listing 3.1 itself.
Your First Php Script
Let's jump straight in with a PHP script. To begin, open your favorite text editor. Like
HTML documents, PHP files are made up of plain text. You can create them with any
text editor, such as Notepad on Windows, Simple Text and BBEdit on MacOS, or VI
and Emacs on UNIX operating systems. Most popular HTML editors provide at least
some support for PHP.
Type in the example in Listing 3.1 and save the file, calling it something like
first.php.
1: <?php
2: print "Hello Web!";
3: ?>
The extension to the PHP document is important because it tells the server to treat
the file as PHP code and invoke the interpreter. The default PHP extension for a PHP
4 document is .php. This can be changed, however, by altering the server's
configuration. You saw how to do this in Hour 2, "Installing PHP."
If you are not working directly on the machine that will be serving your PHP script,
you will probably need to use an FTP client, such as WS-FTP for Windows or Fetch for
MacOS to upload your saved document to the server.
After the document is in place, you should be able to access it via your browser. If
all has gone well, you should see the script's output. If PHP is not installed on your server or your file's extension is not recognized, you
may not see the output shown in Figure 3.2. In these cases, you probably will see
the source code created in Listing 3.1. Figure 3.3 shows what happens when an
unknown extension is encountered.If this happens, first check the extension with which you saved your PHP script. In
Figure 3.3, the document was accidentally called first.nphp. If the file extension is
as it should be, you may need to check that PHP has been installed properly and that
your server is configured to work with the extension that you have used for your
script. You can read more about installing and configuring PHP in Hour 2.
Now that you have uploaded and tested your script, you can take a look at the code
in a little more detail.
HTML documents, PHP files are made up of plain text. You can create them with any
text editor, such as Notepad on Windows, Simple Text and BBEdit on MacOS, or VI
and Emacs on UNIX operating systems. Most popular HTML editors provide at least
some support for PHP.
Type in the example in Listing 3.1 and save the file, calling it something like
first.php.
1: <?php
2: print "Hello Web!";
3: ?>
The extension to the PHP document is important because it tells the server to treat
the file as PHP code and invoke the interpreter. The default PHP extension for a PHP
4 document is .php. This can be changed, however, by altering the server's
configuration. You saw how to do this in Hour 2, "Installing PHP."
If you are not working directly on the machine that will be serving your PHP script,
you will probably need to use an FTP client, such as WS-FTP for Windows or Fetch for
MacOS to upload your saved document to the server.
After the document is in place, you should be able to access it via your browser. If
all has gone well, you should see the script's output. If PHP is not installed on your server or your file's extension is not recognized, you
may not see the output shown in Figure 3.2. In these cases, you probably will see
the source code created in Listing 3.1. Figure 3.3 shows what happens when an
unknown extension is encountered.If this happens, first check the extension with which you saved your PHP script. In
Figure 3.3, the document was accidentally called first.nphp. If the file extension is
as it should be, you may need to check that PHP has been installed properly and that
your server is configured to work with the extension that you have used for your
script. You can read more about installing and configuring PHP in Hour 2.
Now that you have uploaded and tested your script, you can take a look at the code
in a little more detail.
Platforms, Servers, Databases, and PHP
PHP is truly cross-platform. It runs on the Windows operating system, most versions
of UNIX including Linux, and even the Macintosh. Support is provided for a range of
Web servers including Apache (itself open source and cross-platform), Microsoft
Internet Information Server, WebSite Pro, the iPlanet Web Server, and Microsoft's
Personal Web Server. The latter is useful if you want to test your scripts offline on a
Windows machine, although Apache can also be run on Windows.
You can also compile PHP as a standalone application. You can then call it from the
command line. In this book, we will concentrate on building Web applications, but
do not underestimate the power of PHP4 as a general scripting tool comparable to
Perl.
PHP is designed to integrate easily with databases. This feature is one of the factors
that make the language such a good choice for building sophisticated Web
applications. Many databases are directly supported, including Adabas D, InterBase,
Solid, dBASE, mSQL, Sybase, Empress, MySQL, Velocis, FilePro, Oracle, UNIX dbm,
Informix, and PostgreSQL. PHP also supports ODBC.
of UNIX including Linux, and even the Macintosh. Support is provided for a range of
Web servers including Apache (itself open source and cross-platform), Microsoft
Internet Information Server, WebSite Pro, the iPlanet Web Server, and Microsoft's
Personal Web Server. The latter is useful if you want to test your scripts offline on a
Windows machine, although Apache can also be run on Windows.
You can also compile PHP as a standalone application. You can then call it from the
command line. In this book, we will concentrate on building Web applications, but
do not underestimate the power of PHP4 as a general scripting tool comparable to
Perl.
PHP is designed to integrate easily with databases. This feature is one of the factors
that make the language such a good choice for building sophisticated Web
applications. Many databases are directly supported, including Adabas D, InterBase,
Solid, dBASE, mSQL, Sybase, Empress, MySQL, Velocis, FilePro, Oracle, UNIX dbm,
Informix, and PostgreSQL. PHP also supports ODBC.
we will be using a combination of Linux, Apache, and MySQL.
All these are free to download and use, and can be installed relatively easily on a PC.
You can find out more about getting Linux for your computer at
<http://www.linux.org/help/beginner/distributions.html>. If you want to run Linux
on a Power PC, you can find information about LinuxPPC at
<http://www.linuxppc.org>.
MySQL, the database we will use, can be downloaded from
<http://www.mysql.com>. There are versions for many operating systems
including UNIX, Windows, and OS/2.
On the other hand, you can easily stick with Windows, NT, or MacOS. PHP is, after
all, a cross-platform scripting language.
All these are free to download and use, and can be installed relatively easily on a PC.
You can find out more about getting Linux for your computer at
<http://www.linux.org/help/beginner/distributions.html>. If you want to run Linux
on a Power PC, you can find information about LinuxPPC at
<http://www.linuxppc.org>.
MySQL, the database we will use, can be downloaded from
<http://www.mysql.com>. There are versions for many operating systems
including UNIX, Windows, and OS/2.
On the other hand, you can easily stick with Windows, NT, or MacOS. PHP is, after
all, a cross-platform scripting language.
The Zend Engine in Php
When PHP3 was written, an entirely new parser was created from the ground up.
PHP4 represents a similar change to the scripting engine. This rewrite, though, is
more significant by orders of magnitude.Zend is a scripting engine that sits below the PHP-specific modules. It is optimized
to significantly improve performance.
These changes in efficiency will ensure PHP4's continued success. Most code written
for PHP3 will continue to run with no changes; however, these scripts may run up to
200 times faster!
A commercial addition to the Zend engine will be the facility for compiling PHP
scripts. This will provide a further gain in performance that should leave most, if not
all, competitors far behind.
Zend is built to improve performance but is also designed for increased flexibility.
Communication with servers has been improved, so it will be possible to create PHP
modules that work with a wider range of servers. Unlike a CGI interpreter, which sits
outside a server and is initialized every time a script is run, a server module runs in
conjunction with the server. This improves performance because the scripting
engine does not need to be started for a PHP page to be executed.
PHP4 represents a similar change to the scripting engine. This rewrite, though, is
more significant by orders of magnitude.Zend is a scripting engine that sits below the PHP-specific modules. It is optimized
to significantly improve performance.
These changes in efficiency will ensure PHP4's continued success. Most code written
for PHP3 will continue to run with no changes; however, these scripts may run up to
200 times faster!
A commercial addition to the Zend engine will be the facility for compiling PHP
scripts. This will provide a further gain in performance that should leave most, if not
all, competitors far behind.
Zend is built to improve performance but is also designed for increased flexibility.
Communication with servers has been improved, so it will be possible to create PHP
modules that work with a wider range of servers. Unlike a CGI interpreter, which sits
outside a server and is initialized every time a script is run, a server module runs in
conjunction with the server. This improves performance because the scripting
engine does not need to be started for a PHP page to be executed.
Subscribe to:
Posts (Atom)