PHP classic interview questions, there are answers

Posted by tomo11 on Wed, 09 Feb 2022 10:10:16 +0100

1. There are several ways to merge two arrays. Try to compare their similarities and differences
Method:
1,array_merge()
2,'+'
3,array_merge_recursive
Similarities and differences:
array_merge simple merge array
array_merge_recursive merges two arrays. If there are exactly the same data in the array, they are merged recursively
array_combine and '+': merge two arrays with the former value as the key of the new array

2. Please write a function to check whether the data submitted by the user is an integer (regardless of data type, it can be binary, octal, decimal and hexadecimal digits)
A: in fact, it's mainly is_int and floor

if(!is_numeric($jp_total)||strpos($jp_total,".")!==false){  
    echo "Not an integer";  
}else{  
    echo "Is an integer";  
}

3. The strtower() and strtoupper() functions of PHP may cause the conversion of Chinese characters into garbled code under the server installed with non Chinese system. Please write two alternative functions to realize the case conversion of string compatible with Unicode characters
A: the reason is that Chinese is composed of multiple bytes, and only one byte of a single English character in the English system, so the system processes every byte of Chinese with strtower (), and the changed Chinese bytes are spliced together into garbled code (the character corresponding to the newly formed coding mapping may not be Chinese)
Manual solution: STR_ Split (string, string, intstring, intsplit_length = 1) is cut according to each byte. Like Chinese, it can be cut into three bytes. If the recognized byte is an English letter, it will be converted.

<?php  
function mystrtoupper($a){  
    $b = str_split($a, 1);  
    $r = '';  
    foreach($b as $v){  
        $v = ord($v);  
        if($v >= 97 && $v<= 122){  
            $v -= 32;  
        }  

        $r .= chr($v);  
    }  

    return $r;  
}  

$a = 'a You go on F@#$%^&*(BMDJFDoalsdkfjasl';  
echo 'origin string:'.$a." ";  
echo 'result string:';  
$r = mystrtoupper($a);  
var_dump($r);

4. PHP is_ There is a Bug in the writeable() function, which cannot accurately judge whether a directory / file is writable. Please write a function to judge whether the directory / file is absolutely writable
A: there are two aspects of bug s,
1. In windows, when the file has only read-only attribute, is_ The writeable() function returns false. When it returns true, the file is not necessarily writable.
If it is a directory, create a new file in the directory and judge by opening the file;
If it is a file, you can test whether the file is writable by opening the file (fopen).

2. In Unix, when safe is enabled in the php configuration file_ Mode (safe_mode=on), is_writeable() is also unavailable.
Is the configuration file safe to read_ Whether mode is on.

/**
* Tests for file writability
*
* is_writable() returns TRUE on Windows servers when you really can't write to
* the file, based on the read-only attribute. is_writable() is also unreliable
* on Unix servers if safe_mode is on.
*
* @access   private
* @return   void
*/

if ( ! function_exists('is_really_writable'))
{
    function is_really_writable($file){

    // If we're on a Unix server with safe_mode off we call is_writable
    if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE){
        return is_writable($file);
    }

    // For windows servers and safe_mode "on" installations we'll actually
    // write a file then read it. Bah...
    if (is_dir($file)){

        $file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100));

        if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE){
            return FALSE;
        }

        fclose($fp);
        @chmod($file, DIR_WRITE_MODE);
        @unlink($file);
        return TRUE;

    } elseif ( ! is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE) {

        return FALSE;
    }

    fclose($fp);
    return TRUE;

    }

}

5. There is a Bug in the chmod() function of PHP, which cannot guarantee the successful setting. Please write a function to create a directory / file under the specified path and ensure that the permission mask can be set correctly
A: I can't find the answer

6. PHP handles the file types in the uploaded file information array$_ FILES ['type'] is provided by the client browser. It may be information forged by hackers. Please write a function to ensure that the image file type uploaded by users is true and reliable
A: use getimagesize to determine the type ratio of uploaded pictures$_ The type of the FILES function is more reliable
For the same file, the type returned by php using different browsers is different. If the browser provides the type,
It may be used by hackers to submit an executable file disguised as a picture suffix to the server.
You can use the getimagesize() function to determine the type of uploaded file. If it is a avatar file, such an array will be returned

Array(
    [0] => 331
    [1] => 234
    [2] => 3
    [3] => width="331" height="234"
    [bits] => 8
    [mime] => image/png

);

If such an array is returned through the getimagesize() function, it indicates that the uploaded avatar file is. Where the index is 2
1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte
order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 =
JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM,

You can use this to limit the type of avatar uploaded

<?php
    $file=$_FILES['file'];
    if(!empty($file))
    {
       var_dump($file);
       var_dump(getimagesize($file["tmp_name"]));

    }   
?>

7. PHP realizes the data interaction with Javascript by encoding the URL of the data, but the encoding and decoding rules of some special characters are different from those of Javascript. Please specify the difference, and write the encoding and decoding function of PHP and the encoding and decoding function of Javascript for the data of UTF-8 character set to ensure that the PHP encoded data can be correctly decoded by Javascript JavaScript encoded data can be decoded correctly by PHP
Answer:

<?php
 $str = 'Siyuan blog siyuantlw/tlw/sy/I'm just a soy sauce maker';
 $str = iconv("GB2312",'UTF-8',$str);
 $str = urlencode($str); 

?>

//JS decodeuricomponent does not seem to recognize the format of GB2312 encoding. It must be converted to utf-8. Then, if there are spaces in the string, it will be converted to + sign

<html>
 <script>
  var ds = '<?php echo $str;?>';
  var dddd= decodeURIComponent (ds);
  alert(dddd);
 </script>
</html>

Topics: PHP