Jargon
- API - Application Programming Interface
- REST - Representational State Transfer
- AJAX - Asynchronous Javascript And XML
- MVVM - Model - View - ViewModel
- PHP - Hypertext Preprocessor
- ORM - Object-relational Mapping
常用部署工具
- Phing - 基于 Ant 的项目代码构建系统,自动化部署工具,通过编写 XML 文件来实现压缩,上传,解压等操作。Java 上的 Ant,JavaScript 上的 Grunt, Gulp
- PHPUnit - PHP Unit Test工具,同样的还有 PHPSPEC。Java上的 JUnit,JS上的 Mocha
PHP
function __construct(){}; function __destruct(){}; function ClassName(){};
Static attributes cannot be accessed by an object, but static functions could be.
-
PHP submit form three ways: get, post, request
- request can get data from get and post, first get then post.
- get is not secure while post is secure
- get pass value to server by URL while post pass value by HTML header
- maximum data pass by get is 2 kilobyte while there is no limit for post
-
PHP filter:
- Validating: used to validate the input, eg. valid email address
- Sanitizing: used to sanitize the input and return String, eg. www.q**q.com => www.qq.com
-
Require and Include:
- require will throw an exception error if file not exists
- include will throw a warning
-
Three ways to connect DB (http://www.cnblogs.com/joshua317/articles/5989781.html):
Using mysql function, process oriented, is not used currently.
$conn = mysql_connect('127.0.0.1:3306', 'root', 'root');Using mysqli, object-oriented, could only support MySql database
$conn = new mysqli('127.0.0.1:3306', 'root', 'root');-
Using PDO (PHP data object), unified API, could connect to different kind of databases, like JDBC in Java.
$pdo = new PDO("mysql:host=" . '127.0.0.1:3306'. ";dbname=" . 'demoDB', 'root', 'root');- PDO::ERRMODE_SILENT
- PDO::ERRMODE_WARNING
- PDO::ERRMODE_EXCEPTION
Refactoring is a process of changing the code and changing its structure without changing the underline behaviour.
Type Hinting specifies a type of the argument of the function.
Redirect to the page: header("Location: /");
-
... splat array
- when in function header, means accept multiple arguments and can be used as an array in function.
- when in passing argument to function, will unpack the array to match the arguments respectively.
-
PHP 7
- Scalar Typehint: function setAge(int $age){};
- Return Type Declaration: function getUser() : User {};
- Spaceships operator: <=> is used for comparison, return -1, 0, 1
- Null Coalesce Operatro: ?? if variable not defined, return default: echo $name ?? "Jack";
- Grouped Imports: use App{Controller, Model};
- Anonymous Class
-
Form
- Usually, uses input will be processed by trim($input), stripslashes($input), htmlspecialchars($input).
- preg_match("Regular Expression", $input) could be used for regular expression match.
File
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
echo fgetc($myfile);
}
fclose($myfile);
// Upload file
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
?>
- Cookie
// setcookie(name, value, expire, path, domain);
setcookie("user", "Alex Porter", time()+3600);
// Print a cookie
echo $_COOKIE["user"];
// A way to view all cookies
print_r($_COOKIE);
if (isset($_COOKIE["user"]))
echo "Welcome " . $_COOKIE["user"] . "!<br />";
// Delete cookie
// set the expiration date to one hour ago
setcookie("user", "", time()-3600);
- Session
<?php
session_start();
// store session data
$_SESSION['views']=1;
if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
unset($_SESSION['views']);
session_destroy();
- AJAX (Asynchronous JavaScript And XML)
-
Security
- Cross-site scripting (XSS): attackers inject client-side script into web pages viewed by other users. Attachers can use $_SERVER["PHP_SELF"] to inject javascript. htmlspecialchars($_SERVER["PHP_SELF"] could be used to avoid attach because this function will convert special characters to html entities.
Build Management Tool
Phing
Phing is a project build system based on Apache ant.
Grunt (Gulp, Webpack)
Automation tool. The less work you have to do when performing repetitive tasks like minification, compilation, unit testing, linting, etc, the easier your job becomes.
JavaScript
- 'use strict' - force using var to declare new variable, otherwise error, because if it is not used, the variable which is not be declared by var will be regarded as global variable.
ES6
- `...`: multiple line