PHP学习笔记

PHP学习笔记(暂时先不更了…)

PHP Basic Rules

PHP文件默认的文件拓展名.php

The first PHP examle

1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html>
<html>
<body>

<h1>My first PHP page</h1>

<?php
echo "Hello World!";
?>

</body>
</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
2
3
4
5
<?php
$text="Hello world!";
$a=1;
$b=2.5;
?>

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
2
3
4
5
6
7
8
9
10
11
12
13
<?php
$x = 1;
$y = 2;

function sumOfTwo()
{
global $x, $y;
$x = $x + $y
}

sumOfTwo();
echo $x;
?>

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

  1. multiple parameter

    1
    2
    3
    4
    <?php
    echo "Hello world!<br>";
    echo "This is ", "a string,", "which use ", "multiple ", "parameter.";
    ?>
  2. With variables

    1
    2
    3
    echo "在 $txt2 学习 PHP ";
    echo "<br>";
    echo "我车的品牌是 {$cars[0]}";

Difference between echo and print

  • echo - 可以输出一个或多个字符串
  • print - 只允许输出一个字符串,返回值总为 1
  • 其他方面相似,都可以输出with variables

Tips:echo is faster than printecho 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
2
3
<?php
$cars=array("Aito","BMW");
?>
Object
1
2
3
4
5
6
7
8
9
10
11
12
<?php
class Car
{
var $color;
function __construct($color="green"){
$this->color = $color;
}
function what_color(){
return $this->color;
}
}
?>
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
2
3
4
5
<?php
$txt1="Hello world!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?>

. in PHP is similar to + in C++(string)

Other string functions:

  1. strlen(), return the length of string
  2. strpos($s1, $s2), return the $s2 ‘s position in $s1

(String subscripts start at 0)

Structured programming

operator

  • +,-,*,/,%,~ is like C
  • intdiv(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
2
3
4
5
6
7
8
9
10
11
12
<?php
$t=date("H");
if ($t<"10") {
echo "Have a good morning!";
}
elseif ($t<"20") {
echo "Have a good day!";
}
else {
echo "Have a good night!";
}
?>

For、While

For
1
2
3
4
5
6
<?php
for ($i = 1; $i <= 10; $i++)
{
echo "数字为 " . $i . PHP_EOL;
}
?>
For each

Format 1:foreach ($array as $value){}

1
2
3
4
5
6
7
<?php
$x = array("Google","Baidu","Bing");
foreach ($x as $value) // x is the name of array
{
echo $value . PHP_EOL;
}
?>

Format 2:foreach ($array as $key => $value){}

1
2
3
4
5
6
7
<?php
$x = array(1=>"Google", 2=>"Baidu", 3=>"Bing");
foreach ($x as $key => $value)
{
echo "key 为 " . $key . ",对应的 value 为 ". $value . PHP_EOL;
}
?>
While/Do While

In php, while and do while are the same as that in C/C++.

Format:while(condition){// some codes}

Array

1
2
3
4
<?php
$s = array("aaa", "bbb", "abc");
echo count($s); // count() return the length of array
?>

Key-value:

1
2
3
4
<?php
$age = array("Mike" => "35", "Amy"=>"37");
echo "Mike is ". $age['Mike'];
?>

sort

example:

1
2
3
4
<?php
$s = array("aaa", "bbb", "abc");
sort($s);
?>

说明:

  • sort():对数组升序排列
  • rsort(): 对数组降序排列
  • ksort() : 根据关联数组的键,对数组升序排列
  • asort() :根据关联数组的值,对数组升序排列
  • krsort():根据关联数组的键,对数组降序排列
  • arsort() :根据关联数组的值,对数组降序排列

Function

Format

1
2
3
4
5
6
7
<?php
function functionName($a, $b)
{
$total = $a + $b
return $total;
}
?>

php function uses return to return value