PHP精选面试题

一、echo、print、print_r的区别
1、echo是php语句,print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用)
     print只能打印出简单类型变量的值(如int,string)
     print_r可以打印出复杂型变量的值(如数组,对象)

2、echo--输出一个或者多个字符串
     print--输出一个字符串
     print_r--打印关于变量的易于理解的信息

二、用php打印出前一天的时间格式

  echo date("Y-m-d H:i:s",strtotime('-1 days'));

三、php常用字符串函数
1、确认字符串长度,strlen函数和mb_strlen函数,后者需要开启mbstring扩展
<?php
header('content-type:text/html;charset=utf-8');
$str = 'abcdef';
echo strlen($str); // 6
echo "<br/>";
$str = ' ab cd ';
echo mb_strlen($str); // 7
echo "<br/>";
//strlen 是计算字符串"字节"长度
//mb_strlen,是根据编码,计算字符串的"字符"个数.

$str='中华人民共和国';
echo "字节长度是".strlen($str);//在 UTF-8编码下,一个汉字占3个字节 在gbk中一个汉字占2个字节 21
echo "<br/>";
echo "字符长度是".mb_strlen($str,'utf-8'); // 7
?>

2、比较字符串,strcmp函数、strcasecmp函数、strspn函数、strcspn函数
     

3、分割连接反转
     str_split函数、split函数、explode函数和implode函数
<?php
header('content-type:text/html;charset=utf-8');
$str = "Hello Friend";

$arr1 = str_split($str);
print_r($arr1); // Array ( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => [6] => F [7] => r [8] => i [9] => e [10] => n [11] => d )

$arr2 = str_split($str, 3);
print_r($arr2); // Array ( [0] => Hel [1] => lo [2] => Fri [3] => end )

$str = 'abc,中国,美国,日本';
// explode,是根据指定的分割符,把字符串拆成数组.
$arr = explode(',',$str);
print_r($arr); // Array ( [0] => abc [1] => 中国 [2] => 美国 [3] => 日本 )

// implode,是根据指定的连接符,把数组再拼接成字符串
$arr = explode(',',$str);
echo implode('~',$arr),'<br />'; // abc~中国~美国~日本
// 你可以只传一个数组做参数,不指定连接符,
// 这样,将把数组单元直接拼接起来
echo implode($arr); //abc中国美国日本

?>
4、html与字符串相互转化
      htmlspecialchars函数、strip_tags函数、get_html_translation_table函数和addcslashes函数和htmlentities函数


5、填充和剔除字符串
     trim函数、ltrim函数、rtrim函数、str_pad函数、chunk_split函数

6、统计字符和单词个数
     count_chars函数和str_word_count
<?php
$data = "Two Ts and one F.";

foreach (count_chars($data, 1) as $i => $val) {
echo "There were $val instance(s) of \"" , chr($i) , "\" in the string.\n";
}

echo "<hr>";
$str = "Hello fri3nd, you're looking good today!";

print_r(str_word_count($str, 1)); // Array ( [0] => Hello [1] => fri [2] => nd [3] => you're [4] => looking [5] => good [6] => today )

?>
7、查找替换截取
      strpos函数、str_replace函数、substr_replace函数、substr函数、strstr函数
<?php
$substr = "index.html";
$log = <<< logfile
192.168.1.11:/www/htdocs/index.html:[2016/08/10:21:58:27]
192.168.1.11:/www/htdocs/index.html:[2016/08/18:01:51:37]
192.168.1.11:/www/htdocs/index.html:[2016/08/20:11:48:27]
logfile;

$pos =strpos($log, $substr);
$pos2=strpos($log,"\n",$pos);
$pos=$pos+strlen($substr)+1;
$timestamp=substr($log,$pos,$pos2-$pos);
echo "The file $substr was first accessed on:$timestamp";
echo "<br>";
$author="lester@example.com";
$author=str_replace("@", "at", $author);
echo "connect the author of this article at $author";
echo "<br>";
echo ltrim(strstr($author,"@"), "@");

?>

8、大小写处理
      strtolower函数、strtoupper函数、ucfirst函数、ucwords函数
<?php
$url="http://WWWW.BAIDU.COM";
echo strtolower($url),'<br>'; // http://wwww.baidu.com
$str="hello world";
echo strtoupper($str),'<br>'; // HELLO WORLD
$str="php is the most popular language ";
echo ucfirst($str),'<br>'; // Php is the most popular language
echo ucwords($str); // Php is the most popular language
?>
四、用php写出显示客户端ip与服务器ip的代码
      打印客户端IP:echo $_SERVER['REMOTE_ADDR']; 或者:getenv('REMOTE_ADDR');
      打印服务端IP:echo gethostbyname('www.bolaiwo.com');

五、语句include和require的区别是什么?为避免多次包含同一文件,可用(?)语句代替它们?
       require->require 是无条件包含也就是如果一个流程加入require,无论条件成立与否都会先执行require
       include->include有返回值,而require没有(可能因为如此require的速度比include快)
  
  注意:包含文件不存在或者语法错误的时候require是致命的,include不是
   require_once;和include_once;

六、session与cookie的区别

       session:储存用户访问的全局唯一变量,存储在服务器上的php指定的目录中的(session_dir)的位置进行的存放
       cookie:用来存储连续访问一个页面时所使用,是存储在客户端,对于cookie来说是存储在用户WIN的Temp目录中的
  两者都可通过时间来设置时间长短

七、有一个网页地址,比如php开发资源网主页:http://www.phpres.com/index.html,如何得到它的内容?

    方法一:$readcontents = fopen("http://www.phpres.com/index.html","rb");  
                  $contents = stream_get_contents($readcontents);
                  fclose($readcontents);
                  echo $contents;
   方法二:
              echo file_get_contents("http://www.phpres.com/index.html");

八、在HTTP1.0中,状态码401的含义是(?);如果返回"找不到文件"的提示,则可用header函数,其语句为(?);
          状态401代表未被授权,header("Location:www.xxx.php");

九、mysql_fetch_row()和mysql_fetch_array()之间的区别

     mysql_fetch_row是从结果集取出1行数组,作为枚举
     mysql_fetch_array是从结果取出一行数组作为关联数组,或数字数组,两者兼得

十、写一个函数,尽可能高效的,从一个标准url里取出文件的扩展名
       例如:http://www.sina.com.cn/abc/de/fg.php?id=1需要取出php或.php

              function geExt($url){
                      $arr = parse_url($url);
                      $file = basename($arr['path']);
                      $ext = explode(".",$file);
                       return $ext[1];
               }
注:parse_url函数解析一个URL并返回一个关联数组,包含在URL中出现的各种组成部分。
       basename函数返回路径中的文件名部分。

十一、写一个函数,能够遍历一个文件夹下的所有文件和子文件夹           

       function dir_recurse($dir){
             $i=1;
            if($handle=opendir($dir)){
                while(false!==($file=readdir($handle))){
                     if($file!="."&&$file!="--"){
                        $fullpath=$dir."/".$file;
                        dir_recurse($fullpath);
                        echo"$fullpath\n";
                        $i++;   
                  } else {
                  $fullpath = $dir."/".$file;
                   echo"$fullpath\n";
                    $i++;
                  }
                }  
             }
          }













    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值