php初级攻城狮面试题

1. php 模拟 post 提交的方法有哪些?

第一种 : file_get_content 模拟 post

第二种 : curl 模拟 post

第三种 : socket 模拟 post

 

2. 用 php 打印出 2016-05-26 09:31:07 的 Unix 时间戳

$time = "2016-05-26 09:31:07";

$unixtime = strtotime($time);

 

3. php 获取客户端 IP 的方法

echo $_SERVER["REMOTE_ADDR"];

 

4. 用 php 实现 302 跳转到 http://www.qq.com

302 是临时重定向的意思 , 表示被访问页面因为各种需要被临时跳转到其他页面

header(Location:http://www.qq.com); exit;

 

5. 匹配邮箱的正则表达式

$email = $_POST["email"];  

$pattern = "/([\w\-]+\@[\w\-]+\.[\w\-]+)/";

if (!preg_match($pattern ,$email)) {  

$emailErr = "邮箱格式不正确!";   

}  

 

6. 如何处理 php 服务的 500 错误?

找到 php的配置文件  /etc/php.ini 

    配置参数值:display_errors = On 

 error_reporting = E_ALL | E_STRICT

  重启apache 服务  service  httpd  restart 

  这个时候 浏览器已经可以输出错误信息了。

  需要再配置下 apache    /etc/httpd/conf/httpd.conf

 在 apache的配置文件最后添加 两行:

php_flag display_errors on

php_value error_reporting 2039 

重启apache。

对php.ini 中参数的设置 也可用在php代码中完成。

调用:调用ini_set()函数

//开启php.ini中的display_errors指令

ini_set('display_errors',1);

//通过error_reporting()函数设置,输出所有级别的错误报告

error_reporting(E_ALL);

这样可用动态的,在指定的php文件中,输出错误。

 

7. 几个常用的 php 字符串函数

implode : 将一个一维数组的值转化为字符串

$array = array('lastname', 'email', 'phone');

$comma_separated = implode(",", $array);

echo $comma_separated; // lastname,email,phone

explode : 使用一个字符串分割另一个字符串 , 返回一个数组

$pizza  = "piece1,piece2,piece3,piece4,piece5,piece6";

$pieces = explode(",", $pizza);

str_repeat : echo str_repeat("=", 10); ==========

str_replace :

str_replace("a", "apple", "I like eat apple."); I like eapplet applepple.

str_split :

$str = "Hello Friend";

$arr1 = str_split($str);

$arr2 = str_split($str, 3);

print_r($arr1);

print_r($arr2);

Array( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => [6] => F [7] => r [8] => i [9] => e [10] => n [11] => d ) Array ( [0] => Hel [1] => lo [2] => Fri [3] => end )

strpos : $mystring = 'abc'; $findme   = 'a'; $pos = strpos($mystring, $findme); 结果为 0

判断 $mystring 中是否包含有 $findme , 有则返回位置, 无则返回 false

注意 : 判断的时候要用 === 例如:

if ($pos === false) 如果不用 === , 返回位置为 0 ,会判断结果等于false

strlen : 需要计算长度的字符串。

$str = 'abcdef'; echo strlen($str); // 6

$str = ' ab cd '; echo strlen($str); // 7

strtolower :

$str = "Mary Had A Little Lamb and She LOVED It So";

$str = strtolower($str);

echo $str; //mary had a little lamb and she loved it so

strtoupper : $str = "Mary Had A Little Lamb and She LOVED It So";

$str = strtoupper($str);

echo $str; //MARY HAD A LITTLE LAMB AND SHE LOVED IT SO

strtotime : 将任何英文文本的日期时间描述解析为 Unix 时间戳

substr :

$rest = substr("abcdef", -1);    // 返回 "f"

$rest = substr("abcdef", -2);    // 返回 "ef"

$rest = substr("abcdef", -3, 1); // 返回 "d"

$rest = substr("abcdef", 0, -1);  // 返回 "abcde"

$rest = substr("abcdef", 2, -1);  // 返回 "cde"

$rest = substr("abcdef", 4, -4);  // 返回 ""

$rest = substr("abcdef", -3, -1); // 返回 "de"

 

8. 常用的 php 数组函数

array_combine : 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值

$a = array('green', 'red', 'yellow');

$b = array('avocado', 'apple', 'banana');

$c = array_combine($a, $b);

print_r($c);

Array ( [green] => avocado [red] => apple [yellow] => banana )

array_column : 返回数组中指定的一列

array_keys : 返回数组中部分的或所有的键名

array_values : 返回数组中所有的值

array_merge : 合并一个或多个数组

$array1 = array("color" => "red", 2, 4);

$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);

$result = array_merge($array1, $array2);

print_r($result);

Array ( [color] => green [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 )

array_push : 将一个或多个单元压入数组的末尾(入栈)

$stack = array("orange", "banana");

array_push($stack, "apple", "raspberry");

print_r($stack);

Array ( [0] => orange [1] => banana [2] => apple [3] => raspberry )

array_rand : 返回数组中的随机键名

$a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); print_r(array_rand($a,2));

Array ( [0] => a [1] => c )

array_slice :

1. $a=array("red","green","blue","yellow","brown"); print_r(array_slice($a,2));

从数组的第三个元素开始取出,并返回数组中的其余元素:

Array ( [0] => blue [1] => yellow [2] => brown )

2. $a=array("red","green","blue","yellow","brown"); print_r(array_slice($a,1,2));

从数组的第二个元素开始取出,并仅返回两个元素:

Array ( [0] => green [1] => blue )

3. $a=array("red","green","blue","yellow","brown"); print_r(array_slice($a,-2,1));

Array ( [0] => yellow )

array_sum : 返回数组中所有值的和

$a=array("a"=>52.2,"b"=>13.7,"c"=>0.9); echo array_sum($a); 66.8

 

9. php写文件的方法有哪些?

一 . 语法:fwrite(file,string,length)

  1. <span style="font-size:18px;">
  2. <?php  
  3.     header("Content-Type:text/html;Charset=utf-8");  
  4.     $file_path="test.css";  
  5.     if(file_exists($file_path)){  
  6.         //如果是追加内容,则使用a+ append  
  7.         //如果是全新的写入到文件 ,则使用 w+ write  
  8.         $fp=fopen($file_path,"a+");  
  9.         $con="\r\n你好!";  
  10.         for($i=0;$i<10;$i++){  
  11.             fwrite($fp,$con);  
  12.        }  
  13.        echo "添加ok";  
  14.        fclose($fp);  
  15.     }else{  
  16.         echo "文件不存在!";  
  17.     }  
  18. ?>
  19. </span>  

二 . file_put_contents()

  1. <span style="font-size:18px;">
  2. <?php  
  3.     header("Content-Type:text/html;Charset=utf-8");  
  4.     $file_path="test.txt";  
  5.     $con="北京你好!\r\n";  
  6.     for($i=0;$i<10;$i++){  
  7.         $con.="北京你好!\r\n";  
  8.     }  
  9.     //请不要循环的使用该函数 。  
  10.     file_put_contents($file_path,$con,FILE_APPEND); //fopen fwrite fclose  
  11.     echo "ok";  
  12. ?>
  13. </span>  

10. file_get_contents('php://input') 是什么意思?

获取远程内容

php://input 是个可以访问请求的原始数据的只读流。

11. 优化 mysql 数据库的方法有哪些 (重点看 9-16 条)

1、创建索引

2、复合索引

3、索引不会包含有NULL值的列

4、使用短索引

5、排序的索引问题

6、like语句操作

7、不要在列上进行运算

8、不使用NOT IN和<>操作

9. 选取最适用的字段属性

10. 使用连接(JOIN)来代替子查询(Sub-Queries)

11. 使用联合(UNION)来代替手动创建的临时表

12. 事务

13. 锁定表

14. 使用外键

15. 使用索引

16. 优化的查询语句

12. 简述 mysql 多表关联查询的方式和区别

 

13. memcache , redis 分别支持哪些数据类型

memcache : Memcached仅支持简单的key-value结构的数据

redis :

1. string 2. hash 3. list 4. set 5. sort set 6. transaction

14 . git 和 svn 有何不同?

1. git 是分布式 , svn 是集中式

2. git 把内容按元数据方式存储 , 而 svn 是按文件

3. git 分支和 svn 的分支不同

4. git 没有一个全局的版本号 , 而 svn 有

5. git 的内容完整性要优于 svn

GIT的内容存储使用的是SHA-1哈希算法。这能确保代码内容的完整性,确保在遇到磁盘故障和网络问题时降低对版本库的破坏

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章