PHP学习笔记
PHP学习笔记(暂时先不更了…)
PHP Basic Rules
PHP文件默认的文件拓展名.php
The first PHP examle
1 | <!DOCTYPE html> |
The function is to print “Hello World!” to Browser.Each line ends with
;
annotate(注释)
Like C, PHP have two ways to annotate.
- 单行注释,use
//
or#
- 多行注释,use
/* */
Variables
- in PHP, variable must begin with
$
- Variable names are case-sensitive(
$a
and$A
are different variables) - Other rules of name are the same as C language
1 |
|
We notice that PHP is Implicit type conversion(弱类型定义语言)
PHP 有四种不同的变量作用域:
- local
- global
- static
- parameter
If we want to visit global variables in function, we must use global
1 |
|
PHP stores all global variable in an array named $GLOBALS[index],
index
is the name of variable. It can be visited in function.
When a function completes, it’s all variables will be delete. However, sometimes you hope the variable can be saved. To do it, you can use static
when declaring the variable.
1 | static $x=0; |
$x is still a local variable!
Echo
multiple parameter
1
2
3
4
echo "Hello world!<br>";
echo "This is ", "a string,", "which use ", "multiple ", "parameter.";With variables
1
2
3echo "在 $txt2 学习 PHP ";
echo "<br>";
echo "我车的品牌是 {$cars[0]}";
Difference between echo
and print
- echo - 可以输出一个或多个字符串
- print - 只允许输出一个字符串,返回值总为 1
- 其他方面相似,都可以输出with variables
Tips:echo
is faster than print
, echo
have no return value ,print
return 1。
Data Type
- String -> We can put any word in
''
or""
- Integer -> 十六进制( 以 0x 为前缀)或八进制(前缀为 0)
- Float
- Boolean(
true
,false
) - Array
- Object(对象)
- NULL(空值)
- Resource(资源类型)
Array
1 |
|
Object
1 |
|
Resource
resource is a special variable, which stores a refrence to outer resource
(link to database、open the file…)
Comparison of data
- Loose comparison:use
==
, only compare the value of variables - Strict comparison:use
===
,not only value,but also type。
字符串在进行比较时,会转化成数字进行数学比较!
Const
usedefine()
:
1 | bool define ( string $name , mixed $value [, bool $case_insensitive = false ] ) |
three parameter:
- name
- value
- case_insensitive :optional. If case_insensitive is TRUE,The constant is not case sensitive. The default is case-sensitive.
Example:
1 | define("BAIDU", "www.baidu.com"); |
String
并置运算符.
1 |
|
.
in PHP is similar to+
in C++(string)
Other string functions:
strlen()
, return the length of stringstrpos($s1, $s2)
, return the$s2
‘s position in$s1
(String subscripts start at 0)
Structured programming
operator
+
,-
,*
,/
,%
,~
is like Cintdiv(10, 3) === 3
we can use
x += y
,x++
- both
!=
and<>
is legal x!==y
means 如果 x 不等于 y,或它们类型不相同,则返回 true- both
and
and&&
is legal(优先级不同) <<
,>>
If-else, switch
In php, if-else
and switch
are the same as that in C/C++.
The only difference is php provide elseif
.
Example:
1 |
|
For、While
For
1 |
|
For each
Format 1:foreach ($array as $value){}
1 |
|
Format 2:foreach ($array as $key => $value){}
1 |
|
While/Do While
In php, while
and do while
are the same as that in C/C++.
Format:while(condition){// some codes}
Array
1 |
|
Key-value:
1 |
|
sort
example:
1 |
|
说明:
sort()
:对数组升序排列rsort()
: 对数组降序排列ksort()
: 根据关联数组的键,对数组升序排列asort()
:根据关联数组的值,对数组升序排列krsort()
:根据关联数组的键,对数组降序排列arsort()
:根据关联数组的值,对数组降序排列
Function
Format
1 |
|
php function uses return
to return value