PHP basic study notes (4), PHP basic study notes_PHP tutorial
php basic study notes (4), php basic study notes
Introduction to arrays
Concept: It is a collection of several data put together in a certain order, which is called an "array" as a whole. An array is an ordered collection of data.
Definition form:
<span>var</span> arr1 = <span>new</span> Array(<span>1</span>, <span>5</span>, <span>8</span>, <span>7</span>, <span>2</span>, <span>10</span>); <span>//</span><span>定义了一个数组,其中具有6个数据</span> <span>var</span> arr2 = <span>new</span> Array(); <span>//</span><span>只是单纯地定义了一个数组(名),但没有给值(数据),即现在是空的</span> <span>var</span> arr3 = [<span>1</span>, <span>5</span>, <span>8</span>, <span>7</span>, <span>2</span>, <span>10</span>]; <span>//</span><span>同arr1,只是一种简写的定义法。</span> <span>var</span> arr4 = [ ]; <span>//</span><span>同arr2,也是一个空数组。</span>
Usage of arrays: The so-called use actually refers to the use of each item of the array.
Value:
<span>var</span> v1 = arr1[<span>0</span>]; <span>//</span><span>取得数组arr1中的第一项,0叫做下标</span> <span>var</span> v2 = arr3[<span>3</span>] + <span>10</span>; <span>//</span><span>取得数组arr3中的第4项,4叫做下标</span> ——所谓下标,其实就是数组的每一个数据的“顺序号”——从0开始编号,是连续的整数。
Assignment:
arr1[0] = 10;
arr2[<span>0</span>] = <span>22</span><span>; arr2[</span><span>1</span>] = <span>33.3</span><span>; arr2[</span><span>2</span>] = “<span>444</span><span>”; arr2[</span><span>3</span>] =<span> “abc”; arr2[</span><span>4</span>] = <span>true</span>;
//At this time, the array arr2 is equivalent to this: [ 22, 33.3, “444”, “abc”, true ]
The "visual image" of the array (taking arr3 as an example):
下标值: <span>0</span> <span>1</span> <span>2</span> <span>3</span> <span>4</span> <span>5</span><span> 数据值: </span><span>1</span> <span>5</span> <span>8</span> <span>7</span> <span>2</span> <span>10</span>
The syntax to get the length of an array - that is, the number of data in it is:
var v1 = array name.length;
Special note: The maximum subscript of an array is the length of the array minus 1.
Usual pattern for array traversal:
var len = array name.length;
for(var i = 0; i < len; i++)
{
//Here is the processing of each item of the array. The writing method of each item is: Array name[i]
}
Another form of array traversal - for in loop statement.
for( var v1 in array name arr1 )
{
//Here is the loop body, which is a traversal loop specifically for array arr1, where the value of v1 is the subscript value representing each item of the array.
//v1 is just a "temporary variable", representing each subscript, which will change from 0 to the maximum subscript of the array.
}
"two-dimensional" array:
<span>var</span> v1 = [<span>2</span>, <span>5</span>, <span>1</span>, <span>5</span><span>]; </span><span>var</span> v2 = [<span>5</span>, <span>1</span>, <span>6</span>, <span>8</span><span>]; </span><span>var</span> v3 = [<span>8</span>, <span>0</span>, <span>9</span>, <span>7</span><span>]; </span><span>var</span> v4 =<span> [v1, v2, v3]; </span><span>var</span> v5 =<span> [ [</span><span>2</span>, <span>5</span>, <span>1</span>, <span>5</span><span>], [</span><span>5</span>, <span>1</span>, <span>6</span>, <span>8</span><span>], [</span><span>8</span>, <span>0</span>, <span>9</span>, <span>7</span><span>] ]; </span>
——There is actually no difference between v4 and v5. Both of them can be called "two-dimensional arrays".
Operations on "two-dimensional" array elements:
Value:
var s1 = v5[0][1]; //5 //Equivalent to getting the second item of the first item in the array v5 (this is still an array).
var s2 = v5[2][3] + 100; //107
Assignment:
v5[0][1] = 200;
v5[2][3] = 300;
Common methods of array objects:
What is a method: A method is actually a function! ——Just if a function "belongs to" an "object", then the function is called a method of the object.
<span>function maibao(){ document.write(“啦啦啦,我是卖报的小行家,卖报啦卖报啦。”); } </span><span>var</span> myDreamGirl =<span> { name: “小花”, age:</span><span>18</span><span>, edu:”大学”, sex:”女”, nengli1: function (){ document.write(“洗衣!”); } , nengli2: function (){ document.write(“做饭!”); } , nengli3: maibao }; </span><span>var</span> v1 = [<span>2</span>, <span>5</span>, <span>1</span>, <span>5</span><span>]; </span><span>var</span> v2 = [<span>5</span>, <span>1</span>, <span>6</span>, <span>8</span>];
Strictly speaking, an array is also an object - even strings are objects.
As an object, v1 has properties and methods:
Attributes:
An array.length: indicates the length of the array object
Method:
An array.concat (other array): Connect two arrays to form a new "longer" array.
var s1 = v1.concat( v2
); //At this time, s1 is an array like this: [2, 5, 1, 5, 5, 1, 6, 8];
An array.join("string"): "Concatenate" all the items in the array with the specified characters into a "long" string.
var s2 = v1.join(“//”); //The result s2 is the string “2//5//1//5”
某数组.pop(); //将该数组的最后一项“移除”(删除),并返回该项数据,即该数组少了一项
var s3 = v1.pop(); //结果v1只剩这个:[2,5,1]; s3的值是5
某数组.push(新数据项d1); //将新的数据d1添加到该数组的最后位置,即数组多了一项。
var s4 = v1.push( 55 ); //v1此时为:[2,5,1, 55], s4的值为新数组的长度,即4
某数组.shift(); //将该数组的第一项“移除”(删除),并返回该项数据,即该数组少了一项
var s5 = v1.shift(); //结果v1只剩这个:[5, 1,55]; s5的值是2
某数组.unshift(新数据项d1); //将新的数据d1添加到该数组的最前位置,即数组多了一项。
var v6 = v1.unshift( 66 ); //v1此时为:[66, 5, 1, 55], s6的值为新数组的长度,即4
javascript语言是一门基于对象的语言。
字符串对象:
<span>var</span> str1 = <span>new</span> String(“abcdefgabc”); <span>//</span><span>这是一个“字符串对象”</span> <span>var</span> str2 = “abcdefgabc”; <span>//</span><span>这个字符串跟前面str1几乎没有区别</span> <span>字符串对象的属性: .length——获得一个字符串的长度(也就是字符个数) 字符串对象的方法: </span><span>1</span>. str1.charAt( n ); ——获得字符串str1中位置为n的那个字符(字符的位置也是从0开始算起)<span>var</span> s1 = str1.charAt( <span>3</span> ); <span>//</span><span>s1的结果是:”d”</span> <span>2</span><span>. str1.toUpperCase(); ——获取str1全部转换为大写的结果 </span><span>var</span> s2 = str1.toUpperCase(); <span>//</span><span>s2的结果是:”ABCDEFGABC”</span> <span>3</span><span>. str1.toLowerCase(); ——获取str1全部转换为小写的结果 </span><span>var</span> s3 = str1.toLowerCase(); <span>//</span><span>s3的结果是:”abcdefgabc”</span> <span>4</span><span>. str1.replace(“字符1”, “字符2”); ——将str1中的“字符1”替换为“字符2” </span><span>var</span> s4 = str1.replace(“cd”, “<span>999</span>”); <span>//</span><span>s4的结果是:”ab999efgabc”</span> <span>5</span>. str1.indexOf(“字符1”); ——获得“字符1”在str1中第一次出现的位置,如果没有出现,结果是-<span>1</span> <span>var</span> s5 = str1.indexOf(“ab”); <span>//</span><span>s5的结果是0</span> <span>6</span>. str1.lastIndexOf(““字符1”); ——获得“字符1”在str1中最后一次出现的位置,如果没有出现,结果是-<span>1</span> <span>var</span> s6 = str1.lastIndexOf(“ab”); <span>//</span><span>s6的结果是7</span> <span>7</span><span>. str1.substr(n, m ) ——取得str1中从位置n开始的m个字符,m可以省略,则表示从位置n一直取到字符串的最后——注意,这种“取”并不影响str1这个原始字符 </span><span>var</span> s7 = str1.substr(<span>2</span>, <span>4</span>); <span>//</span><span>s7为:”cdef”</span> <span>8</span><span>. str1.substring( n, m )——取得str1中从位置n到位置m的前一个字符。 </span><span>var</span> s8 = str1.substring(<span>2</span>, <span>4</span>); <span>//</span><span>s8为:”cd”</span> <span>9</span><span>. str1.split(“字符1”) ——将str1以指定的“字符1”为分界,分割成一个数组,结果是一个数组 </span><span>var</span> s9 = str1.split(“b”); <span>//</span><span>s9的结果是一个数组:[“a”, “cdefga”, “c”]</span>
Math对象
Math对象是一个系统内部定义的对象,我们无需去“新建一个Math对象”——跟string对象和array对象不同。即Math对象是直接使用的。
我们学习Math对象,无非是学习一些常见的数学处理函数——这里当就叫做方法了:
属性:
Math.PI——代表圆周率这个“常数”
方法:
<span>1</span><span>. Math.max(数值1,数值2,…..) ——求得若干个数值中的最大值。 </span><span>2</span><span>. Math.min(数值1,数值2,…..) ——求得若干个数值中的最小值。 </span><span>3</span><span>. Math.abs( 数值1) ——求得数值1的绝对值 </span><span>4</span><span>. Math.pow( x,y) ——求得数值x的y次方,也就是“幂运算” </span><span>5</span><span>. Math.sqrt( x ) ——求得x的开方 </span><span>6</span><span>. Math.round( x ) ——求得x的四舍五入的结果值; </span><span>7</span><span>. Math.floor( x ) ——求得x的向下取整的结果,即找到不大于x的一个最大的整数。 a) Math.floor( </span><span>3.1</span> ) <span>3</span><span> b) Math.floor( </span><span>3.8</span> ) <span>3</span><span> c) Math.floor( </span><span>3</span> ) <span>3</span><span> d) Math.floor( </span>-<span>3.1</span> ) -<span>4</span><span> e) Math.floor( </span>-<span>3.8</span> ) -<span>4</span> <span>8</span><span>. Math.ceil( x ) ——求得x的向上取整的结果,即找到不小于x的一个最小的整数 a) Math.floor( </span><span>3.1</span> ) <span>4</span><span> b) Math.floor( </span><span>3.8</span> ) <span>4</span><span> c) Math.floor( </span><span>3</span> ) <span>3</span><span> d) Math.floor( </span>-<span>3.1</span> ) -<span>3</span><span> e) Math.floor( </span>-<span>3.8</span> ) -<span>3</span> <span>9</span>. Math.random() ——获得一个0~<span>1范围中的随机“小数”,含0,但不含1 </span><span>//</span><span>获得两个整数(n1,n2)之间的随机整数的通用做法:</span> <span>var</span> v1 =<span> Math.random() v1 </span>= v1 * (n2-n1+<span>1</span><span>); v1 </span>= Math.floor( v1 ) + n1; <span>//</span><span>此时v1就是结果 </span><span>//</span><span>将上述3步写出一步就是(通用公式):</span> <span>var</span> v1 = Math.floor( Math.random() * (n2-n1+<span>1</span>) ) + n1

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
