Skip to Content
PHP编程:第4版
book

PHP编程:第4版

by Kevin Tatroe, Peter MacIntyre
January 2021
Intermediate to advanced
505 pages
9h 45m
Chinese
Publishing House of Electronics Industry
Content preview from PHP编程:第4版
识别数组元素
127
都不能让两个元素使用相同的键。
PHP
数组中的元素有一种“不依赖于键和值的”内部顺序,很多用于遍历数组的函数都
基于这个内部顺序,这个顺序一般就是把值插入数组的顺序。但是稍后介绍的各种排序
函数,可以让你基于键、值或其他属性来改变元素的顺序。
识别数组元素
在了解如何创建一个数组之前,我们先来看一个已经存在的数组的结构。可以使用数组
变量名字后面跟括在中括号中的元素键(有时也称作索引,
index
)来访问数组中特定的值:
$age['fred']
$shows[2]
键可以是一个字符串或一个整数,等价于整数(但不以零开头)的字符串值被当作整数
对待,因此,
$array[3]
$array['3']
引用的是相同的元素,而
$array['03']
引用的
是另一个不同的元素。负数也是有效的键,但是它们不像在
Perl
中那样从数组的末尾逆
序地指定位置。
不一定非要将单个词的字符串加上引号,例如,
$age['fred']
$age[Fred]
是相同的。
但是,使用引号是良好的
PHP
编程风格,因为没有引号的键不能和常量区别开来。当将
一个常量当作没有引号的键使用时,
PHP
会把常量的值当作索引使用,并产生一个告警,
在未来版本的
PHP
中,这会抛出一个错误
$person =
array
("name" => 'Peter');
print "Hello, {$person[name]}";
//
输出
: Hello, Peter
//
"
起作用
",
但同时发出警告
:
Warning:
Use of undefined ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

解密金融数据

解密金融数据

Justin Pauley
算法技术手册(原书第2 版)

算法技术手册(原书第2 版)

George T.Heineman, Gary Pollice, Stanley Selkow
Java持续交付

Java持续交付

Daniel Bryant, Abraham Marín-Pérez

Publisher Resources

ISBN: 9787121404634