PHP Getting Started learning notes one _php tutorial

Source: Internet
Author: User
1. Basic Syntax
The way to embed PHP scripts in HTML code is to Write code in. Output data to the browser using the Echo or print function. Echo can accept multiple parameters, and print can accept only one. Echo is in the form of

void Echo (String arg1,[,... string argn]);

PHP syntax allows you to omit parentheses. For example
Copy CodeThe code is as follows:
$my = ' my ';
Echo ' Hello ', $my, ' World '
?>

Will output Hello My world on the browser

PHP also supports a printf function that resembles the C language, such as printf ('%d apples ', 100), which will output the apples. The usage of sprintf is consistent with printf, but instead of outputting to the browser, it returns a string.

2. Data types and Variables

PHP is a weak type, a variable that does not need to be declared beforehand, and does not require a specified type. PHP variables are $ plus variable names, PHP variables are case-sensitive. For example, $my = ' my ' in the previous example.

The types of variables supported by PHP include: boolean, Integer, float, string, array, and object. The first four kinds are very common, also similar to other languages, do not introduce more. Arrays and objects are described in detail later in this article.

PHP has functions to detect the types of objects, which are getttype. GetType returns a string whose value can be array,boolean,double,integer,object,resource,string and unknow type. PHP also supports explicit type conversions, and syntax is similar to C.

Conversion operator to
Arrays (Array)
(BOOL) (Boolean) Boolean type
(int) (integer) Integer
(object) objects
(float), (double), (real) floating-point number
(String) string

For example:
Copy CodeThe code is as follows:
$str = ' A string ';
$num = 15;
$numstr = ' 123.3 ';
Echo GetType ($STR), '
';
Echo GetType ($num), '
';
Echo GetType ($NUMSTR), '
';
$numstr = (float) $numstr;
Echo GetType ($NUMSTR);
?>

The output is:

String
Integer
String
Double

There are also functions that can be used to determine whether a variable is of a certain type, such as Is_array (), Is_bool (), and so on, with similar usage.

3. Function and Variable scope
PHP declares functions in a simple way, in the following form:
Copy CodeThe code is as follows:
function functionname (parameters) {

function body

}

You do not need to specify a return type, and you do not need to specify the variable type in parentheses, as long as you have the variable name. For example:
Copy CodeThe code is as follows:
function Taxedprice ($price, $taxrate) {
Return $price * (1+ $taxrate);
}
Echo taxedprice (100, 0.03);
?>

By default, PHP is passing parameters by value, and changing the value of the parameter within the function does not change the value of the out-of-function variable, but PHP also supports pass-by-reference, syntax and C-consistent,& $paramName, for example, the following classic example:
Copy CodeThe 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, and B is 5
A is 5, and B is 3

PHP's functions also support the default values of parameters, and the syntax is the same as C. For example:
Copy CodeThe code is as follows:
function Taxedprice ($price, $taxrate =0.03) {
Return $price * (1+ $taxrate);
}
echo taxedprice (100);
?>

The scope of variables is described below. PHP's variable scope and C are very similar, there are local variables, function parameters, global variables, static variables 4 kinds. A local variable is a variable declared within a function, which is a variable declared at the first part of the functions, a variable that is not declared in a function is a global variable, and a global variable can be accessed anywhere, but unlike C, if the value of a global variable is to be modified explicitly in the function, it is a global variable. Otherwise, PHP will declare a local variable with the same name and overwrite it. For example:
Copy CodeThe 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 result of the output is:

0.03

1.03

PHP also has a super global variable. Super global variables are predefined by the PHP system and are primarily used to access information about the environment, such as current user sessions, user operating environments, and local environments. A super global variable is an array, such as a $_server that stores information about the server. $_get,$_post,$_files,$_cookie stores the client with GET submission, and POST submission information, uploaded files, cookie information, etc. The use of these variables is simple, and what information is needed to find only

Variables of 4 variables
Unlike static languages such as C, the PHP variable name itself can be a variable, which is handy for the need to dynamically generate many variables. For example:
Copy CodeThe code is as follows:
$r = "Hello";
$ $r = "I am Hello";
Echo $hello;
?>

The output is: I am Hello


5. Process Control Statements
The main include, if else, While,for,do While,switch. These are very similar to the C language and are basically consistent. Not much to do the introduction. Somewhat different, PHP ElseIf is a keyword that is linked together while the C language is else if.

http://www.bkjia.com/PHPjc/322534.html www.bkjia.com true http://www.bkjia.com/PHPjc/322534.html techarticle 1. Basic syntax the way to embed PHP scripts in HTML code is to write code in PHP. Output data to the browser using the Echo or print function. Echo can accept multiple parameters, print can only ...

  • 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.