php笔记

PHP 的脚本块:<?php      ?>
 
注释://或者/*......*/
 
变量的定义:
$txt = "Hello World!";
$number = 16;
 
字符串操作函数:
把两个字符串连接在一起可以用“.”运算符
 

strlen():计算字符串的长度

 
strpos():检索一段字符串或者字符
 
条件语句:
if
     ......
else
     ......
 
多个条件语句:elseif,这两个是连起来的
也支持switch语句
 
数组:
数值数组:
$names = array("Peter","Quagmire","Joe");
 
关联数组:
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
 
多维数组
 
循环:
while
do...while
for($i=1; $i<=5; $i++)
foreach:
<?php
$arr=array("one", "two", "three");
 
foreach ($arr as $value)
{
  echo "Value: " . $value . "<br />";
}
?>

$_GET、
$_POST 变量:
提取使用get或者post方法提交的表单的参数

$_REQUEST 变量

PHP 的 $_REQUEST 变量包含了 $_GET, $_POST 以及 $_COOKIE 的内容。
 

date()函数:格式化日期或时间

date(format,timestamp)
format 必需。规定时间戳的格式。
timestamp 可选。规定时间戳。默认是当前的日期和时间。
 
d - 月中的天 (01-31)
m - 当前月,以数字计 (01-12)
Y - 当前的年(四位数)
 
mktime(hour,minute,second,month,day,year,is_dst)可以生成时间戳
 

服务器端引用

include() 或 require() 函数
除了它们处理错误的方式不同之外,这两个函数在其他方面都是相同的。
include() 函数会生成一个警告(但是脚本会继续执行),而 require() 函数会生成一个致命错误(fatal error)(在错误发生后脚本会停止执行)。
include() 函数可获得指定文件中的所有文本,并把文本拷贝到使用 include 函数的文件中。
推荐使用 require() 而不是 include()
 

文件处理

fopen("文件名","打开模式")
例子:
尝试读,如果失败则返回一段错误信息
<?php
$file=fopen("welcome.txt","r") or exit("Unable to open file!");
?>
打开与关闭文件
<?php
$file = fopen("test.txt","r");
//some code to be executed
fclose($file);
?>
检测文件的末尾
if (feof($file)) echo "End of file";
逐行读取文件
<?php
$file = fopen("welcome.txt", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
  {
  echo fgets($file). "<br />";
  }
fclose($file);
?>
fgetc()函数用于逐字符读取文件
文件的上传
 
php类相关:
定义类:
<?php
 
class User
{
     public $name;
     private $pwd;
     public function __construct($name,$pwd)//两个下划线
     {
          $this->name=$name;
          $this->pwd=$pwd;
          
     }
}
 
//创建对象的实例
$user=new User("Leon","hispwd");
 
 
?>
访问限定语:public private protected
也可以用static来修饰一个成员
type hint:可以对类中方法的参数加上类型限制
构造函数与析构函数:__construct和__destruct
使用extends关键字来继承一个类
parent::__construct//在子类中调用父类的构造函数
self:://自身命名空间,可以调用自身的类或方法???????????
静态属性不可以用this来引用,但是可以用self来引用
 
 
属性与方法的引用:
$this->$property
$obj->$method();
使用const关键字来建立静态常量
 
posted @ 2012-12-07 16:24  xxxyyylll  阅读(110)  评论(0)    收藏  举报