Home  >  Article  >  Backend Development  >  PHP study notes one_PHP tutorial

PHP study notes one_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:32:31819browse

配置PHP以及MySQL的过程略去,如果在Ubuntu下,参考 Ubuntu 10.04 Installation Memo 即可。

1. 基本语法

要在HTML代码中嵌入PHP脚本的方法是在中书写代码。向浏览器输出数据采用echo或者print函数。echo可以接受多个参数,print 只能接受一个。echo的形式是
void echo(string arg1,[,…string argn]);
PHP语法允许省略括号。 例如,

复制代码 代码如下:

$my =' my ';
echo 'Hello',$my,'world'
?>

将会在浏览器上输出 Hello my world
PHP 还支持一个和C语言很像的printf函数,例如 printf(‘ %d apples',100),将会输出 100 apples。sprintf的用法和printf一致,不过它不是输出到浏览器,而是返回一个字符串。
2.数据类型和变量
PHP是弱类型的,一个变量不需要预先声明,也不需要指定类型。PHP中变量是$加上变量名,PHP的变量是区分大小写的。 例如上例中的 $my='my'。
PHP支持的变量类型包括:布尔型,整型,浮点型,字符串,数组和对象。前四种很常用,也和其他语言类似,不多做介绍。数组和对象后文具体介绍。
PHP中有函数来检测 对象的类型,它们是 getttype。gettype返回一个string,它的值可以是array,boolean,double,integer,object,resource,string 和 unknow type. PHP也支持显式的类型转换,语法和C类似。
转换操作符 转换为
(array) 数组
(bool) (boolean) 布尔型
(int) (integer) 整数
(object) 对象
(float),(double),(real) 浮点数
(string) 字符串
For example:
Copy code The 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 result is:

string
integer
string
double

There are also functions that can be used to determine whether a variable is It is not a certain type, such as is_array(), is_bool(), etc., the usage is similar.

3. Function and variable scope
The method of declaring functions in PHP is very simple, in the following form:
Copy code The code is as follows:

function functionName(parameters){
function body
}

There is no need to specify the return type, nor in parentheses Specify the variable type, as long as there is a variable name. For example:
Copy code The code is as follows:

function taxedPrice($price,$taxrate) {
return $price*(1+$taxrate);
}
echo taxedPrice(100, 0.03);
?>

By default, PHP Parameters are passed by value. Changing the value of the parameter within the function will not change the value of the variable outside the function. However, PHP also supports passing by reference. The syntax is consistent with C, &$paramName. For example, the following is a classic example:
Copy code 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
?>

Output result:

a is 3, b is 5
a is 5, b is 3

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

function taxedPrice($price,$taxrate= 0.03){
return $price*(1+$taxrate);
}
echo taxedPrice(100);
?>

The following describes the role of variables domain. PHP's variable scope is very similar to C. There are four types: local variables, function parameters, global variables, and static variables. Local variables are variables declared within the function, and function parameters are variables declared at the beginning of the function; variables not declared in the function are global variables, and global variables can be accessed anywhere, but unlike C, if they are in a function To modify the value of a global variable, you need to explicitly specify it as a global variable using the GLOBAL keyword, otherwise PHP will declare a local variable with the same name and overwrite it. For example:
Copy code 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 environment-related information, such as the current user session, user operating environment and local environment. The super global variable is an array. For example, $_SERVER stores server-related information. $_GET, $_POST, $_FILES, and $_COOKIE respectively store information submitted by the client using get and post, uploaded files, cookie information, etc. The use of these variables is very simple. If you need any information, just look for

4. Variables of variables
are different from static languages ​​such as C. The variable name of PHP itself can be a Variables, which are very convenient when many variables need to be generated dynamically. For example:
Copy code The code is as follows:

$r="hello";
$$r="I am hello";
echo $hello;
?>

The output result is: I am hello

5. Flow control statements
mainly include, if else, while, for, do while, switch. These are similar to C language Very similar, basically the same. Not much introduction. There are some differences. PHP's elseif is a keyword that is connected together, while C language is elseif.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322820.htmlTechArticleThe process of configuring PHP and MySQL is omitted. If you are under Ubuntu, please refer to Ubuntu 10.04 Installation Memo. 1. Basic syntax The way to embed a PHP script in HTML code is to write it in ?php?...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn