PHP光速教程
PHP光速教程
日期:2010-07-29 | 来源:redice's Blog | 作者:redice | 182 人围观 | 0 人鼓掌了! // by redice 2010.07.29// redice@163.com
为公司实习生写的PHP学习提纲,根据自己的学习及项目开发经验总结了PHP语言最核心的知识点。可以作为PHP快速入门的教程。
1 客户端脚本和服务端脚本
客户端:VBscript(对IE依赖性很强,放弃)、Javascript
服务端:ASP、PHP、Perl
JSP(服务端,非脚本)
Python(可用服务端,非脚本)
2 学习一门语言的要点/顺序
功能、特点、语法、变量、运算符、流程控制、函数、数据结构
3 学习服务端语言的要点
数据输入输出、数据库操作、session和cookie的使用
4 PHP的功能
支持与众多服务器软件(Apache,IIS ISAPI/FastCGI,Nginx等)结合进行数据处理
5 PHP的特点
跨平台、内置函数库非常丰富(写得少做得多)、语法简洁、参考资料非常多
6 PHP的语法
// 这是注释行,注释行以//开始
phpinfo(); // 语句以;结束
?>
7 PHP的变量
松散类型,自动声明,强制转换
PHP变量以$开头(从钱开始,很实惠的一种语言)
$txt = "Hello World!"; // 字符串用""包围,转义",\
$number = 16;
?>
松散类型语言:VB,VBscript,ASP,PHP,Python
强类型语言:C(C++),JSP
8 PHP的运算符
算术运算:+,-,*,/,%(取余),++(自增1),--(自减1)
赋值运算:=,+=,-=,*=,/=,.=,%=
比较运算符:==,===,!=,>,=,
逻辑运算符:&&,||,!
其它运算符:.(字符串连接)
$i=10;
$i+=1;
echo $i; // echo 数据输出函数,还可以用print
echo ++$i; // ?
echo $i++; // ?
?>
*两等号与三等号
== 只比较值是否相等,会将两侧值进行类型转换
=== 比较值和类型是否相同,不会进行类型转换,类型和值都相同才为真
例如:
$a="2"; // 字串型2
$b=2; // 数值型2
$a==$b, 是对的,都是2
$a===$b,是不对的,因为$a是字符型$b是数值型,值虽一样,但类型不一样
9 PHP流程控制
顺序语句
分支语句:if else,Switch
// if else语句
if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
// switch语句
switch (expression)
{
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed
if expression is different
from both label1 and label2;
}
循环语句:while,for,foreach
// while循环
while (condition)
code to be executed;
// for循环
for (initialization; condition; increment)
{
code to be executed;
}
// foreach循环,遍历数组
foreach (array as value)
{
code to be executed;
}
// foreach循环的示例
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
echo "Value: " . $value . "
";
}
?>
*结束PHP程序的执行:
exit($str); // 结束PHP执行并输出$str
die(); // 仅结束PHP执行
10 PHP函数
内置函数:
http://php.chinaunix.net/manual/en/
(1)数据输出:echo,print,print_r(输出数组)
(2)字符串操作:
// 返回$string的长度
int strlen ( string $string )
// 删除$str两端的空格或其它字符
string trim ( string $str [, string $charlist ] )
$str=" xiao ping ni hao! ";
echo trim($str);
echo "
";
echo trim($str," x!")
?>
// 字符串转大小写
string strtolower ( string $str )
string strtoupper ( string $string )
// 寻找字串
int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
返回值:返回找到的子串的位置,没有找到返回false
注意:
如果子串$needle在$haystack串首出现将返回0
因此判断是否找到字串应该用===而不能用==
$str="redicecn.com";
// 下面是错误的判断
if(strpos($str,"redice")==false)
{
echo "没有找到子串!";
}
else
{
echo "找到了子串!";
}
?>
// 字符串截取
string substr ( string $string , int $start [, int $length ] )
// 字符串替换
string str_replace ( mixed $search , mixed $replace , mixed $subject)
string str_ireplace ( mixed $search , mixed $replace, mixed $subject) // 不区分大小写
(3)时间日期:
// time() 函数返回当前时间的 Unix 时间戳
time(void)
// date() 函数格式化一个本地时间/日期
// 默认是格林威治时区
date(format,timestamp)
// 设置为东8区
date_default_timezone_set('Etc/GMT-8');
echo date("Y-m-d H:i:s",time());
?>
自定义函数:
$sitename="电子工程社区";
function welcom($user)
{
global $sitename; // 引用全局变量
// 返回值
return $user.",欢迎您的光临".$sitename."!";
}
// 函数调用
echo welcom("redice");
?>
11 PHP的数据结构
数组
// 数值数组的定义
(1) 自动分配key
$names = array("芙蓉姐姐","凤姐","犀利哥");
(2) 手动分配key
$names[0]="芙蓉姐姐";
$names[1]="凤姐";
$names[2]="犀利哥";
// 关联数组的定义
$ages = array("芙蓉姐姐"=>32, "凤姐"=>29, "犀利哥"=>42);
也可以这样
$ages["芙蓉姐姐"] = 32;
$ages["凤姐"] = 29;
$ages["犀利哥"] = 42;
// 多维数组
$students = array
(
"0911120688"=>array
(
"姓名"=>"齐鹏",
"年龄"=>24,
"性别"=>"男"
),
"0911120699"=>array
(
"姓名"=>"宋玉伟",
"年龄"=>22,
"性别"=>"女"
),
"0911120670"=>array
(
"姓名"=>"陈素芳",
"年龄"=>22,
"性别"=>"女"
)
);
12 PHP输入(获取客户端输入)
(1)$_GET 变量
$_GET 变量是一个数组,内容是由 HTTP GET 方法发送的变量名称和值。
(2)$_POST 变量
$_POST 变量是一个数组,内容是由 HTTP POST 方法发送的变量名称和值。
(3)$_REQUEST 变量
PHP 的 $_REQUEST 变量包含了 $_GET, $_POST 以及 $_COOKIE 的内容。
13 SESSION和COOKIE的使用
(1)SESSION
保存在服务端,服务器通过COOKIE中的SESSIONID判断,常用来进行身份验证。
使用SESSION前需要用session_start()启动会话,
由于session_start()需要修改HTTP应答报文的COOKIE头(存SESSIONID),
因此session_start()必须在HTTP应答正文输出之前被调用。
// 创建session
session_start();
$_SESSION['user']="redice";
?>
// 读取session
session_start();
echo $_SESSION['user'];
?>
删除SESSION
unset($_SESSION['user']);
?>
或
session_destroy(); // 将删除所有的session
?>
(2)COOKIE
保存在客户端,随请求报文一起被发送到服务端,
常用来存储用户自定义设置、浏览记录等与安全无关的数据。
*COOKIE在客户端存储,可被用户修改,因此不能存储敏感数据。
// 创建cookie
setcookie(name, value, expire);
setcookie()也需要修改HTTP应答头,因此需要在输出任何正文之前被调用
setcookie("user", "redice", time()+3600);
?>
// 读取cookie
echo $_COOKIE["user"];
?>
// 删除cookie
// 设置立即过期,客户端(浏览器)会自动删除
setcookie("user", "", time()-3600);
?>
14 数据库操作
操作流程:
连接数据库->选择库->设置采用的字符集
->操作数据(查询,更新,删除,插入)->关闭数据库
$conn=0;
$conn = mysql_connect("localhost","root","redice2009");
if (!$conn)
{
die("不能打开数据库连接,错误: " . mysql_error());
}
// 选择数据库
mysql_select_db("thymall", $conn);
// 设置mysql数据库输出数据的字符集
mysql_query("set names 'gbk'");
$sql="select * from thym_goods LIMIT 5";
// 执行查询
$result=mysql_query($sql,$conn);
// 遍历查询结果
while($result && $row=mysql_fetch_array($result))
{
}
// 关闭数据库
mysql_close($conn);
?>
15 其它
良好的程序风格:缩进,注释
开发工具的选取原则:代码关键字高亮,自动完成

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
