Home > Backend Development > PHP Tutorial > 关于数组的一点困惑解决方法

关于数组的一点困惑解决方法

WBOY
Release: 2016-06-13 13:35:45
Original
887 people have browsed it

关于数组的一点困惑

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
$arr = array();
$num='hello world';

if(isset($num)) {
    $arr = $num;
    var_dump($arr);
} else {
    $arr[] = array(1,2,3);
    var_dump($arr);
}

Copy after login

运行后再浏览器显示的是字符串型
HTML code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->string(11) "hello world" 
Copy after login

可我已经设置了arr是数组,不懂内在原因,还是$arr = $num;这句已经重新声明了个变量arr???

------解决方案--------------------
这就是弱类型……

$str = "123";
echo $str[0];//这样子也没问题的,输出1
------解决方案--------------------
变量的类型可随上下文自动变换,这就是所谓的弱类型

你有 $arr = $num; 于是 $arr 就变成字符串了
------解决方案--------------------
PHP的类型随时会变的,要强制类型转换才行,像这样:var_dump((array)$arr);
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template