快速了解PHP和PHP Web开发的笔记。
PHP Intro
PHP Syntax
PHP脚本在服务器端执行,返回HTML结果至浏览器。PHP脚本开始于<?php
,结束于?>
,
1 |
|
PHP中的关键字 (e.g. if
, echo
, etc) 大小写不敏感,但是变量名大小写敏感。
PHP Variables
PHP中变量以$
开头,后面跟着变量名。例如:
1 |
|
命名规则跟其他语言差不多,别搞些奇奇怪怪的就行。
PHP Echo/Print
echo可以有多个参数,print只能一个。echo 输出的速度比 print 快, echo 没有返回值,print有返回值1。其他就差不多。
PHP Data Types
String
Integer
Float (floating point numbers - also called double)
Boolean
Array (e.g.
$cars = array("Volvo","BMW","Toyota");
)Object
1
2
3
4
5
6
7
8
9
10
11
12
13
class Car {
function Car() {
$this->model = "VW";
}
}
// create an object
$herbie = new Car();
// show object properties
echo $herbie->model;NULL
Resource
The special resource type is not an actual data type. It is the storing of a reference to functions and resources external to PHP.
A common example of using the resource data type is a database call.
var_dump()
函数可以返回data type和value
PHP Strings
- strlen()
- str_word_count()
- strrev() ->Reverse a String
- strpos() -> Search For a Text Within a String
- str_replace()
PHP Numbers
- is_int()
- is_integer() - alias of is_int()
- is_long() - alias of is_int()
- is_float()
- is_double() - alias of is_float()
- is_finite()
- is_infinite()
- is_nan()
- is_numeric() -> check if a string is numeric string
- The (int), (integer), or intval() function are often used to convert a value (could be string or float) to an integer.
PHP Math
- pi()
- min() max()
- abs()
- sqrt()
- round()
- rand()
PHP Constants
感觉跟java里加了final的变量差不多。用define() 函数来建。
1 |
|
PHP Operators
稍微特殊点的运算符就**
==/!=和===/!==的区别:是否比较type
<=> (spaceship)
1 | a <=> b := |
支持自增自减
逻辑运算符:and/or/xor/&&/||/!
PHP String Operators
.
连接两个string.=
Append str2 to str1
PHP Array Operators
- +
- ==
- ===
- !=
- <>
- !==
PHP Conditional Assignment Operators
?:
??
(e.g.$x = *expr1* ?? *expr2*
)expr1存在并不为null时返回expr2 反之expr2
PHP Conditional Statements
if
statement - executes some code if one condition is trueif...else
statement - executes some code if a condition is true and another code if that condition is falseif...elseif...else
statement - executes different codes for more than two conditionsswitch
statement - selects one of many blocks of code to be executed
PHP Loops
while
- loops through a block of code as long as the specified condition is truedo...while
- loops through a block of code once, and then repeats the loop as long as the specified condition is truefor
- loops through a block of code a specified number of timesforeach
- loops through a block of code for each element in an array
PHP Arrays
PHP Superglobals
- $GLOBALS
- $_SERVER
- $_REQUEST
- $_POST
- $_GET
- $_FILES
- $_ENV
- $_COOKIE
- $_SESSION
PHP Forms
Form handling
1 | <html> |
welcome.php looks like:
1 | <html> |
Form Validation
清理并检查输入
1 |
|
Check Email and URL
filter_var() function to check whether an email address is well-formed
preg_match() 正则检查URL
Form Required
display error message
1 | <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> |
check required field
1 |
|
PHP OOP
PHP Class
1 |
|
Constructor & Destructor
__construct() function
1 |
|
__destructor() function
1 |
|
Access Modifiers
public
protected
private
修饰变量
1 |
|
修饰方法
1 |
|
Inheritance
extends
关键词
1 |
|
子类不能继承父类的protected function
子类可以override父类的方法
父类的final 修饰符可以阻止子类override
Class Constants
1 |
|
和static变量不一样喔,具体区别参考这。
Abstract Class
An abstract class is a class that contains at least one abstract method. An abstract method is a method that is declared, but not implemented in the code.
1 |
|
子类继承规则
- The child class method must be defined with the same name and it redeclares the parent abstract method
- The child class method must be defined with the same or a less restricted access modifier
- The number of required arguments must be the same. However, the child class may have optional arguments in addition
1 |
|
Traits
Traits are used to declare methods that can be used in multiple classes. Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).
1 |
|
trait和interface不一样喔。具体区别参考这。
Static Methods
1 |
|
To access, use ClassName::staticMethod();
To call a static method from a child class, use the parent
keyword inside the child class. Here, the static method can be public
or protected
.
Static Properties
1 |
|
use parent
keyword
1 |
|