Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么PHP在数组中包含多个类型?_Php - Fatal编程技术网

为什么PHP在数组中包含多个类型?

为什么PHP在数组中包含多个类型?,php,Php,我是PHP脚本的初学者。据我所知,数组意味着相同类型元素的集合,但PHP违反了该规则 这是我的密码: <!DOCTYPE html> <html> <body> <?php $a = 10; echo $a; $arr = array(); for($i = 0; $i < 10; $i++){ $arr[$i] = $i * $i; /

我是PHP脚本的初学者。据我所知,数组意味着相同类型元素的集合,但PHP违反了该规则

这是我的密码:

    <!DOCTYPE html>
<html>
<body>
    <?php 
        $a = 10; 
        echo $a;

        $arr = array();

        for($i = 0; $i < 10; $i++){
            $arr[$i] = $i * $i; // Integer values
        }

        $arr[10]="Rohit"; // string value
        $arr[11] = 10.15; // float value
        $arr[12] = true; // boolean value

        for($i = 0; $i < COUNT($arr); $i++){
            echo "<br>".$arr[$i];
        }

        echo "<br>Length of array: ".COUNT($arr);
        echo "var_dump output: ".var_dump($arr);
    ?>
</body>
</html>
您可以注意到前10个元素是整数,第10个元素是字符串,第11个元素是浮点,第12个元素是布尔


请任何人解释一下为什么PHP有如此令人困惑的行为?

PHP是一种具有动态类型系统的语言。与大多数动态语言(如Python、Javascript)一样,Ruby默认情况下不检查任何类型。这实际上只是其他一切工作方式中的紧急行为。

每种编程语言都是不同的,可能与您的问题没有直接关系,但这里有一篇有趣的文章,可以帮助您了解PHP数组以及它们与许多其他语言中的数组的区别:@Mike您了解了我的问题,我也了解了我的问题的解决方案。在PHP中,数组是一个有序的哈希映射,是的,它与其他语言完全不同。谢谢你,迈克。你的共享文章很有用,我得到了我问题的答案。那是什么语言?那个页面是说“学习PHP”还是“学习java”?你为什么要在互联网上阅读bull cough网站?为什么不使用手册呢?www.php.net阅读。如果您在那里,您会看到所有示例都显示了值和键上的字符串和整数的混合。但保持这些类型的动态性背后可能有原因,我希望有这个原因。然后,根据学习材料中给出的定义,数组概念变得完全混乱。你的学习材料必须关注其他类型的语言。对于像PHP这样的动态语言,强制使用特定类型是没有意义的,因为这种情况在语言中没有发生过。只有在PHP5之后才存在类型检查。我使用W3School和PHP文档来理解PHP。我理解你的答案,但没有得到我问题的答案。使用动态数组背后可能有原因,我不明白原因是什么?为什么?他们为什么以这种方式实现阵列?
10
0
1
4
9
16
25
36
49
64
81
Rohit
10.15
1
Length of array: 13

C:\wamp\www\phpModules\check16_6_18_study.php:28:
array (size=13)
  0 => int 0
  1 => int 1
  2 => int 4
  3 => int 9
  4 => int 16
  5 => int 25
  6 => int 36
  7 => int 49
  8 => int 64
  9 => int 81
  10 => string 'Rohit' (length=5)
  11 => float 10.15
  12 => boolean true

var_dump output: