PHP Interview Questions: Array Merging, Integer Validation, Unicode Case Conversion, File Writability, Permission Handling, Upload Validation, and URL Encoding Differences

This article presents a series of PHP interview questions covering array merging methods, integer validation, Unicode‑compatible case conversion, reliable file‑writability checks, permission‑setting functions, secure image upload verification, and differences between PHP and JavaScript URL encoding, each accompanied by code examples.

php Courses
php Courses
php Courses
PHP Interview Questions: Array Merging, Integer Validation, Unicode Case Conversion, File Writability, Permission Handling, Upload Validation, and URL Encoding Differences

1. Merge two arrays – The common ways are array_merge(), the + operator, and array_merge_recursive(). array_merge simply concatenates values, array_merge_recursive merges recursively when keys overlap, and the + operator (or array_combine) uses the left‑hand array’s values as keys for the resulting array.

1、array_merge()</code>
<code>2、+</code>
<code>3、array_merge_recursive

2. Check if a user‑submitted value is an integer – Use is_int or floor logic, for example:

if(!is_numeric($jp_total)||strpos($jp_total,".")!==false){
    echo "不是整数";
}else{
    echo "是整数";
}

3. Unicode‑compatible case conversion – Because strtolower() and strtoupper() may corrupt multibyte Chinese characters on non‑Chinese systems, a custom implementation can split the string into bytes and convert only ASCII letters:

<?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中你继续F@#$%^&*(BMDJFDoalsdkfjasl';
echo 'origin string:'.$a." ";
$r = mystrtoupper($a);
var_dump($r);
?>

4. Reliable writability test – is_writable() is unreliable on Windows with read‑only attributes and on Unix when safe_mode is on. A robust function creates a temporary file (or opens the target) to verify actual write permission:

<?php
/**
 * Tests for file writability
 */
if (!function_exists('is_really_writable')) {
    function is_really_writable($file) {
        if (DIRECTORY_SEPARATOR == '/' && @ini_get("safe_mode") == FALSE) {
            return is_writable($file);
        }
        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) || ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE) {
            return FALSE;
        }
        fclose($fp);
        return TRUE;
    }
}
?>

5. Ensure chmod() succeeds – The author notes no reliable solution was found, indicating a gap in standard PHP permission handling.

6. Verify uploaded image file type – Rely on getimagesize() rather than the client‑supplied $_FILES['type']. The function returns an array where index 2 indicates the image type (1 = GIF, 2 = JPG, 3 = PNG, etc.). Example usage:

Array(
    [0] => 331,
    [1] => 234,
    [2] => 3,
    [bits] => 8,
    [mime] => image/png
);

And a simple upload check:

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

7. PHP vs. JavaScript URL encoding differences – PHP’s urlencode() works on UTF‑8 strings, while JavaScript’s decodeURIComponent expects UTF‑8 and converts spaces to ‘+’. Example PHP encoding:

<?php
$str = '思源博客siyuantlw/tlw/sy/俺只是一个打酱油的';
$str = iconv("GB2312", 'UTF-8', $str);
$str = urlencode($str);
?>

And the corresponding JavaScript decoding:

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

The article concludes with links to related PHP interview question collections.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Securityinterviewfile I/OArray
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.