php之面试汇总
--------------引用变量<?php
$a=1;
$b=&$a;
//echo $b; 1
$b=3;
//echo $a; 3
$c=&$b;
//echo $c; 3
$c=4;
echo $a; //4
echo $b; //4
echo $c; //4
------------isset empty
isset --- 测定变量是否设定.
若参数var存在则传回true,否则传回false。
empty --- 测定变量是否设定.
若变量存在而且有设定变量值则传回false,否则传回true。
当用在任何不是变量上时,是无意义的。例如 : empty (addslashes($name))是无意义的。
------------------------------strstr
string strstr (string haystack, string needle)
找出字符串第一次出现的地方
传回参数 needle在 haystack中,第一次出现处到 haystack结尾的字符串。如果没有找到 needle则传回 false。如果参数 needle不是字符串时,它会转换成整数并且按照字元的顺序值来使用。注意 : 此函数是会区分大小写的,要不区分大小写的搜寻可以使用stristr( )
-------------------------------array_flip
array_flip --- 交换数组中的键和值 语法 : array array_flip(array trans);说明 : 此函数传回翻转(flip)顺序的数组。Example :
<?php
$a=array("a","b","c");
print_r(array_flip($a)); //Array ( => 0 => 1 => 2 )
----------------------------------array_shift array_push
array_shift -- 将数组开头的单元移出数组
<?php
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack);
print_r($stack);
?>
这将使 $stack 剩下 3 个单元:
Array
(
=> banana
=> apple
=> raspberry
)
array_push -- 将一个或多个单元压入数组的末尾(入栈)
<?php
$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
print_r($stack);
?>
本例将使 $stack 具有如下单元:
Array
(
=> orange
=> banana
=> apple
=> raspberry
)
------------------写一条SQL删除两个表中字段name相同且都为“Sue”
DELETE data,data2 FROM data,data2 WHERE data.name=data2.name AND data.name='Sue';
------------------------------------------获取URL
1 如何从域名http://www.php.net/index.html中取得主机名www.php.net
//方法一(用系统变量)
//缺点不使用传递过来的地址和不支持系统变量的主机
echo $_SERVER['HTTP_HOST'];
//方法二(用自带函数)
$url = 'http://www.51php.net/index.php?referer=51php.net';
$arr_url = parse_url($url);
echo $arr_url['host'];
//方法三(自己写函数)
function getdomain($url)
{
$url = str_replace('http://','',$url); //如果有http前缀,则去掉
$pos = strpos($url,'/');
if($pos === false)
{
return $url;
}else
{
return substr($url, 0, $pos);
}
}
echo getdomain($url);
//方法四(用正则)
preg_match("/^(http://)?([^/]+)/i", $url, $arr_domain);
echo $arr_domain;
2 如何用php的环境变量得到一个网页http://www.***.cn/test/index.php地址的内容?ip地址又要怎样得到?
解:网页地址内容:
$_SERVER['REQUEST_URI']
$_SERVER['PHP_SELF'] // /test/index.php
3 IP地址:
$_SERVER['HTTP_X_FORWARDED_FOR']
$_SERVER['HTTP_CLIENT_IP']
$_SERVER['REMOTE_ADDR']
4 当前执行脚本的绝对路径名
/var/www/html/**/test/index.php
echo $_SERVER['SCRIPT_FILENAME'];
echo __FILE__ ;
-----------------------请写一个函数,实现以下功能:字符串“open_door” 转换成“OpenDoor”、” make_by_id” 转换成 ”MakeById”。
解: function convString($string)
{
$array = explode('_', $string);
array_walk($array, 'ucwords');
return implode('', $array);
}
------------------------用户的工具:Rose,PowerDesigner,Project,CVS
Rose侧重于软件建模
PowerDesigner侧重于数据库建模
Project仅用于项目监控 进程,而一个进程可以包含若干线程
------------------------------------------------------ceil floor round
ceil --- 取得大于指定数的最小整数值
echo ceil(1.1);//2
floor --- 取得小于指定数的最大整数值
echo floor(1.1);//1
round --- 取四拾五入
语法 : double round(double val [ , int precision] );
说明 :
传回四拾五入后val的值。
Example :
<?php
$foo = round( 3.4 ); // $foo == 3.0
$foo = round( 3.5 ); // $foo == 4.0
$foo = round( 3.6 ); // $foo == 4.0
?>
-------------------------计算两个日期相差的 年月日
$data="2007-4-13";
$data2="2009-4-13";
echo (int)((strtotime($data2) - strtotime($data))/(3600*24*30*12))."年";
echo (int)((strtotime($data2) - strtotime($data))/(3600*24*30))."月";
echo (int)((strtotime($data2) - strtotime($data))/(3600*24))."日";
----------------------------------写一个函数,能够遍历一个文件夹下的所有文件和子文件夹
<?
function show_list($path){
if(is_dir($path)){
$dp=dir($path);
while($file=$dp->read())
if($file!='.'&&$file!='..')
show_list($path.'/'.$file);
$dp->close();
}
echo "$path<br>";
}
show_list('phpQuery');
?>
页:
[1]