ホームページ  >  記事  >  php教程  >  PHP_基础,php基础

PHP_基础,php基础

WBOY
WBOYオリジナル
2016-06-13 08:47:24825ブラウズ

PHP_基础,php基础

目录

  • 数组

  • 函数

  • 类和对象

  • 字符串操作

  • 会话控制

  • 时间和日期

  • 异常处理

 

一、数组

  1、索引数组

<span>header</span>("Content-Type: text/html; charset=utf-8"<span>);

</span><span>//</span><span>创建空数组</span>
<span>$str</span> = <span>array</span><span>();

</span><span>//</span><span>索引数组:数组的键是整数的数组,并且键的整数顺序是从0开始,依次类推。</span>
<span>$fruit</span> = <span>array</span>("苹果","香蕉","菠萝"<span>);

</span><span>//</span><span>索引数组赋值:
//1.用数组变量的名字后面跟一个中括号的方式赋值</span>
<span>$arr</span>[0]='苹果'<span>;
</span><span>//</span><span>使用=>符号来分隔键和值,左侧表示键,右侧表示值。</span>
<span>array</span>('0'=>'苹果'<span>);

</span><span>//</span><span>count($arr)返回数组长度

//for循环访问索引数组里的值</span>
<span>for</span>(<span>$i</span>=0; <span>$i</span><<span>count</span>(<span>$fruit</span>); <span>$i</span>++<span>){
    </span><span>echo</span> "<span>$fruit</span>[<span>$i</span>]<br>"<span>;
}

</span><span>//</span><span>foreach循环访问索引数组里的值</span>
<span>foreach</span>(<span>$fruit</span> <span>as</span> <span>$k</span>=><span>$v</span><span>){
    </span><span>echo</span> <span>$k</span>."...".<span>$v</span>."<br>"<span>;
}</span>

 

  2、关联数组

<span>header</span>("Content-Type: text/html; charset=utf-8"<span>);

</span><span>//</span><span>关联数组:指数组的键是字符串的数组</span>
<span>$fruit</span> = <span>array</span>('apple'=>"苹果", 'banana'=>"香蕉", 'pineapple'=>"菠萝"<span>);

</span><span>//</span><span>关联数组赋值
//1.用数组变量的名字后面跟一个中括号的方式赋值</span>
<span>$arr</span>['apple']='苹果'<span>;
</span><span>//</span><span>2.使用=>符号来分隔键和值,左侧表示键,右侧表示值</span>
<span>array</span>('apple'=>'苹果'<span>);

</span><span>//</span><span>foreach循环访问关联数组里的值</span>
<span>foreach</span>(<span>$fruit</span> <span>as</span> <span>$k</span>=><span>$v</span><span>){
    </span><span>echo</span> '<br>水果的英文键名:'.<span>$k</span>.',对应的值是:'.<span>$v</span><span>;
}</span>

 

二、函数

   1.可变函数

<span>//</span><span>可变函数,通过变量的值来调用函数</span>
<span>function</span><span> name() {
    </span><span>echo</span> 'jobs'<span>;
}
</span><span>$func</span> = 'name'<span>;
</span><span>//</span><span>调用可变函数</span>
<span>$func</span>(); 

 

  2.判断函数是否存在

<span>function</span><span> func() {
}
</span><span>if</span> (<span>function_exists</span>('func'<span>)){
    </span><span>echo</span> 'exists'<span>;
}</span>

 

三、类和对象 

<span>//</span><span>定义一个类</span>
<span>class</span><span> Car {
    </span><span>//</span><span>定义属性</span>
    <span>public</span> <span>$name</span> = '汽车'<span>;
    </span><span>//</span><span>定义方法</span>
    <span>public</span> <span>function</span><span> getName() {
        </span><span>//</span><span>方法内部可以使用$this伪变量调用对象的属性或者方法</span>
        <span>return</span> <span>$this</span>-><span>name;
    }
}
</span><span>//</span><span>实例化一个对象</span>
<span>$car</span> = <span>new</span><span> Car();
</span><span>//</span><span>调用对象的方法</span>
<span>echo</span> <span>$car</span>-><span>getName();

</span><span>//</span><span>类的属性
//public:公开的
//protected:受保护的,受保护的属性不允许外部调用
//private:私有的,私有属性不允许外部调用

//静态方法:使用关键字static修饰
//静态方法不需要实例化对象,可以通过类名直接调用,操作符为双冒号::</span>
<span>class</span><span> Car {
    </span><span>public</span> <span>static</span> <span>function</span><span> getName() {
        </span><span>return</span> '汽车'<span>;
    }
​}
</span><span>echo</span> Car::getName(); <span>//</span><span>结果为&ldquo;汽车&rdquo;

//构造函数__construct():在每次对象创建的时候调用该函数
//析构函数__destruct():在每次对象销毁的时候调用该函数</span>
<span>class</span><span> Car {
    </span><span>//</span><span>增加构造函数与析构函数</span>
    <span>function</span><span> __construct() {
        </span><span>print</span> "构造函数被调用 \n"<span>;
    }
    </span><span>function</span><span> __destruct() {
        </span><span>print</span> "析构函数被调用 \n"<span>;
    }
}
</span><span>$car</span> = <span>new</span> Car();

 

四、字符串操作

  1.单双引号的区别

<span>//</span><span>双引号串中直接包含字串变量
//单引号串中的内容总被认为是普通字符</span>
<span>$str</span>='hello'<span>;
</span><span>echo</span> "str is <span>$str</span>"; <span>//</span><span>运行结果: str is hello</span>
<span>echo</span> 'str is $str'; <span>//</span><span>运行结果: str is $str</span>

 

  2.去除字符串首尾的空格

<span>//</span><span>trim去除一个字符串两端空格。
//rtrim是去除一个字符串右部空格
//ltrim是去除一个字符串左部空格</span>
<span>trim</span>(" 空格 ");

 

  3.获取字符串的长度

<span>//</span><span>英文字符长度</span>
<span>strlen</span>(<span>$str</span><span>);
</span><span>//</span><span>中文字符长度</span>
mb_strlen(<span>$str</span>,"UTF8");

 

  4.字符串的截取

<span>//</span><span>英文字符串的截取函数substr()
//substr(字符串变量,开始截取的位置,截取个数)</span>
<span>$str</span>='i love you'<span>;
</span><span>echo</span> <span>substr</span>(<span>$str</span>, 2, 4<span>);

</span><span>//</span><span>中文字符串的截取函数mb_substr()
//mb_substr(字符串变量,开始截取的位置,截取个数, 网页编码)</span>
<span>$str</span>='我爱你,中国'<span>;
</span><span>echo</span> mb_substr(<span>$str</span>, 4, 2, 'utf8');

 

 

  5.字符串查找

<span>//</span><span>strpos(要处理的字符串, 要定位的字符串, 定位的起始位置[可选])</span>
<span>$str</span> = 'I want to study at imooc'<span>;
</span><span>$pos</span> = <span>strpos</span>(<span>$str</span>, 'imooc');

 

  6.字符串替换

<span>//</span><span>str_replace(要查找的字符串, 要替换的字符串, 被搜索的字符串, 替换进行计数[可选])</span>
<span>$str</span> = 'I want to learn js'<span>;
</span><span>$replace</span> = <span>str_replace</span>('js', 'php', <span>$str</span>);

 

  7.字符串的合并与分割

<span>//</span><span>字符串合并函数implode():把数组元素组合为一个字符串
//implode(分隔符[可选], 数组)</span>
<span>$arr</span> = <span>array</span>('Hello', 'World!'<span>);
</span><span>$result</span> = <span>implode</span>('', <span>$arr</span><span>);
</span><span>print_r</span>(<span>$result</span>);<span>//</span><span>结果显示Hello World!

//字符串分隔函数explode():函数返回由字符串组成的数组
//explode(分隔符[可选], 字符串)</span>
<span>$str</span> = 'apple,banana'<span>;
</span><span>$result</span> = <span>explode</span>(',', <span>$str</span><span>);
</span><span>print_r</span>(<span>$result</span>);<span>//</span><span>结果显示array('apple','banana')</span>

 

五、会话控制

  1.cookie  

<span>//</span><span>设置cookie
//name( Cookie名)可以通过$_COOKIE['name'] 进行访问
//value(Cookie的值)
//expire(过期时间)Unix时间戳格式,默认为0,表示浏览器关闭即失效
//path(有效路径)如果路径设置为'/',则整个网站都有效
//domain(有效域)默认整个域名都有效,如果设置了'www.imooc.com',则只在www子域中有效</span>
<span>$value</span> = 'test'<span>;
</span><span>setcookie</span>("TestCookie", <span>$value</span><span>);
</span><span>//</span><span>有效期一小时</span>
<span>setcookie</span>("TestCookie", <span>$value</span>, <span>time</span>()+3600<span>);

</span><span>//</span><span> cookie的删除与过期时间</span>
<span>setcookie</span>('test', '', <span>time</span>()-1);

 

  2.session

<span>//</span><span>先执行session_start方法开启session</span>
<span>session_start</span><span>();

</span><span>//</span><span>通过全局变量$_SESSION进行session的读写。</span>
<span>$_SESSION</span>['name'] = 'jobs'<span>;
</span><span>echo</span> <span>$_SESSION</span>['name'<span>];

</span><span>//</span><span>删除某个session</span>
<span>unset</span>(<span>$_SESSION</span>['name'<span>]);
</span><span>//</span><span>删除所有session</span>
<span>session_destroy</span>();

 

 六、时间和日期

<span>//</span><span>UNIX 时间戳:表示从 1970年1月1日 00:00:00 到当前时间的秒数之和。
//函数 time() 来取得服务器当前时间的时间戳</span>
<span>$time</span>=<span>time</span><span>();
</span><span>echo</span> <span>$time</span><span>;

</span><span>//</span><span>date()函数,来取得当前的日期
//date(时间戳的格式, 规定时间戳【默认是当前的日期和时间,可选】)
//设置默认的时区</span>
date_default_timezone_set('Asia/Shanghai'<span>);
</span><span>//</span><span>输出1396193923对应的日期</span>
<span>echo</span> <span>date</span>("Y-m-d"<span>);

</span><span>//</span><span>函数strtotime实现功能:获取某个日期的时间戳,或获取某个时间的时间戳
//strtotime(要解析的时间字符串, 计算返回值的时间戳【默认是当前的时间,可选】)
//1398700800,这个数字表示从1970年1月1日 00:00:00 到2014年4月29号经历了1398700800秒</span>
<span>echo</span> <span>strtotime</span>('2014-04-29'<span>);
</span><span>//</span><span>1398700801,这个数字表示从1970年1月1日 00:00:00 到2014-04-29 00:00:01时经历了1398700801秒</span>
<span>echo</span> <span>strtotime</span>('2014-04-29 00:00:01');

 

 

七、异常处理

<span>//</span><span>创建可抛出一个异常的函数</span>
<span>function</span> checkNum(<span>$number</span><span>){
    </span><span>if</span>(<span>$number</span>>1<span>){
        </span><span>throw</span> <span>new</span> <span>Exception</span>("异常提示-数字必须小于等于1"<span>);
    }
    </span><span>return</span> <span>true</span><span>;
}

</span><span>//</span><span>在 "try" 代码块中触发异常</span>
<span>try</span><span>{
    checkNum(</span>2<span>);
    </span><span>//</span><span>如果异常被抛出,那么下面一行代码将不会被输出</span>
    <span>echo</span> '如果能看到这个提示,说明你的数字小于等于1'<span>;
}</span><span>catch</span>(<span>Exception</span> <span>$e</span><span>){
    </span><span>//</span><span>捕获异常</span>
    <span>echo</span> '捕获异常: ' .<span>$e</span>-><span>getMessage();
}

</span><span>//</span><span>Exception具有几个基本属性与方法,其中包括了:

//message 异常消息内容
//code 异常代码
//file 抛出异常的文件名
//line 抛出异常在该文件的行数

//其中常用的方法有:
//getTrace 获取异常追踪信息
//getTraceAsString 获取异常追踪信息的字符串
//getMessage 获取出错信息</span>

<span>class</span> MyException <span>extends</span> <span>Exception</span><span> {
    </span><span>function</span><span> getInfo() {
        </span><span>return</span> '自定义错误信息'<span>;
    }
}

</span><span>try</span><span> {
    </span><span>throw</span> <span>new</span> MyException('error'<span>);
} </span><span>catch</span>(<span>Exception</span> <span>$e</span><span>) {
    </span><span>echo</span> <span>$e</span>-><span>getInfo();
}</span>

 

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。