One of PHP learning notes _php Tips

Source: Internet
Author: User
Tags php and mysql variable scope
The process of configuring PHP and MySQL is omitted, if under Ubuntu, refer to Ubuntu 10.04 installation Memo.

1. Basic grammar

The way to embed PHP scripts in HTML code is to write the code in <?php?>. 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 form of ECHO is
void Echo (String arg1,[,... string argn]);
The PHP syntax allows you to omit parentheses. For example
Copy Code code as follows:

<?php
$my = ' my ';
Echo ' Hello ', $my, ' World '
?>

will be exported on the browser Hello my World
PHP also supports a printf function like 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 weakly typed, a variable does not need to be declared in advance, and no type needs to be specified. Variables in PHP are $ plus variable names, and PHP variables are case-sensitive. For example, the $my in the previous example = ' my '.
The types of variables supported by PHP include: boolean, Integer, floating-point, string, array, and object. The first four kinds are very common, and similar to other languages, do not do more introduction. 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 operators Converted to
(array) Array
(BOOL) (Boolean) Boolean type
(int) (integer) Integer
(object) Object
(float), (double), (real) Floating point numbers
(string) String
For example:
Copy Code code as follows:

<?php
$str = ' A string ';
$num = 15;
$numstr = ' 123.3 ';
Echo GetType ($STR), ' <br/> ';
Echo GetType ($num), ' <br/> ';
Echo GetType ($NUMSTR), ' <br/> ';
$numstr = (float) $numstr;
Echo GetType ($NUMSTR);
?>

The output results are:

String
Integer
String
Double

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

3. Function and variable scopes
The way PHP declares a function is simple, in the form of the following:
Copy Code code 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 a variable name. For example:
Copy Code code as follows:

<?php
function Taxedprice ($price, $taxrate) {
Return $price * (1+ $taxrate);
}
Echo taxedprice (100, 0.03);
?>

By default, PHP is passed the parameter by value, changing the value of the parameter within the function does not change the value of the function external variable, but PHP also supports passing by reference, syntax and C consistent,& $paramName, for example, the following is a classic example:
Copy Code code as follows:

<?php
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 <br/>", $a, $b);
SWAP2 ($a, $b);
printf ("A is%d, B is%d <br/>", $a, $b);
?>

Output results:

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

PHP functions also support the default values of parameters, syntax and C is the same. For example:
Copy Code code as follows:

<?php
function Taxedprice ($price, $taxrate =0.03) {
Return $price * (1+ $taxrate);
}
echo taxedprice (100);
?>

The scope of the variable 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 of the functions; A variable 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 in a function needs to be explicitly specified with the Global keyword, it is a globally variable. Otherwise, PHP declares a local variable with the same name and overwrites it. For example:
Copy Code code as follows:

<?php
$taxrate = 0.03; Global
function Change1 () {
$taxrate +=1;
}
function Change2 () {
GLOBAL $taxrate;
$taxrate +=1;
}
Change1 ();
echo $taxrate, ' <br/> ';
Change2 ();
echo $taxrate, ' <br/> ';
?>

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 to access information about the environment, such as the current user session, the user's operating environment, and the local environment. A super global variable is an array, such as a server-related information stored in $_server. $_get,$_post,$_files,$_cookie is stored in the client with get submit, and POST the submission of information, uploaded files, cookie information and so on. The use of these variables is simple, what information is needed only to find

variable of 4 variables
Unlike static languages such as C, PHP's variable name itself can be a variable, which is convenient when you need to dynamically generate many variables. For example:
Copy Code code as follows:

<?php
$r = "Hello";
$ $r = "I am Hello";
Echo $hello;
?>

The output is: I am Hello

5. Process Control Statements
Mainly include, if else, While,for,do While,switch. These and C language are very similar, basically is consistent. Do not introduce more. Somewhat differently, PHP's elseif is a keyword that is linked together, while the C language is else if.

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.