1.setcookie('a','value');print $_COOKIE['a'];输出结果是啥?
答:value 第一次访问为空!第二次访问$_COOKIE['a']已经被赋值完成!
----------------------------------------
2.
try {
require_once "aaa";
print "aaa";
} catch (Exception $e) {
echo 'dddd';
}
结果是啥?
答:没有输出。程序退出。
如果修改为:
try {
include_once "aaa";
print "aaa";
} catch (Exception $e) {
echo 'dddd';
}
则输出aaa
或者修改为:
try {
if(!include_once "aaa") {
throw new Exception;
}
print "aaa";
} catch (Exception $e) {
echo 'dddd';
}
则输出dddd
本人测试,原题的代码输出就是aaa 或者aaa文件不存在时报警告信息
------------------------------
3.
用JS实现trim函数
答:虽然javascript中没有trim()函数,但是可以使用正则表达式实现trim()函数的功能。
<script type="text/javascript">
String.prototype.trim = function() {
var reg = /^\s*(.*?)\s*$/;
return this.replace(reg,"$1");
}
function Testtrim() {
var TestStr = " yingfm ";
alert("("+TestStr.trim()+")");
}
Testtrim();
</script>
------------------------------
4.
<script src=\'#\'" >alert(1);</script>
输出啥?
答:本人觉得应该输出1.js中输出的内容
------------------------------
5.
<script type="text/javascript">alert(["a","b","c","d","e"][[1,2],3,4]);</script>
输出啥?
答:e 经测试,确实输出e,看看alert(["a","b","c","d","e"][4,3,2])的输出是什么
------------------------------
6.
类似这样的aaa<em>sss</em>ddd字符串,写个函数CutStr($str,$max)实现截取:
1)如果$max大于$str的长度则返回$str
2)<em>和</em>不包含在长度计算范围。
例:
如果CutStr("aaa<em>sss</em>ddd",6) 则返回aaa<em>sss</em>.
如果CutStr("aaa<em>sss</em>ddd",4) 则返回aaas,不包含<em>标签
------------------------------
7.
不使用php内置函数实现数组排序,如果value相等,比较key的值。例: array("d" => 100,"c" => 22, "a" => 22);
答:
简单排序:
<?php
$data = array(5,8,1,7,2);
sort($data);//从低到高排序
print_r($data);
rsort($data);//从高到低排序
?>
输出:
Array ( [0] => 1 [1] => 2 [2] => 5 [3] => 7 [4] => 8 )
Array ( [0] => 8 [1] => 7 [2] => 5 [3] => 2 [4] => 1 )
根据关键字排序:
<?php
$data = array("US"=>"United States","IN"=>"India","DE"=>"Germany","ES"=>
"Spain");
ksort($data);//它的相反排序函数krsort()
print_r($data);
?>
输出:
Array ( [DE] => Germany [ES] => Spain [IN] => India [US] => United States )
根据值排序:使用asort()代替ksort()就可以。它的相反排序arsort()
Array ( [DE] => Germany [IN] => India [ES] => Spain [US] => United States )
自然语言排序: natsort(),对它的相反操作可以使用array_reverse()
<?php
$data = array("book-1","book-10","book-100","book-5");
sort($data);
print_r($data);
natsort($data);
print_r($data);
?>
输出结果:
Array ( [0] => book-1 [1] => book-10 [2] => book-100 [3] => book-5 )
Array ( [0] => book-1 [3] => book-5 [1] => book-10 [2] => book-100 )
根据用户自己定义的规则排序:
<?php
function sortByLen($a, $b) {
if (strlen($a) == strlen($b)) {
return 0;
} else {
return (strlen($a) > strlen($b)) ? 1 : -1;
}
}
$data = array("joe@", "@", "asmithsonian@", "jay@");
usort($data, 'sortByLen');
print_r($data);
?>
多维排序:
<?php
$data = array(array("id" => 1, "name" => "Boney M", "rating" => 3),
array("id" => 2, "name" => "Take That", "rating" => 1),
array("id" => 3, "name" => "The Killers", "rating" => 4),
array("id" => 4, "name" => "Lusain", "rating" => 3)
);
foreach ($data as $key => $value) {
$name[$key] = $value['name'];
$rating[$key] = $value['rating'];
}
array_multisort($rating, $name, $data);
print_r($data);
?>
------------------------------
8.
数据库中有两个字段 id, sorce.假设sorce的取值范围是 5-10.
按照以下概率实现sorce数据的更新。
5(25%) => 6 5(25%) => 7
6(20%) => 7
7(25%) => 8 7(25%) => 6
8(10%) => 6 8(25%) => 9
9(15%) => 8 9(20%) => 10
10(25%) => 9
要求考虑性能及扩展性。写出概率分布相关代码。
------------------------------
9.
设计一个缓存系统。写出思路。画出图。考虑命中,生存期等多种要素。
答:可以参考memcached
本文出自 “代军-互联网专家” 博客,谢绝转载!