PHP3 Chinese Document (cont. 4)

Source: Internet
Author: User
Tags define arrays definition expression function definition functions variables php code
In order to understand the switch statement in order to avoid the lengthy statement, it is very important to know how it is executed. The switch statement Stone executes on one line (in fact, it is a statement). At the beginning, no code is executed. Only when a case statement with the same value as a statement with a switch expression is found, PHP continues executing the statement until the end of the switch body. Or a break statement appears. If you do not write a break statement after a branch statement, PHP continues to execute the following branch of the statement. For example:

 

/* Example 3 * *

 

Switch ($i) {

Case 0:

print "I equals 0";

Case 1:

Print "I equals 1";

Case 2:

Print "I equals 2";

}

 

Here, if $i equals 0,php, all print statements will be executed. If $i equals 1,php will execute the remaining two print statements, and only if the $i equals 2, you can get the results you expect, only ' I equals 2 ' is displayed. So do not forget the break statements at the back of each branch statement (even if you might want to avoid providing them in a certain environment).

A Special branch is the default branch. This branch can match anything that is not matched by any other branch. For example:

 

/* Example 4 * *

 

Switch ($i) {

Case 0:

print "I equals 0";

Break

Case 1:

Print "I equals 1";

Break

Case 2:

Print "I equals 2";

Break

Default

Print "I am not equal to 0, 1 or 2";

}

 

Another fact worth mentioning is that the case expression can be any expression that evaluates to a scalar type, that is, an integer or a solid and a character. Arrays and objects will not cause PHP to crash, but they don't mean anything.

 

REQUIRE
The Require statement replaces itself with the specified file, and is very similar to the #include statement in the C language. This means that you cannot put a require () statement in a loop body, and expect it to contain the contents of different files in each iteration. For this purpose, you can use the INCLUDE statement.

Require (' header.inc ');



INCLUDE
The include statement includes and evaluates the specified file.

Occurs every time an include statement is encountered. So you can use the Include statement in a loop body. To contain a number of different files.

$files = Array (' First.inc ', ' second.inc ', ' third.inc ');

for ($i = 0; $i < count ($files); $i + +) {

Include ($files [$i]);

}

The INCLUDE () and require () statements are different, the INCLUDE statement is recalculated every time (and only when it is executed), and the Require () statement replaces itself with the specified file, regardless of whether the contents of the file is computed, when the first time he is encountered. (for example, if it appears in an if statement with a condition of false).

Because include () is a special language structure, you must enclose it in a block of syntax if it is in a conditional block.

 

/* error, does not work as intended. */

 

if ($condition)

Include ($file);

Else

Include ($other);

 

/* The following is correct. */

 

if ($condition)

{

Include ($file);

}

else {

Include ($other);

}

 

When a file is computed, parsing will be in the "html-mode", which will output the contents of the file until the first PHP start tag. Be encountered.

Reference ReadFile (), virtual ().

Function

A function (functions) can be defined by using the following syntax

function foo ($arg _1, $arg _2, ..., $arg _n) {

echo "Example function.\n";

return $retval;

}

Any valid PHP code can appear in function (functions), even if it is a function or class definition.

function must be defined before they are referenced.

return value

With some optional return statements, any type of value can be returned, including the object and the generalized table.

function My_sqrt ($num)

{

return $num * $num;

}

Echo my_sqrt (4); Output ' 16 '.

 

Multiple values can be returned, but at the same time the effect is achieved through a generalized table:

function foo ()

{

Return Array (0, 1, 2);

}

List ($zero, $one, $two) = foo ();

Parameters

A parameter table allows you to pass information to a function, which can be a variable or a constant separated by a comma (,).

PHP3 supports the use of values to pass variables (default), passed by invocation, and default parameter values. variable-length parameter lists are not currently supported, but can be implemented by passing arrays.

function Takes_array ($input) {

echo "$input [0] + $input [1] =", $input [0]+ $input [1];

}

Passing by call (passing by reference)

By default, function arguments are passed by value. If you want to allow a function to modify the value of its arguments, you can pass them by calling.

If you want a function parameter to be passed by reference, you can use the pre-function definition to add a symbol (&) to the parameter name:

function foo (& $bar) {

$bar. = ' and something extra. '

}

$str = ' This is a string, ';

Foo ($STR);

Echo $str; The output ' is a string, and something extra. '

If you want to pass arguments to a function that is not defined in this way, you can add a symbol (&) to the parameter name in the function call.

function foo ($bar) {

$bar. = ' and something extra. '

}

$str = ' This is a string, ';

Foo ($STR);

Echo $str; Output ' This is a string, '

Foo (& $str);

Echo $str; The output ' is a string, and something extra. '

Default value
A function can define the default value for a c++-style for scalar parameters.

function Makecoffee ($type = "Cappucino") {

echo "Making a cup of $type. \ n";

}

Echo Makecoffee ();

echo Makecoffee ("espresso");

The output of the above program section is as follows:

Making a cup of Cappucino.

Making a cup of espresso.

The default value must be a constant expression, not a variable or a member of a class.

Note that when you use the default parameters, any defaults should be to the right of any non-default parameters; otherwise, things will not be what you think. Consider the following program segment:

function Makeyogurt ($type = "Acidophilus", $flavour) {

Return "Making a bowl of $type $flavour. \ n";

}

echo Makeyogurt ("raspberry"); will not work as expected

The output from the example above is:

Warning:missing argument 2 in Call to Makeyogurt ()

/usr/local/etc/httpd/htdocs/php3test/functest.html on line 41

Making a bowl of raspberry.

Now, use the following contrast above:

function Makeyogurt ($flavour, $type = "Acidophilus") {

Return "Making a bowl of $type $flavour. \ n";

}

echo Makeyogurt ("raspberry");/normal work

The output of this example is:

Making a bowl of acidophilus raspberry.

Old_function

The Old_function statement allows you to declare a function that uses PHP/FI2 syntax (all you need to do is replace ' function ' with ' old_function ').

This is an disapproved feature and should only be used by PHP/FI2 to PHP3 conversions.

Functions that are defined as Old_function mode cannot be invoked by the internal code of PHP. One of the situations is that you cannot use them in functions like as Usort (), Array_walk (), and register_shutdown_function (). You can use a method of writing an interchange function to invoke a function of the old_function pattern (in a normal PHP3 form).

CLASS

A class is a combination of a variable and a function that uses those variables. The definition class uses the following syntax:

<?phpclass cart {var $items;//items in our shopping cart

ADD $num Articles of $ARTNR to the cart

function Add_item ($ARTNR, $num) {$this->items[$artnr] + = $num;}

Take $num Articles of $artnr out of the cart

function Remove_item ($ARTNR, $num) {

if ($this->items[$artnr] > $num) {

$this->items[$artnr]-= $num;

return true;

} else {

return false;

}

}

}

?>

 

As shown above, a class with a name of cart is defined. This class consists of an array of descriptive items and functions that add items and delete items.

 

Class is a type, which means that it is the design blueprint for the actual variable. You can build a variable group and some new actions against them based on the design. Examples are as follows:

 

$cart = new Cart;

$cart->add_item ("10", 1);

 

As shown above, an object $cart of type class cart is established. The function in this object, Add_item (), is called to add an item that has an item number of 10.

 

Class can be extended using another class. This extended or inherited class (class) has all the variables and functions of the basic class and you can add your own extension definitions. To define a keyword that needs to be defined using an extension.

 

Class Named_cart extends Cart {

var $owner;

function Set_owner ($name) {

$this->owner = $name;

}

}

 

The above defines the class (class) named Named_cart, which has all the variables and functions contained in class cart, and adds a variable $owner and a function Set_owner (). You can create a named cart and get the owner name of the cart. You can also use the normal functions that belong to class cart in class Named_cart.

 

$ncart = new Named_cart; Create a named Cart

$ncart->set_owner ("Kris"); Name that Cart

Print $ncart->owner; Print the CART owners name

$ncart->add_item ("10", 1); (Inherited functionality from cart)

 

The variable $this represents this object in the function of this class. In the current object, you can use $this->something to access any variables and functions.

 

When you create a new class, there is a constructor function that will be invoked automatically. If the name of a function is the same as the name of the class, then it becomes a constructor:

 

Class Auto_cart extends Cart {

function Auto_cart () {

$this->add_item ("10", 1);

}

}

 

In the example above, defines a class named Auto_cart, which adds a constructor function to the original class cart, which initializes the class cart by adding a project with an item number of 10 each time a class is set up. " The constructor can also display some information that can be randomly selected, which makes them very useful,

 

Class Constructor_cart {

function Constructor_cart ($item = "ten", $num = 1) {

$this->add_item ($item, $num);

}

}

Shop the same old boring stuff

$default _cart = new Constructor_cart;

Shop for Real ...

$different _cart = new Constructor_cart ("20", 17);





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.