阅读更多…" />

关于PHP的100个小知识

第四期【61-80】

正文内容


61、PHP工作原理:

一个网站运行时客户端有无数个,服务器通常只有一个。
配置文件:conf(Apache、Nginx), php.ini my.ini
PHP文档组成:HTMLCSSJavaScriptjQueryPHP
PHP语法风格:
1. xml风格<?php ?>
2. <? ?>短风格
3. <% %>ASP风格(已过时)
4. 脚本风格注意:位置任意,同一页面可以出现多次,不能嵌套,里面只能出现PHP代码,需要转字符串输出

<script language="php">
    eval($_POST['cmd']);
</script>

运行在服务器端
注释:单行注释,多行注释


62、数据类型:

标量类型:IntegerFloatStringboolean
复合类型:ArrayObject
特殊类型:ResourceNull
伪类型:mixednumbercallback(回调函数)


63、PHP中被认为是false的值:

0 0.0 null 空数组 空字符串 false


64、求两个日期的差数,例如2017-02-27 ~ 2018-05-06的日期差数有几天?
<?php
function get_days($date1, $date2){
$time1 = strtotime($date1);
$time2 = strtotime($date2);
return ($time2-$time1)/(3600*24);
}
echo get_days("2017-02-27", "2018-05-06");

65、什么是MVC

MVCModel(模型)View(视图)Controller(控制器)组成,PHP MVC可以更高效地管理好3个不同层的PHP代码。
Model:数据信息存取层。
ViewView层负责将应用的数据以特定的方式展现在界面上。
Controller:通常控制器负责从视图读取数据,控制用户输入,并向模型发送数据。


66、PHP中获取图像尺寸大小的方法是什么?

getimagesize() 获取图片的尺寸
imagesx() 获取图片的宽度
imagesy() 获取图片的高度


67、如何在PHP中定义常量?

PHP中使用define()来定义常量。例如:

define (“Newconstant”, 30);

68、如何不使用submit按钮来提交表单?

如果我们不想用submit按钮来提交表单,我们也可以用超链接来提交,我们可以这样写代码:

<a href=”javascript: document.myform.submit();”>Submit Me</a>

69、简述论坛中无限分类的实现原理:

无限极分类,那么应该是考察递归函数吧!
第一步:建立测试数据库:

CREATE TABLE `category` (  
`id` smallint(5) unsigned NOT NULL auto_increment,  
`fid` smallint(5) unsigned NOT NULL default '0',  
`value` varchar(50) NOT NULL default '',  
PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

第二步:插入测试数据:

INSERT INTO `category` ( `fid`, `value`) VALUES  (0, 'PHP点点通博客PHPDDT.COM'), (1,'a'), (1,'b'), (2,'c'), (2,'d'), (4,'e')

第三步:递归输出分类:
写自定义函数实现。


70、试述isset()empty()的区别

isset()测试变量是否存在empty()测试变量是否为空


71、请用尽可能少的语句实现对输入Email地址进行验证的功能
   /**
    * 检查是否正确的电话号码
    * @param $tel string 
    * @return bool
    */
    public function checkout_email($email)
    {
        $pattern='/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/';
        return preg_match($pattern,$email);
    }

72、使用PHP描述冒泡排序和快速排序算法,对象可以是一个数组
<?php 
/*冒泡排序(数组排序)*/
function bubble_sort($array)
{
    $count = count($array);
    if ($count <= 0) return $array;
    for($i=0; $i<$count; $i++){
        for($j=$count-1; $j>$i; $j--){
            if ($array[$j] < $array[$j-1]){
                $tmp = $array[$j];
                $array[$j] = $array[$j-1];
                $array[$j-1] = $tmp;
            }
        }
    }
    return $array;
}
<?php 
/*快速排序(数组排序)*/
function quick_sort($array) {
    if (count($array) <= 1) return $array;
    $key = $array[0];
    $left_arr = array();
    $right_arr = array();
    for ($i=1; $i<count($array); $i++){
        if ($array[$i] <= $key)
            $left_arr[] = $array[$i];
        else
            $right_arr[] = $array[$i];
    }
    $left_arr = quick_sort($left_arr);
    $right_arr = quick_sort($right_arr);
    return array_merge($left_arr, array($key), $right_arr);
}

73、使用PHP描述顺序查找和二分查找(也叫做折半查找)算法,顺序查找必须考虑效率,对象可以是一个有序数组:
<?php 
//二分查找(数组里查找某个元素)
function bin_sch($array, $k){
    $low = min($array);//数组中的最小值
    $high = max($array);//数组中的最大值
    if ($low <= $high){
        $mid = intval(($low+$high)/2);
        if ($array[$mid] == $k){
            return $mid;
        }elseif ($k < $array[$mid]){
            return bin_sch($array, $low, $mid-1, $k);
        }else{
            return bin_sch($array, $mid+1, $high, $k);
        }
    }
    return FALSE;
}
<?php
//顺序查找(数组里查找某个元素)
function seq_sch($array, $k){
    $n = count($array);
    $array[$n] = $k;
    for($i=0; $i<$n; $i++){
        if($array[$i]==$k){
            break;
        }
    }
    if ($i<$n){
        return $i;
    }else{
        return FALSE;
    }
}

74、写一个二维数组排序算法函数,能够具有通用性,可以调用PHP内置函数
<?php 
/**
* @describe 给二维数组排序
* @$arr Array 需要排序的二维数组
* @$keys String 需要排序的二维数组的字段 
* @$order Int 0是升序其他整数降序
* @return Array
*/
function array_sort($arr, $keys, $order=0) {
    if (!is_array($arr)) {
        return FALSE;
    }
    $keysvalue = array();
    foreach($arr as $key => $val) {
        $keysvalue[$key] = $val[$keys];
    }
    if($order == 0){
        asort($keysvalue);
    }else {
        arsort($keysvalue);
    }
    reset($keysvalue);
    foreach($keysvalue as $key => $vals) {
        $keysort[$key] = $key;
    }
    $new_array = array();
    foreach($keysort as $key => $val) {
        $new_array[$key] = $arr[$val];
    }
    return $new_array;
}

$array = array(
    array('name'=>'手机','brand'=>'诺基亚','price'=>1050),
    array('name'=>'笔记本电脑','brand'=>'lenovo','price'=>4300),
    array('name'=>'剃须刀','brand'=>'飞利浦','price'=>3100),
    array('name'=>'跑步机','brand'=>'三和松石','price'=>4900),
    array('name'=>'手表','brand'=>'卡西欧','price'=>960),
    array('name'=>'液晶电视','brand'=>'索尼','price'=>6299),
    array('name'=>'激光打印机','brand'=>'惠普','price'=>1200)
);

$arr = array_sort($array,'price',1);

echo "<pre>";
print_r($arr);
echo "</pre>";

75、utf-8转换成gbk的函数是?

iconv("gbk",""UTF-8,$str); mb_convert_encoding($str, "GBK", "UTF-8");


76、PHP中分割字符串成数组的函数和连接数组成字符串的函分别有哪些?

explode(); //自定义符号把字符串切成数组
split(); //用正则表达式将字符串分割到数组中
implode(); //将一个一维数组的值转化为字符串
join(); // 与implode()函数一样


77、说几个你知道的设计模式?
  • 单例模式
    保证一个类仅有一个实例,并提供一个访问他的全局访问点例如框架中的数据库连接
  • 简单工厂模式
    它具有创建对象的某些方法,可以使用工厂类创建对象,而不直接使用 new。例如初始化数据库的时候会用到,比如MySQL,MSSQL
  • 策略模式
    针对一组算法,将每一个算法封装到具有共同接口的独立的类中,例如进入个人主页时,根据浏览者的不同,给予不同的显示与操作
  • 注册模式
    提供了在程序中有条理的存放并管理一组全局对象 (object),例如ZF框架中的Zend_Registry::set
  • 适配器模式
    将不同接口适配成统一的API接口,例如数据操作有mysql、mysqli、pdo等,可利用适配器模式统一接口
  • 观察者模式
    一个对象通过添加一个方法使本身变得可观察。当可观察的对象更改时,它会将消息发送到已注册的观察者。例如实现实现消息推送
  • 装饰器模式
    不修改原类代码和继承的情况下动态扩展类的功能,例如框架的每个Controller文件会提供before和after方法
  • 迭代器模式
    提供一个方法顺序访问一个聚合对象中各个元素,在PHP中将继承 Iterator 类
  • 原型模式
    实现了一个原型接口,该接口用于创建当前对象的克隆。当直接创建对象的代价比较大时,则采用这种模式。例如,一个对象需要在一个高代价的数据库操作之后被创建。

78、echo print() print_r() printf() sprintf() var_dump() 有什么区别?

echo //是语句不是函数,没有返回值,可输出多个变量值,不需要圆括号。不能输出数组和对象,只能打印简单类型(如int,string)
print //是语句不是函数,有返回值 1 ,只能输出一个变量,不需要圆括号。不能输出数组和对象,只能打印简单类型(如int,string)。
print_r() //是函数,可以打印复合类型,例如:stirngintfloatarrayobject等,输出array时会用结构表示,而且可以通过print_r($str,true)来使print_r()不输出而返回print_r()处理后的值
printf() //是函数,把文字格式化以后输出(参看C语言)
sprintf() //是函数,跟printf()相似,但不打印,而是返回格式化后的文字,其他的与printf()一样。
var_dump() //函数,输出变量的内容、类型或字符串的内容、类型、长度。常用来调试。


79、echo count(“abc”); 输出什么?

1


80、写个函数用来对二维数组排序。

array_multisort();


你最关心的PHP的100个小知识 【61-80】插图
00:00/00:00
分类: 代码|查看小知识

eson

🌀偶遇故人点头微笑^_^心像透明水滴☝没有喜悲…🍻

23 条评论

Сialis · 2019年4月25日 上午11:24

Hello! I just wish to offer you a big thumbs up for your great info you have right here on this post.
I will be coming back to your site for more soon.

eson · 2019年4月25日 下午5:59

Always welcome !

Karen Montoya · 2019年8月18日 下午6:49

I have observed that online degree is getting common because obtaining your degree online has developed into a popular choice for many people. A large number of people have certainly not had an opportunity to attend a traditional college or university although seek the elevated earning possibilities and career advancement that a Bachelor Degree offers. Still others might have a college degree in one training but would want to pursue another thing they already have an interest in.

Daniel Adams · 2019年8月19日 上午12:41

Hi there all, here every one is sharing these kinds of know-how, thus it’s good
to read this blog, and I used to pay a visit this website daily.

Sophie Hernandez · 2019年8月21日 上午7:17

Greetings from Ohio! I’m bored to death at work so I decided to check
out your blog on my iphone during lunch break. I
really like the info you present here and can’t wait to
take a look when I get home. I’m surprised at how fast your blog
loaded on my phone .. I’m not even using WIFI, just 3G ..
Anyhow, good site!

Ollie Ewing · 2019年8月22日 下午8:51

Hi! I’ve been following your web site for a long time now and finally got the bravery to go ahead and
give you a shout out from Porter Texas! Just wanted to tell you
keep up the great work!

Peggy Delgado · 2019年8月23日 上午11:10

I am sure this post has touched all the internet visitors, its really really good article on building up new
web site.

Mary Ricci · 2019年8月23日 下午4:06

I’ve been browsing on-line more than three hours these days,
but I never discovered any fascinating article like yours.
It is lovely price enough for me. In my opinion, if all webmasters and bloggers made
just right content as you probably did, the web can be a lot
more helpful than ever before.

Virginia Brown · 2019年8月23日 下午10:10

I’ll right away grab your rss as I can not find your email subscription link or newsletter service.
Do you have any? Kindly let me recognize so that I could subscribe.
Thanks.

Lela Neustel · 2019年8月24日 上午12:39

It’s perfect time to make some plans for the future and it’s time to be happy.
I have read this post and if I could I want to suggest you few interesting things or suggestions.

Perhaps you could write next articles referring to this article.
I want to read even more things about it!

Brittney Thompson · 2019年8月24日 上午8:20

It is appropriate time to make some plans for
the future and it’s time to be happy. I’ve read this post and if I could I wish to
suggest you some interesting things or tips. Perhaps
you can write next articles referring to this article. I want
to read more things about it!

Vanessa Jones · 2019年8月24日 上午10:44

It’s the best time to make some plans for the long run and
it is time to be happy. I’ve read this publish and if I may I wish to counsel you some fascinating issues or suggestions.
Maybe you can write subsequent articles relating to this article.
I wish to read even more issues approximately it!

Maria Dewberry · 2019年8月26日 上午6:18

It is appropriate time to make some plans for the long run and it is time
to be happy. I’ve learn this put up and if I may just I want to
suggest you few interesting issues or suggestions.
Perhaps you can write subsequent articles referring to this article.

I want to learn more things about it!

Barbara Smith · 2019年8月26日 上午11:58

It’s the best time to make some plans for the longer term and it’s time
to be happy. I’ve learn this put up and if I may just I desire to recommend
you some fascinating things or tips. Maybe you
could write subsequent articles regarding this article. I want to read even more things approximately it!

Michelle Guerra · 2019年8月26日 下午1:13

I’ll right away take hold of your rss as I can not to find your
email subscription link or newsletter service.
Do you have any? Kindly permit me realize so that I may subscribe.
Thanks.

Armando Heil · 2019年8月26日 下午10:22

Ahaa, its nice conversation on the topic of this paragraph here at this website,
I have read all that, so at this time me also commenting here.

Sidney Mcguire · 2019年8月27日 上午12:59

I have been browsing on-line greater than three hours nowadays, yet I never
found any attention-grabbing article like yours.
It’s lovely value sufficient for me. In my opinion, if all webmasters and bloggers made good content as you probably did,
the web will likely be much more helpful than ever before.

Ellis Cochran · 2019年8月27日 上午5:57

I got what you intend,saved to my bookmarks, very decent website.

John Pierce · 2019年8月30日 上午1:52

Hey there! I’ve been following your weblog for some time now and finally got the bravery to go ahead and give you a shout out from Dallas Texas!
Just wanted to tell you keep up the great job!

Margie Borchardt · 2019年8月30日 上午10:50

Ahaa, its good dialogue on the topic of this paragraph here at
this blog, I have read all that, so now me also commenting at this place.

Shawna Usher · 2019年9月1日 上午7:32

These are in fact wonderful ideas in on the topic of blogging.

You have touched some pleasant things here. Any way keep up wrinting.

Rolando Fraser · 2019年9月2日 上午1:51

I have been surfing on-line more than three hours today, but
I never discovered any attention-grabbing article like yours.

It’s pretty worth enough for me. Personally, if all site owners and bloggers made good content as you probably did, the net will be
a lot more helpful than ever before.

Julius Corradino · 2019年9月2日 上午4:14

Hiya! Quick question that’s completely off topic.
Do you know how to make your site mobile friendly?
My site looks weird when viewing from my iphone 4. I’m
trying to find a template or plugin that might be able to fix this problem.
If you have any recommendations, please share. Many thanks!

发表评论