PHP Interview Question Bank

//1. Print the previous day’s time in PHP in the format 2006-5-10 22:21:22
//Method 1

1
2
date\_default\_timezone\_set('Asia/Shanghai'); 
$times=time()-86400; echo date('Y-m-d H:i:s',$times)."<br />"; //Method 2 echo date('Y-m-d H:i:s', strtotime('-1 days'));

  1. The difference between echo(), print, and print_r() — echo can only print strings; print can print strings and the specified value in an array; print_r prints all the values in an array but cannot print strings

//3. How to reverse a string echo “
“;

1
2
3
$str=qrewwewe;
$st=Strrev($str);
echo $st;

  1. Methods for amp to achieve maximum load: adopt a caching mechanism: static caching, memcache, etc. Depending on the transaction processing mechanism, use innDB tables or mylsam tables. Optimize SQL statements, optimize the table field structures.
    1
    2
    3
    4
    5
    echo "<br />";
    $b=201;
    $c=40;
    $a=$b>$c?4:5;
    // In PHP, if the value before ? is true, the result takes the value before :; otherwise it takes the value after : echo $a;
  2. The difference between pass-by-value and pass-by-reference in PHP: pass-by-value simply passes the value of one variable to another variable, whereas a reference means the two point to the same place.

  3. What does the error_reporting function do in PHP? error_reporting() is used to specify the severity level of error reporting during a PHP program’s execution

  4. Please write a function that verifies whether an email address’s format is correct

    1
    2
    3
    function CheckMaiAdr($str){ 
    return(eregi("^\[\_.0-9a-z\]+@(\[0-9a-z\]\[0-9a-z\]+.)+\[a-z\]{2,3}$",$str));
    }

    Or

    1
    2
    3
    function CheckMaiAdr($str){
    return(eregi("^\[A-Za-z0-9_.\]+@(\[A-Za-z0-9_\]+\\.\[A-Za-z0-9.\]+{2,3}$",$str));
    }
  5. Given a web page address http://www.domain.com/xxx.php, how do you get its contents? $files = file_get_contents(http://www.domain.com/xxx.php);

  6. There is a one-dimensional array containing integer data. Please write a function that sorts them in descending order. //High execution efficiency is required. Also explain how to improve execution efficiency. (The function must be implemented yourself; you may not use PHP functions)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    $str=123467890; 
    function BubbleSort($str){
    for($i=0;$i<count($str);$i++){
    for($j=count($str)-2;$j>=$i;$j--){
    if($str\[$j+1\]<$str\[$j\]){
    $tmp=$str\[$j+1\]; $str\[$j+1\]=$str\[$j\]; $str\[$j\]=$tmp;
    }
    }
    }
    return $str;
    }
  7. Write the SQL for the names of the ten people with the most posts, using the following table: members(id,username,posts,pass,email) Select username from members order by posts desc limit 0,10 **/

  8. Get the current time

    1
    echo "<br />"; echo date('Y-m-d H:i:s',time());//get the current time 
  9. Assume a.html and b.html are in the same folder. Use JavaScript to make it so that 5 seconds after a.html is opened, it automatically redirects to b.html.

    1
    <script>setTimeout(“window.location.href =’http://www.baidu.com’”,5000)</script> 
  10. Write a piece of PHP code that swaps the values of $a and $b without using a third variable. Set the initial values of $a and $b yourself.

    1
    2
    3
    4
    5
    6
    echo "<br />"; $a="abcd"; $b="1234";
    echo "初始化时 a=$a,b=$b<br />";
    $a=$a.$b; $b=strlen($b);//strlen gets the length of a string
    $b=substr($a,0,(strlen($a)-$b));//substr returns part of a string
    $a=substr($a,strlen($b));
    echo "交换后 a=$a,b=$b<br />";
  11. Write a function that can traverse all files and subfiles under a folder

    1
    2
    3
    4
    5
    6
    7
    $d=dir(dirname(\_\_file\_\_));
    echo "Handle:".$d->handle."\\n";
    echo "Path:".$d->path."\\n";
    while(false !==($entry=$d->read())){
    echo $enty."<br />";
    }
    $d->close();
  12. Briefly describe how to get the path of the currently executing script, including the parameters obtained $script_name=basename(__file__);

    1
    print\_r($script\_name);
  13. The function that checks whether a variable is set, and the function that checks whether it is empty

1
isset($str) empty($str); 
  1. The difference between the get and post submission methods in a form? get sends a request over the HTTP protocol and is received through URL parameter passing; post is entity data and can submit large amounts of information through a form

  2. The difference between session and cookie: session stores the globally unique variables of a user’s visit, kept in a location in a PHP-specified directory on the server; cookie is used to store what is used when continuously visiting a page, and it is stored on the client — for cookies that means a specified directory on the user’s computer

  3. What is a transaction in a database? A transaction is a group of ordered database operations treated as a single unit. If all operations in the group succeed, the transaction is considered successful; even if only one operation fails, the transaction is not successful. If all operations complete, the transaction commits, and its changes take effect for all other database processes. If one operation fails, the transaction rolls back, and the effects of all operations in that transaction are cancelled.

  4. The meaning of php: php is a server-side scripting language for creating dynamic websites.

  5. Implement a method to truncate Chinese strings without garbled characters

    1
    function GBsubstrat($string,$start,$length){ if(strlen($string)>$length){ $str=null; $len=$start+$length; for($i=$start;$i<$len;$i++){ if(ord(substr($string,$i,1))>0xa0){ $str.=substr($string,$i,2); $i++; }else{ $str.=substr($string,$i,1); } } return $str.'......'; }else{ return $string; } } 
  6. Write PHP code that displays the client IP and the server IP (1 point)

Answer: Print the client IP: echo $_SERVER[‘REMOTE_ADDR’]; or: getenv(‘REMOTE_ADDR’); Print the server IP: echo gethostbyname(“www.bolaiwu.com”) **/ echo gethostbyname(“erik.xyz”);

  1. Talk about your understanding of MVC: an application made up of the model, the view, and the controller

The model sends the functionality to be implemented to the controller; the controller receives and organizes the functionality and passes it to the view

  1. What is the difference between mysql_fetch_row() and mysql_fetch_array

mysql_fetch_row takes one row of the array from the result set as an enumerated array; mysql_fetch_array takes one row of the array from the result set as an associative array, or a numeric array — it gets you both

  1. What is the GD library for

The gd library provides a series of APIs for handling images. With the gd library you can process images or generate images. On websites, the gd library is usually used to generate thumbnails, or to add watermarks to images, or to generate reports from website data

  1. Please write PHP 5’s constructor and destructor

__contstrut,__destruct Note that there are 2 underscores

  1. Write a function that, as efficiently as possible, extracts the file extension from a standard url
    1
    2
    3
    4
    5
    6
    function getExt($url){ 
    $arr=parse\_url($url);
    $file=basename($arr\['path'\]);
    $ext=explode(".",$file);
    return $ext\[1\];
    }
  2. Singleton pattern requirement: only one object may be produced by instantiation. How to implement it: privatize the constructor and forbid cloning the object. Provide a public static method to access this instance, thereby returning the unique object. A static property holding the class is needed. class demo{ private static $MyObject;//the static property that holds the object
    1
    2
    3
    4
    5
    6
    7
    8
    private  function \_\_construct(){//privatize the constructor }
    private function \_\_clone(){//forbid cloning }
    public static function getlnstance(){
    if(!(self::$MyObject instanceof self)){
    self::$MyObject=new self;
    }
    return self::$MyObject;
    }