PHP 函数中的奇怪行为:根据英语/西里尔文本显示/不显示文本.为什么


PHP strange behavior in function: displays/does not display text depending on English/Cyrillic text. Why?

有两个文件run.php和class/functions.php用于在javascript事件上的html页面上放置一些文本(AJAX用于此)。

运行.php文件为:

<?php
mb_internal_encoding("UTF-8");
require_once('class/functions.php');
$text = array();
$text[0]="rrrrrr";
strange_func($text, 0);//puts "фа" into $text[0]
$res1=$text[0];
$arr = array();
$arr['res1'] = $res1;
$arr['res2'] = "test";
header('Content-type: application/json');
echo json_encode($arr);
?>

类/函数.php包含:

function strange_func(&$text, $i){
    $text[$i]="hello123";//hello123 will be displayed on html page on javascript event
    //$text[$i]="фь";
    //if we comment hello123 and uncomment фь, then фь will NOT be displayed on the html page on javascript event by unknown reason. Why?
}

此外,如果我require_once('class/functions.php');

function strange_func(&$text, $i){
    $text[$i]="фь";
}

,则将显示"фь"。那么,如果文本是西里尔文,为什么从class/functions.php strange_func函数不显示文本?

上面的代码是简化的结果,没有太多意义。但是,问题仍然存在。

使用 utf-8 编码保存(不带 BOM)