PHP3 Chinese Documents

Source: Internet
Author: User
Tags php tutorial setcookie type casting

Detach from HTML

Here are three ways to detach from HTML while adding "PHP (PHP Tutorial) code Mode"

Example 5-1. How to detach from HTML

1.

2.

3.;

4. <% echo ("As of PHP 3.0.4 optionally use asp-style tags"); %>

How to break a statement

The escape method for statements is the same as C and Perl------Each statement is terminated with a semicolon (;).

End tag (? >) also implies the end of the description, so the following two statements are equivalent:

<php< p= "" >

echo "This is a test";

?>

 

Variable type

PHP supports the following variable types:

Integer: Integers

Double: Dual Precision number

String: Strings

Array: Queue arrays

Object: Objects

Pdfdoc (only if-enabled PDF support): PDF text (only valid after PDF is supported)

Pdfinfo (only if enabled PDF support): PDF information (only valid after the PDF is supported)

The types of these variables are usually not set by the programmer, and, in particular, they are set by PHP in terms of what type to use in the context of the runtime.

If you prefer to force a variable to be converted to the specified type, you can also "cast" the variable, or use the function "Settype ()".

 

Note that these variables may be used by different controllers under certain circumstances, depending on what type of variable it is at this time. For further information, please refer to the "Type Juggling" section.

Initialization of variables

To initialize a variable in PHP, you simply assign it a value. This is very simple for most types, but there are a few different methods for the type array (array) and objects (object) to use.

Initializing an array

An array can be initialized using successive specified values, using the "array ()" structure (which is demonstrated in the "Array function Description section").

 

Adds a continuous value to an array variable. You can simply specify the value of an array variable that is not labeled. These values are added to the last element of the array variable.

 

$names [] = "Jill"; $names [0] = "Jill"

$names [] = "Jack"; $names [1] = "Jack"

 

As in C and Perl, the starting number of an array element is 0 instead of 1.

Initializing an object (object) variable

To initialize the object variable, you use the declaration method shown in the following example:

class Foo {

function Do_foo () {

echo "Doing foo.";

}

}

$bar = new Foo;

$bar-Do_foo ();

Variable range of activity

The scope of the variable's activity is limited to the context in which it is defined. For most parts, all PHP variables have only a separate range of activities. However, in a user-defined function, the concept of a local function scope is introduced. Any variables used in this function are limited by default to the local function scope of the function. For example:

 

$a = 1; /* Global scope */

Function Test () {

echo $a; /* Reference to local scope variable */}

Test ();

 

This script will not cause any output because the variable "$a" that is presented to the local translation is displayed, and the variable does not have a value specified in its range of activity. You may notice that this differs from the C language in that the global variable is automatically set to be available in the C language, unless specifically described in the function. Because people may accidentally change the value of a global variable, this can cause many problems in the program. In PHP, the global variable must be advertised in a function if you want to use it in this function. Examples are as follows:

 

$a = 1;

$b = 2;

Function Sum () {

Global $a, $b;

$b = $a + $b;

}

Sum ();

Echo $b;

 

The above script will output "3". The global variable $ A and $b are declared in the function, and any references to the two variables are assigned to the global variable. There is no limit to the number of global variables that a function can manipulate.

 

The second way to accept global variables is to use PHP's special definition array $globals, as shown in the following example:

$a = 1;

$b = 2;

 

Function Sum () {

$GLOBALS ["b"] = $GLOBALS ["a"] + $GLOBALS ["B"];

}

 

Sum ();

Echo $b;

 

$GLOBALS Array is a union array using "global" as the name of the variable, and the global variable as the value of one of the elements in the variable array.

 

Another important feature about variable range of activity is "static variables". A static variable exists only within the scope of the local function's activity, but when the program leaves the range, its value is not lost. Please refer to the following example:

 

Function Test () {

$a = 0;

echo $a;

$a + +;

}

 

Each time this function is called, the variable $ A is set to 0 while printing "0", so it is almost no effect. The expression "$a + +" will increment the value of the variable, but the variable $ A will disappear each time you exit the function. To use a count function that does not lose the current calculation, the user can set the variable $ A to be static, as shown in the following example:

 

Function Test () {

static $a = 0;

echo $a;

$a + +;

}

 

Now, every time the test () function is called, it prints out the variable $ A and its value after it was added.

 

When a function is called recursively, using a static variable is an important method. A recursive function is a function that can call itself. When writing recursive functions, you must be aware of the cyclic definitions that may occur. You must have an appropriate method to interrupt this recursive process. The following example is recursively 10 times:

 

Function Test () {

static $count = 0;

$count + +;

Echo $count;

if ($count < 10) {

Test (); }

$count--;

}

Change variable

Sometimes it is very convenient to use variable variables. In other words, the name of a variable will be dynamically set and used. An ordinary variable will use the following declaration:

 

$a = "Hello";

 

A change variable gets the value of a variable and sees it as the name of the variable. In the example above, "Hello" can be used with a variable name plus two $ for example.

 

$ $a = "world";

 

At this point, two variables are defined and stored in the PHP symbol tree; the $a is "hello" and the value of $hello is "world". Therefore, the following statement:

 

echo "$a ${$a}";

 

Produced the exact same output as the following:

 

echo "$a $hello";

 

They all output: "Hello World"

 

To use variable variables in an array, you must solve a vague problem. It is: If you write "$ $a [1]", then the resolver will need to know whether you want to use $a[1] as a variable or use $ $a as a variable, so that the index "[1]" may be ambiguous. The syntax for resolving this ambiguity is as follows: "${$a [1]}" or use "${$a}[1]" (for the second case described above).

External PHP variables

HTML forms (Get and post)

When a form is submitted to a PHP script, the variables obtained from the form are automatically set to be available by the PHP script. Please refer to the following example:

 

Example 5-2. Simple form variable (easy form variable)

When committed, PHP will create a variable "$name" that will contain any content entered in the form in "name".

 

PHP also knows how to schedule the context of a form variable, but only once. You can, for example, describe a set of variables at the same time, or use this feature to re-derive values from multiple selection inputs:

 

Example 5-3. More complex form variables

 

If PHP's Track_var feature is turned on, any structure setting or indication of it, then a variable submission via POST or GET mode will find it appropriate to use the Global Union array "$HTTP _post_vars" and "$HTTP _get_vars".

 

Graph commit Variable Name

When submitting a form, it is possible to use an image instead of the standard Tagged submit button, for example:

 

 

When the user clicks anywhere on the image, the corresponding form is routed to the server using two additional variables (sub_x and sub_y). They contain information about where the user clicked the corresponding location on the graph. This will include the name of the real variable from the browser (even the underscore), but PHP will automatically convert it to an underlined form.

 

HTTP Cookies

PHP, of course, supports the use of HTTP Cookies defined by Netscape ' s spec. Cookies are devices that store data on a remote browser and are used to track and authenticate a user's identity. You can use the Setcookie () function to set up cookies. Cookies are part of the HTTP header, so the Setcookie program must be called before it is exported to the browser. This is similar to the header () function setting. Any cookies sent from the server to the user will automatically be converted into a PHP variable, just like data in get and post patterns.

 

If you want to assign multiple values to a single cookie, simply add a "[]" after the name of the cookie. For example:

 

Setcookie ("mycookie[]", "testing", Time () +3600);

 

Note that if the domain or path is not the same on your browser, the cookie will replace the previous cookie with the same name. So, for a shopping order application, you might want to keep a counter that transmits it through a cookie at the same time, for example:

 

Example 5-4. Setcookie Example (example of setting cookies)

$Count + +;

Setcookie ("Count", $Count, Time () +3600);

Setcookie ("cart[$Count]", $item, Time () +3600);

 

Environment variables

PHP automatically uses environment variables as common PHP variables, as shown in the following example.

 

Echo $HOME; /* Shows the HOME environment variable, if set. */

 

Since the information is passed in with mechanisms such as Get,post,cookie, and the PHP variable is automatically created, it is sometimes best to read a variable from the outside environment to make sure you are using the correct version. The getenv () function is the job, and you can use the putenv () function to set an environment variable.

Server Setup Instructions

Variable type change (juggling)

PHP does not need (or is not supported) to explicitly define his variable type in a reputation variable; the type of a variable is determined by the relationship that the variable is used in, that is, if you assign a value of a string to a variable Var, var becomes a string variable. If you assign an integer value to Var, he becomes an integer variable.

An example of a PHP automatic conversion variable type is the operator ' + ' of the addition. If any one operand is a double-precision number, then all operands are evaluated as double-precision numbers, and the result is a double-precision number. Otherwise, the operand will be considered an integer and the result will be an integer. Note that this does not affect the variable type of each operand itself, and the only change is how the operand is handled during the calculation.

$foo = "0"; $foo is a string with a value of "0" (ASCII 48)

$foo + +; $foo is a string with a value of "1" (ASCII 49)

$foo + = 1; $foo is now an integer (2).

$foo = $foo + 1.3; $foo is now a double-precision number (3.3).

$foo = 5 + "Ten Little piggies"; $foo is an integer (15)

$foo = 5 + "Ten Small Pigs"; $foo is an integer (15)

 

If you think the last two expressions in the example above look a bit strange, look at the "Conversion of Strings" section.

If you want to force a variable to be evaluated as a fixed type, see the section "type coercion (casting)". If you want to change the type of a variable, see the description of the function "Settype ()".

 

Determine the type of a variable

Because PHP itself determines the types of variables and generally translates them as needed, the type of a particular variable is not obvious at any time. PHP includes some functions to find out the type of the variable. These functions are GetType (), Is_long (), is_double (), is_string (), Is_array (), and Is_object ().

 

Type coercion (type casting)

Type coercion in PHP is similar in C: Write the kind of type you want in parentheses in front of a strong variable.

$foo = 10; $foo is an integer

$bar = (double) $foo; $bar is a double-precision number

The following enforcement methods are allowed:

(int), (integer) – Force integer

(real), (double), (float) – Force double-precision number

(string) – forced into a string

(array) – Force an array

(object) – coerced into an object

Note that tabs (tabs) and spaces (spaces) are allowed in parentheses, so the following statements are equivalent:

$foo = (int) $bar;

$foo = (int) $bar;

 

String conversions

When a string is evaluated as a numeric value, his result and type are determined as described below.

If the string contains the character '. ', ' e ', or ' e ', it is treated as a double-type variable, otherwise it is considered an integer.

The value of this string is determined by the first part of the word. If the string starts with any valid numeric data, then the numeric data is the value of the string participating in the operation. Otherwise, the value is 0 (zero). Valid numeric data follows these tags, followed by one or more digits (which can contain a decimal point) followed by an optional exponent. An exponent is made up of one or more numbers following the ' e ' or ' e '.

 

$foo = 1 + "10.5"; $foo is the double precision number (11.5)

$foo = 1 + " -1.3e3"; $foo is the double precision number (-1299)

$foo = 1 + "bob-1.3e3"; $foo is an integer (1)

$foo = 1 + "BOB3"; $foo is an integer (1)

$foo = 1 + "Ten Small Pigs"; $foo is an integer (11)

$foo = 1 + "Ten Little piggies"; $foo is an integer (11);

This string includes the character ' E '

For more information, refer to the UNIX manual section on strtod (3).

Array manipulation

PHP supports both scalar arrays and associative arrays (associative arrays). In fact, there is no difference between the two. You can use the function list () or array () to create an array, or you can explicitly set the value of each element of a group.

$a [0] = "abc";

$a [1] = "DEF";

$b ["foo"] = 13;

You can also create an array by adding values to the array.

$a [] = "Hello"; $a [2] = = "Hello"

$a [] = "World"; $a [3] = = "World"

The array can be sorted by function asort (), Arsort (), Ksort (), Rsort (), sort (), Uasort (), Usort (), and Ksort (), using that function depending on the type of sort you want.

You can use the cunt () function to count the number of elements in an array.

You can use the next () and Prev () functions to iterate through an array. Another common way to iterate through an array is to use the function each ().

PHP3 Chinese Documents

Related Article

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.