PHP study notes _ PHP Tutorial

Source: Internet
Author: User
One of the PHP study notes. The process of configuring PHP and MySQL is omitted. for Ubuntu, see Ubuntu10.04InstallationMemo. 1. how does the basic syntax embed a PHP script in HTML code? Php? The process of configuring PHP and MySQL in the Chinese book is omitted. if you are in Ubuntu, refer to Ubuntu 10.04 Installation Memo.

1. Basic syntax

To embed a PHP script in HTML code . The echo or print function is used to output data to the browser. Echo can accept multiple parameters, and print can only accept one. The echo format is
Void echo (string arg1 ,[,... String argn]);
Brackets can be omitted in the PHP syntax. For example,

The code is as follows:


$ My = 'my ';
Echo 'hello', $ my, 'World'
?>


Hello my world will be output in the browser
PHP also supports a printf function similar to the C language. for example, printf ('% d apples', 100) will output 100 apples. Sprintf is used in the same way as printf, but instead of output to the browser, it returns a string.
2. data types and variables
PHP is of a weak type, and a variable does not need to be declared in advance or specified. In PHP, the variable is $ plus the variable name. in PHP, the variable is case sensitive. For example, $ my = 'My in the above example '.
PHP supports the following variable types: Boolean, integer, floating point, string, array, and object. The first four are very common and similar to other languages. Introduction to arrays and objects.
PHP has functions to detect object types. they are getttype. Gettype returns a string whose values can be array, boolean, double, integer, object, resource, string, and unknow type. PHP also supports explicit type conversion. The syntax is similar to that of C.
Conversion operator Convert
(Array) Array
(Bool) (boolean) Boolean
(Int) (integer) Integer
(Object) Object
(Float), (double), (real) Floating point number
(String) String
For example:

The code is as follows:


$ Str = 'A string ';
$ Num = 15;
$ Numstr = '192. 3 ';
Echo gettype ($ str ),'
';
Echo gettype ($ num ),'
';
Echo gettype ($ numstr ),'
';
$ Numstr = (float) $ numstr;
Echo gettype ($ numstr );
?>


Output result:

String
Integer
String
Double

Other functions can be used to determine whether a variable is of a certain type, such as is_array () and is_bool (). The usage is similar.

3. function and variable scope
The PHP method for declaring a function is as follows:

The code is as follows:


Function functionName (parameters ){
Function body
}


You do not need to specify the return type. you do not need to specify the variable type in parentheses as long as there is a variable name. For example:

The code is as follows:


Function taxedPrice ($ price, $ taxrate ){
Return $ price * (1 + $ taxrate );
}
Echo taxedPrice (100, 0.03 );
?>


By default, PHP transmits parameters by value. changing the value of a parameter in a function does not change the value of a variable outside the function. However, PHP also supports passing parameters by reference, the syntax is the same as that of C. & $ paramName. for example, the following is a classic example:

The code is as follows:


Function swap1 ($ x, $ y ){
$ T = $ x; $ x = $ y; $ y = $ t;
}
Function swap2 (& $ x, & $ y ){
$ T = $ x; $ x = $ y; $ y = $ t;
}
$ A = 3; $ B = 5;
Swap1 ($ a, $ B );
Printf ("a is % d, B is % d
", $ A, $ B );
Swap2 ($ a, $ B );
Printf ("a is % d, B is % d
", $ A, $ B );
?>


Output result:

A is 3, B is 5
A is 5, B is 3

PHP functions also support the default parameter values. The syntax is the same as that of C. For example:

The code is as follows:


Function taxedPrice ($ price, $ taxrate = 0.03 ){
Return $ price * (1 + $ taxrate );
}
Echo taxedPrice (100 );
?>


The following describes the scope of a variable. PHP's variable scope is similar to C. There are four types of variables: local variables, function parameters, global variables, and static variables. A local variable is a variable declared in a function. a function parameter is a variable declared in the function header. a variable not declared in a function is a global variable, which can be accessed anywhere, however, unlike C, if you want to modify the value of a GLOBAL variable in a function, you must use the GLOBAL keyword to explicitly specify that it is a GLOBAL variable, otherwise, PHP will declare a local variable with the same name and overwrite it. For example:

The code is as follows:


$ Taxrate = 0.03; // global
Function change1 (){
$ Taxrate + = 1;
}
Function change2 (){
GLOBAL $ taxrate;
$ Taxrate + = 1;
}
Change1 ();
Echo $ taxrate ,'
';
Change2 ();
Echo $ taxrate ,'
';
?>


The output result is:

0.03

1.03

PHP also has a super global variable. Super global variables are predefined by the PHP system and are mainly used to access information related to the environment, such as the current user session, user operation environment, and local environment. A Super global variable is an array. for example, $ _ SERVER stores SERVER-related information. $ _ GET, $ _ POST, $ _ FILES, and $ _ Cookies respectively store the information submitted by the client using get, post, uploaded FILES, and COOKIE information. These variables are easy to use. you only need to find the required information.

4. variable
Unlike the static language C, the PHP variable name itself can be a variable, which is convenient for dynamic generation of many variables. For example:

The code is as follows:


$ R = "hello ";
$ R = "I am hello ";
Echo $ hello;
?>


Output result: I am hello

5. process control statements
It mainly includes, if else, while, for, do while, switch. These are similar to the C language and are basically the same. Not much. In some cases, PHP elseif is a keyword that is connected together, and c is else if.

Unzip Ubuntu 10.04 Installation Memo. 1. how does the basic syntax embed a PHP script in HTML code? Php? Books...

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.