Hướng dẫn dùng php str_pad trong PHP

- Hai dấu dùng để đặt xung quanh chuỗi phải cùng một loại, nếu khác loại sẽ dẫn đến sai cú pháp làm chương trình bị lỗi.

- Nếu chuỗi được đặt bên trong cặp dấu nháy kép thì chuỗi đó không được chứa ký tự là dấu nháy kép [tuy nhiên nó có thể chứa ký tự là dấu nháy đơn]

- Nếu chuỗi được đặt bên trong cặp dấu nháy đơn thì chuỗi đó không được chứa ký tự là dấu nháy đơn [tuy nhiên nó có thể chứa ký tự là dấu nháy kép]

- Để giải quyết trường hợp bạn muốn chuỗi được đặt bên trong cặp dấu nháy kép có thể chứa ký tự là dấu nháy kép thì bạn phải đặt một dấu gạch chéo ngược phía trước ký tự là dấu nháy kép đó [trường hợp dấu nháy đơn cũng tương tự].

3] Điểm khác nhau giữa " " và ' '

- Đối với chuỗi được đặt bên trong cặp dấu nháy kép, nếu chuỗi đó có chứa cụm từ "gọi tên biến" thì khi ta hiển thị chuỗi này lên màn hình, nó sẽ hiển thị luôn cả giá trị của biến.

- Đối với chuỗi được đặt bên trong cặp dấu nháy đơn, nếu chuỗi đó có chứa cụm từ "gọi tên biến" thì khi ta hiển thị chuỗi này lên màn hình, nó sẽ KHÔNG hiển thị giá trị của biến.

4] Cách nối các chuỗi lại với nhau

- Ta có thể nối hai hoặc nhiều chuỗi lại với nhau thành một chuỗi bằng cách đặt dấu chấm ở giữa hai chuỗi cần nối.

Giá trị của biến $text là một chuỗi được nối từ ba chuỗi.

- Ví dụ trên, giá trị của biến $text là một chuỗi được nối từ ba chuỗi Tài liệu học ngôn ngữ lập trình PHP và cả ba chuỗi đều được đặt bên trong cặp dấu nháy kép. Điều đó không có nghĩa là để nối các chuỗi lại với nhau thì các chuỗi phải có cùng một loại dấu bao xung quanh, mà chỉ cần mỗi chuỗi viết đúng theo là được.

Hàm ucfirst[] trả về chuỗi chuyển đổi ký tự đầu tiên thành chữ hoa. Nó không thay đổi trường hợp của các ký tự khác.

Cú pháp

string ucfirst [ string $str ] 

Thí dụ

  

Đầu ra:

My name is David

4] Hàm lcfirst[] trong PHP

Hàm lcfirst[] trả về chuỗi chuyển đổi ký tự đầu tiên thành chữ thường. Nó không thay đổi trường hợp của các ký tự khác.

Cú pháp

string lcfirst [ string $str ]  

Thí dụ

  
0

Đầu ra:

  
1

5] Hàm ucwords[] trong PHP

Hàm ucwords[] trả về chuỗi chuyển đổi ký tự đầu tiên của mỗi từ thành chữ hoa.

Cú pháp

  
2

Thí dụ

  
3

Đầu ra:

  
4

6] Hàm strrev[] trong PHP

Hàm strrev[] trả về chuỗi đảo ngược.

Cú pháp

  
5

Thí dụ

  
6

Đầu ra:

  
7

7] Hàm strlen[] trong PHP

Hàm strlen[] trả về độ dài của chuỗi.

Cú pháp

  
8

Thí dụ

  
9

Đầu ra:

my name is David
0

Cài ứng dụng cafedev để dễ dàng cập nhật tin và học lập trình mọi lúc mọi nơi tại đây.

Tài liệu từ cafedev:

  • Full series tự học PHP từ cơ bản tới nâng cao tại đây nha.
  • Ebook về PHP tại đây.
  • Các nguồn kiến thức MIỄN PHÍ VÔ GIÁ từ cafedev tại đây

Nếu bạn thấy hay và hữu ích, bạn có thể tham gia các kênh sau của cafedev để nhận được nhiều hơn nữa:

Bài viết dưới đây sẽ tổng hợp một số hàm phổ biến có chức năng tương tự nhau giữa hai ngôn ngữ lập trình: PHP và JavaScript [JS].

Việc nắm rõ các hàm built-in của cả hai ngôn ngữ sẽ giúp việc coding trở nên linh hoạt hơn.

Đồng thời, việc phân biệt giữa cách sử dụng những hàm này sẽ tránh những nhầm lẫn không đáng có.

Các hàm thao tác với chuỗi

Tách chuỗi thành một mảng

Nếu như trong PHP, hàm đầu tiên có thể nghĩ tới là

$input = "Alien";
echo str_pad[$input, 10];                      // produces "Alien     "
echo str_pad[$input, 10, "-=", STR_PAD_LEFT];  // produces "-=-=-Alien"
echo str_pad[$input, 10, "_", STR_PAD_BOTH];   // produces "__Alien___"
echo str_pad[$input,  6, "___"];               // produces "Alien_"
echo str_pad[$input,  3, "*"];                 // produces "Alien"
0, thì trong JS, hàm tương ứng sẽ là
$input = "Alien";
echo str_pad[$input, 10];                      // produces "Alien     "
echo str_pad[$input, 10, "-=", STR_PAD_LEFT];  // produces "-=-=-Alien"
echo str_pad[$input, 10, "_", STR_PAD_BOTH];   // produces "__Alien___"
echo str_pad[$input,  6, "___"];               // produces "Alien_"
echo str_pad[$input,  3, "*"];                 // produces "Alien"
1 .

$input = "Alien";
echo str_pad[$input, 10];                      // produces "Alien     "
echo str_pad[$input, 10, "-=", STR_PAD_LEFT];  // produces "-=-=-Alien"
echo str_pad[$input, 10, "_", STR_PAD_BOTH];   // produces "__Alien___"
echo str_pad[$input,  6, "___"];               // produces "Alien_"
echo str_pad[$input,  3, "*"];                 // produces "Alien"
0

$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode[" ", $pizza];
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

$input = "Alien";
echo str_pad[$input, 10];                      // produces "Alien     "
echo str_pad[$input, 10, "-=", STR_PAD_LEFT];  // produces "-=-=-Alien"
echo str_pad[$input, 10, "_", STR_PAD_BOTH];   // produces "__Alien___"
echo str_pad[$input,  6, "___"];               // produces "Alien_"
echo str_pad[$input,  3, "*"];                 // produces "Alien"
1

const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"

Khớp một biểu thức chính quy

$input = "Alien";
echo str_pad[$input, 10];                      // produces "Alien     "
echo str_pad[$input, 10, "-=", STR_PAD_LEFT];  // produces "-=-=-Alien"
echo str_pad[$input, 10, "_", STR_PAD_BOTH];   // produces "__Alien___"
echo str_pad[$input,  6, "___"];               // produces "Alien_"
echo str_pad[$input,  3, "*"];                 // produces "Alien"
4

// The "i" after the pattern delimiter indicates a case-insensitive search
if [preg_match["/php/i", "PHP is the web scripting language of choice."]] {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}

$input = "Alien";
echo str_pad[$input, 10];                      // produces "Alien     "
echo str_pad[$input, 10, "-=", STR_PAD_LEFT];  // produces "-=-=-Alien"
echo str_pad[$input, 10, "_", STR_PAD_BOTH];   // produces "__Alien___"
echo str_pad[$input,  6, "___"];               // produces "Alien_"
echo str_pad[$input,  3, "*"];                 // produces "Alien"
5

const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
const regex = /[A-Z]/g;
const found = paragraph.match[regex];

console.log[found];
// expected output: Array ["T", "I"]

Có thể thấy, dù thực hiện cùng một chức năng, nhưng kết quả trả về giữa hai hàm trên là khác nhau.

$input = "Alien";
echo str_pad[$input, 10];                      // produces "Alien     "
echo str_pad[$input, 10, "-=", STR_PAD_LEFT];  // produces "-=-=-Alien"
echo str_pad[$input, 10, "_", STR_PAD_BOTH];   // produces "__Alien___"
echo str_pad[$input,  6, "___"];               // produces "Alien_"
echo str_pad[$input,  3, "*"];                 // produces "Alien"
4: trả về
$input = "Alien";
echo str_pad[$input, 10];                      // produces "Alien     "
echo str_pad[$input, 10, "-=", STR_PAD_LEFT];  // produces "-=-=-Alien"
echo str_pad[$input, 10, "_", STR_PAD_BOTH];   // produces "__Alien___"
echo str_pad[$input,  6, "___"];               // produces "Alien_"
echo str_pad[$input,  3, "*"];                 // produces "Alien"
7 nếu khớp với pattern,
$input = "Alien";
echo str_pad[$input, 10];                      // produces "Alien     "
echo str_pad[$input, 10, "-=", STR_PAD_LEFT];  // produces "-=-=-Alien"
echo str_pad[$input, 10, "_", STR_PAD_BOTH];   // produces "__Alien___"
echo str_pad[$input,  6, "___"];               // produces "Alien_"
echo str_pad[$input,  3, "*"];                 // produces "Alien"
8 nếu không khớp và
$input = "Alien";
echo str_pad[$input, 10];                      // produces "Alien     "
echo str_pad[$input, 10, "-=", STR_PAD_LEFT];  // produces "-=-=-Alien"
echo str_pad[$input, 10, "_", STR_PAD_BOTH];   // produces "__Alien___"
echo str_pad[$input,  6, "___"];               // produces "Alien_"
echo str_pad[$input,  3, "*"];                 // produces "Alien"
9 nếu có lỗi xảy ra.

$input = "Alien";
echo str_pad[$input, 10];                      // produces "Alien     "
echo str_pad[$input, 10, "-=", STR_PAD_LEFT];  // produces "-=-=-Alien"
echo str_pad[$input, 10, "_", STR_PAD_BOTH];   // produces "__Alien___"
echo str_pad[$input,  6, "___"];               // produces "Alien_"
echo str_pad[$input,  3, "*"];                 // produces "Alien"
5: trả về chuỗi khớp với pattern và
const str1 = '5';

console.log[str1.padStart[2, '0']];
// expected output: "05"

const fullNumber = '2034399002125581';
const last4Digits = fullNumber.slice[-4];
const maskedNumber = last4Digits.padStart[fullNumber.length, '*'];

console.log[maskedNumber];
// expected output: "************5581"

const str1 = 'Breaded Mushrooms';

console.log[str1.padEnd[25, '.']];
// expected output: "Breaded Mushrooms........"

const str2 = '200';

console.log[str2.padEnd[5]];
// expected output: "200  "
1 nếu không có chuỗi thoản mãn.

Tương tự, cũng sẽ có cặp hàm tương ứng

const str1 = '5';

console.log[str1.padStart[2, '0']];
// expected output: "05"

const fullNumber = '2034399002125581';
const last4Digits = fullNumber.slice[-4];
const maskedNumber = last4Digits.padStart[fullNumber.length, '*'];

console.log[maskedNumber];
// expected output: "************5581"

const str1 = 'Breaded Mushrooms';

console.log[str1.padEnd[25, '.']];
// expected output: "Breaded Mushrooms........"

const str2 = '200';

console.log[str2.padEnd[5]];
// expected output: "200  "
2 và
const str1 = '5';

console.log[str1.padStart[2, '0']];
// expected output: "05"

const fullNumber = '2034399002125581';
const last4Digits = fullNumber.slice[-4];
const maskedNumber = last4Digits.padStart[fullNumber.length, '*'];

console.log[maskedNumber];
// expected output: "************5581"

const str1 = 'Breaded Mushrooms';

console.log[str1.padEnd[25, '.']];
// expected output: "Breaded Mushrooms........"

const str2 = '200';

console.log[str2.padEnd[5]];
// expected output: "200  "
3

Lấp đầy chuỗi [pad a string] theo độ dài cho trước

const str1 = '5';

console.log[str1.padStart[2, '0']];
// expected output: "05"

const fullNumber = '2034399002125581';
const last4Digits = fullNumber.slice[-4];
const maskedNumber = last4Digits.padStart[fullNumber.length, '*'];

console.log[maskedNumber];
// expected output: "************5581"

const str1 = 'Breaded Mushrooms';

console.log[str1.padEnd[25, '.']];
// expected output: "Breaded Mushrooms........"

const str2 = '200';

console.log[str2.padEnd[5]];
// expected output: "200  "
4

$input = "Alien";
echo str_pad[$input, 10];                      // produces "Alien     "
echo str_pad[$input, 10, "-=", STR_PAD_LEFT];  // produces "-=-=-Alien"
echo str_pad[$input, 10, "_", STR_PAD_BOTH];   // produces "__Alien___"
echo str_pad[$input,  6, "___"];               // produces "Alien_"
echo str_pad[$input,  3, "*"];                 // produces "Alien"

const str1 = '5';

console.log[str1.padStart[2, '0']];
// expected output: "05"

const fullNumber = '2034399002125581';
const last4Digits = fullNumber.slice[-4];
const maskedNumber = last4Digits.padStart[fullNumber.length, '*'];

console.log[maskedNumber];
// expected output: "************5581"

const str1 = 'Breaded Mushrooms';

console.log[str1.padEnd[25, '.']];
// expected output: "Breaded Mushrooms........"

const str2 = '200';

console.log[str2.padEnd[5]];
// expected output: "200  "
5,
const str1 = '5';

console.log[str1.padStart[2, '0']];
// expected output: "05"

const fullNumber = '2034399002125581';
const last4Digits = fullNumber.slice[-4];
const maskedNumber = last4Digits.padStart[fullNumber.length, '*'];

console.log[maskedNumber];
// expected output: "************5581"

const str1 = 'Breaded Mushrooms';

console.log[str1.padEnd[25, '.']];
// expected output: "Breaded Mushrooms........"

const str2 = '200';

console.log[str2.padEnd[5]];
// expected output: "200  "
6,

const str1 = '5';

console.log[str1.padStart[2, '0']];
// expected output: "05"

const fullNumber = '2034399002125581';
const last4Digits = fullNumber.slice[-4];
const maskedNumber = last4Digits.padStart[fullNumber.length, '*'];

console.log[maskedNumber];
// expected output: "************5581"

const str1 = 'Breaded Mushrooms';

console.log[str1.padEnd[25, '.']];
// expected output: "Breaded Mushrooms........"

const str2 = '200';

console.log[str2.padEnd[5]];
// expected output: "200  "

Thay vì có một đối số

const str1 = '5';

console.log[str1.padStart[2, '0']];
// expected output: "05"

const fullNumber = '2034399002125581';
const last4Digits = fullNumber.slice[-4];
const maskedNumber = last4Digits.padStart[fullNumber.length, '*'];

console.log[maskedNumber];
// expected output: "************5581"

const str1 = 'Breaded Mushrooms';

console.log[str1.padEnd[25, '.']];
// expected output: "Breaded Mushrooms........"

const str2 = '200';

console.log[str2.padEnd[5]];
// expected output: "200  "
7 - quy định pad về bên trái hay bên phải như hàm
const str1 = '5';

console.log[str1.padStart[2, '0']];
// expected output: "05"

const fullNumber = '2034399002125581';
const last4Digits = fullNumber.slice[-4];
const maskedNumber = last4Digits.padStart[fullNumber.length, '*'];

console.log[maskedNumber];
// expected output: "************5581"

const str1 = 'Breaded Mushrooms';

console.log[str1.padEnd[25, '.']];
// expected output: "Breaded Mushrooms........"

const str2 = '200';

console.log[str2.padEnd[5]];
// expected output: "200  "
4 của PHP, sẽ có hai hàm tương ứng với từng kiểu padding trong JS.

Tìm vị trí xuất hiện đầu tiên của chuỗi cần tìm trong chuỗi cho trước

const str1 = '5';

console.log[str1.padStart[2, '0']];
// expected output: "05"

const fullNumber = '2034399002125581';
const last4Digits = fullNumber.slice[-4];
const maskedNumber = last4Digits.padStart[fullNumber.length, '*'];

console.log[maskedNumber];
// expected output: "************5581"

const str1 = 'Breaded Mushrooms';

console.log[str1.padEnd[25, '.']];
// expected output: "Breaded Mushrooms........"

const str2 = '200';

console.log[str2.padEnd[5]];
// expected output: "200  "
9

$mystring = 'abc';
$findme   = 'a';
$pos = strpos[$mystring, $findme];

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th [first] character.
if [$pos === false] {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}

$mystring = 'abc';
$findme   = 'a';
$pos = strpos[$mystring, $findme];

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th [first] character.
if [$pos === false] {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
0

const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf[searchTerm];

console.log['The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst];
// expected output: "The index of the first "dog" from the beginning is 40"

console.log['The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf[searchTerm, [indexOfFirst + 1]]];
// expected output: "The index of the 2nd "dog" is 52"

Một lưu ý tiếp theo về sự khác nhau giữa dữ liệu trả về của hai hàm trên.

Khi không có chuỗi cần tìm:

Hàm

const str1 = '5';

console.log[str1.padStart[2, '0']];
// expected output: "05"

const fullNumber = '2034399002125581';
const last4Digits = fullNumber.slice[-4];
const maskedNumber = last4Digits.padStart[fullNumber.length, '*'];

console.log[maskedNumber];
// expected output: "************5581"

const str1 = 'Breaded Mushrooms';

console.log[str1.padEnd[25, '.']];
// expected output: "Breaded Mushrooms........"

const str2 = '200';

console.log[str2.padEnd[5]];
// expected output: "200  "
9: trả về giá trị
$input = "Alien";
echo str_pad[$input, 10];                      // produces "Alien     "
echo str_pad[$input, 10, "-=", STR_PAD_LEFT];  // produces "-=-=-Alien"
echo str_pad[$input, 10, "_", STR_PAD_BOTH];   // produces "__Alien___"
echo str_pad[$input,  6, "___"];               // produces "Alien_"
echo str_pad[$input,  3, "*"];                 // produces "Alien"
9. Do đó, cần tránh nhầm lẫn với giá trị
$input = "Alien";
echo str_pad[$input, 10];                      // produces "Alien     "
echo str_pad[$input, 10, "-=", STR_PAD_LEFT];  // produces "-=-=-Alien"
echo str_pad[$input, 10, "_", STR_PAD_BOTH];   // produces "__Alien___"
echo str_pad[$input,  6, "___"];               // produces "Alien_"
echo str_pad[$input,  3, "*"];                 // produces "Alien"
8

Hàm

$mystring = 'abc';
$findme   = 'a';
$pos = strpos[$mystring, $findme];

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th [first] character.
if [$pos === false] {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
0: trả về giá trị
$mystring = 'abc';
$findme   = 'a';
$pos = strpos[$mystring, $findme];

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th [first] character.
if [$pos === false] {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
5.

Một số cặp hàm xử lý chuỗi khác

PHPJavaScript
$mystring = 'abc';
$findme   = 'a';
$pos = strpos[$mystring, $findme];

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th [first] character.
if [$pos === false] {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
6
$mystring = 'abc';
$findme   = 'a';
$pos = strpos[$mystring, $findme];

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th [first] character.
if [$pos === false] {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
7
$mystring = 'abc';
$findme   = 'a';
$pos = strpos[$mystring, $findme];

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th [first] character.
if [$pos === false] {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
8
$mystring = 'abc';
$findme   = 'a';
$pos = strpos[$mystring, $findme];

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th [first] character.
if [$pos === false] {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
9
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf[searchTerm];

console.log['The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst];
// expected output: "The index of the first "dog" from the beginning is 40"

console.log['The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf[searchTerm, [indexOfFirst + 1]]];
// expected output: "The index of the 2nd "dog" is 52"
0
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf[searchTerm];

console.log['The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst];
// expected output: "The index of the first "dog" from the beginning is 40"

console.log['The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf[searchTerm, [indexOfFirst + 1]]];
// expected output: "The index of the 2nd "dog" is 52"
1
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf[searchTerm];

console.log['The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst];
// expected output: "The index of the first "dog" from the beginning is 40"

console.log['The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf[searchTerm, [indexOfFirst + 1]]];
// expected output: "The index of the 2nd "dog" is 52"
2
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf[searchTerm];

console.log['The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst];
// expected output: "The index of the first "dog" from the beginning is 40"

console.log['The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf[searchTerm, [indexOfFirst + 1]]];
// expected output: "The index of the 2nd "dog" is 52"
3

Các hàm thao tác với mảng

Đếm số lượng phần tử của mảng

Một trong những bài toàn thường gặp nhất ở bất kỳ ngôn ngữ lập trình nào.

const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf[searchTerm];

console.log['The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst];
// expected output: "The index of the first "dog" from the beginning is 40"

console.log['The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf[searchTerm, [indexOfFirst + 1]]];
// expected output: "The index of the 2nd "dog" is 52"
4

$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
var_dump[count[$a]];  // 3

$b[0]  = 7;
$b[5]  = 9;
var_dump[count[$b]];  // 2

const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf[searchTerm];

console.log['The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst];
// expected output: "The index of the first "dog" from the beginning is 40"

console.log['The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf[searchTerm, [indexOfFirst + 1]]];
// expected output: "The index of the 2nd "dog" is 52"
5

const clothing = ['shoes', 'shirts', 'socks', 'sweaters'];

console.log[clothing.length];
// expected output: 4

Trong trường hợp của JS, có thể đếm số lượng phần tử mảng dựa vào thuộc tính

const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf[searchTerm];

console.log['The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst];
// expected output: "The index of the first "dog" from the beginning is 40"

console.log['The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf[searchTerm, [indexOfFirst + 1]]];
// expected output: "The index of the 2nd "dog" is 52"
5 , thay vì lời gọi hàm.

Nối các phần tử của mảng thành một chuỗi

const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf[searchTerm];

console.log['The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst];
// expected output: "The index of the first "dog" from the beginning is 40"

console.log['The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf[searchTerm, [indexOfFirst + 1]]];
// expected output: "The index of the 2nd "dog" is 52"
7

const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
0

const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf[searchTerm];

console.log['The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst];
// expected output: "The index of the first "dog" from the beginning is 40"

console.log['The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf[searchTerm, [indexOfFirst + 1]]];
// expected output: "The index of the 2nd "dog" is 52"
8

const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
1

Hàm

const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf[searchTerm];

console.log['The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst];
// expected output: "The index of the first "dog" from the beginning is 40"

console.log['The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf[searchTerm, [indexOfFirst + 1]]];
// expected output: "The index of the 2nd "dog" is 52"
7 có một alias
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf[searchTerm];

console.log['The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst];
// expected output: "The index of the first "dog" from the beginning is 40"

console.log['The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf[searchTerm, [indexOfFirst + 1]]];
// expected output: "The index of the 2nd "dog" is 52"
8, có tên giống với hàm
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf[searchTerm];

console.log['The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst];
// expected output: "The index of the first "dog" from the beginning is 40"

console.log['The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf[searchTerm, [indexOfFirst + 1]]];
// expected output: "The index of the 2nd "dog" is 52"
8 trong JS.

Gộp hai mảng vào nhau

$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
var_dump[count[$a]];  // 3

$b[0]  = 7;
$b[5]  = 9;
var_dump[count[$b]];  // 2
2

const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
2

$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
var_dump[count[$a]];  // 3

$b[0]  = 7;
$b[5]  = 9;
var_dump[count[$b]];  // 2
3

const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
3

Hàm

$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
var_dump[count[$a]];  // 3

$b[0]  = 7;
$b[5]  = 9;
var_dump[count[$b]];  // 2
2 sẽ có kết quả tương đương với hàm
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
var_dump[count[$a]];  // 3

$b[0]  = 7;
$b[5]  = 9;
var_dump[count[$b]];  // 2
3 trong trường hợp mảng đầu vào chỉ có key dạng số.

Đối với mảng có key dạng chuỗi, phần tử của mảng bị trùng key sẽ bị ghi đè.

Xác định giá trị cho trước có tồn tại trong mảng

$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
var_dump[count[$a]];  // 3

$b[0]  = 7;
$b[5]  = 9;
var_dump[count[$b]];  // 2
6

const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
4

$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
var_dump[count[$a]];  // 3

$b[0]  = 7;
$b[5]  = 9;
var_dump[count[$b]];  // 2
7

const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
5

Mặc định cả hai hàm trên đều phân biệt hoa thường khi tìm kiếm.

Tìm vị trí xuất hiện đầu tiên của phần tử cần tìm trong chuỗi cho trước

$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
var_dump[count[$a]];  // 3

$b[0]  = 7;
$b[5]  = 9;
var_dump[count[$b]];  // 2
8

const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
6

$mystring = 'abc';
$findme   = 'a';
$pos = strpos[$mystring, $findme];

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th [first] character.
if [$pos === false] {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
0

const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
7

Chú ý:

Hàm

$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
var_dump[count[$a]];  // 3

$b[0]  = 7;
$b[5]  = 9;
var_dump[count[$b]];  // 2
8: trả về giá trị
$input = "Alien";
echo str_pad[$input, 10];                      // produces "Alien     "
echo str_pad[$input, 10, "-=", STR_PAD_LEFT];  // produces "-=-=-Alien"
echo str_pad[$input, 10, "_", STR_PAD_BOTH];   // produces "__Alien___"
echo str_pad[$input,  6, "___"];               // produces "Alien_"
echo str_pad[$input,  3, "*"];                 // produces "Alien"
9 khi không tìm thấy phần tử tương ứng. Do đó, cũng cần lưu ý để tránh nhầm với trường hợp giá trị trả về là
$input = "Alien";
echo str_pad[$input, 10];                      // produces "Alien     "
echo str_pad[$input, 10, "-=", STR_PAD_LEFT];  // produces "-=-=-Alien"
echo str_pad[$input, 10, "_", STR_PAD_BOTH];   // produces "__Alien___"
echo str_pad[$input,  6, "___"];               // produces "Alien_"
echo str_pad[$input,  3, "*"];                 // produces "Alien"
8 [phần tử đầu tiên trong mảng].

Hàm

$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
var_dump[count[$a]];  // 3

$b[0]  = 7;
$b[5]  = 9;
var_dump[count[$b]];  // 2
8 mặc định so sánh sử dụng loose comparison
const clothing = ['shoes', 'shirts', 'socks', 'sweaters'];

console.log[clothing.length];
// expected output: 4
4.

Hàm

$mystring = 'abc';
$findme   = 'a';
$pos = strpos[$mystring, $findme];

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th [first] character.
if [$pos === false] {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
0: trả về giá trị
$mystring = 'abc';
$findme   = 'a';
$pos = strpos[$mystring, $findme];

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th [first] character.
if [$pos === false] {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
5 khi không tìm thấy phần tử trong mảng. Hàm này chỉ sử dụng strict comparison
const clothing = ['shoes', 'shirts', 'socks', 'sweaters'];

console.log[clothing.length];
// expected output: 4
7.

Cả hai hàm trên đều phân biệt hoa thường.

Sắp xếp mảng theo hàm tự định nghĩa

const clothing = ['shoes', 'shirts', 'socks', 'sweaters'];

console.log[clothing.length];
// expected output: 4
8

const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
8

const clothing = ['shoes', 'shirts', 'socks', 'sweaters'];

console.log[clothing.length];
// expected output: 4
9

const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
9

Một số cặp hàm xử lý mảng khác

PHPJavaScript
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
00
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
01
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
02
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
03
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
04
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
05
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
06
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
07
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
08
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
09
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
10
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
11
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
12
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
13
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
14
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
15
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
16
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
17
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
18
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
19

Tổng hợp chung các hàm đã điểm qua

PHPJavaScriptString
$input = "Alien";
echo str_pad[$input, 10];                      // produces "Alien     "
echo str_pad[$input, 10, "-=", STR_PAD_LEFT];  // produces "-=-=-Alien"
echo str_pad[$input, 10, "_", STR_PAD_BOTH];   // produces "__Alien___"
echo str_pad[$input,  6, "___"];               // produces "Alien_"
echo str_pad[$input,  3, "*"];                 // produces "Alien"
0
$input = "Alien";
echo str_pad[$input, 10];                      // produces "Alien     "
echo str_pad[$input, 10, "-=", STR_PAD_LEFT];  // produces "-=-=-Alien"
echo str_pad[$input, 10, "_", STR_PAD_BOTH];   // produces "__Alien___"
echo str_pad[$input,  6, "___"];               // produces "Alien_"
echo str_pad[$input,  3, "*"];                 // produces "Alien"
1
$input = "Alien";
echo str_pad[$input, 10];                      // produces "Alien     "
echo str_pad[$input, 10, "-=", STR_PAD_LEFT];  // produces "-=-=-Alien"
echo str_pad[$input, 10, "_", STR_PAD_BOTH];   // produces "__Alien___"
echo str_pad[$input,  6, "___"];               // produces "Alien_"
echo str_pad[$input,  3, "*"];                 // produces "Alien"
4
$input = "Alien";
echo str_pad[$input, 10];                      // produces "Alien     "
echo str_pad[$input, 10, "-=", STR_PAD_LEFT];  // produces "-=-=-Alien"
echo str_pad[$input, 10, "_", STR_PAD_BOTH];   // produces "__Alien___"
echo str_pad[$input,  6, "___"];               // produces "Alien_"
echo str_pad[$input,  3, "*"];                 // produces "Alien"
5
const str1 = '5';

console.log[str1.padStart[2, '0']];
// expected output: "05"

const fullNumber = '2034399002125581';
const last4Digits = fullNumber.slice[-4];
const maskedNumber = last4Digits.padStart[fullNumber.length, '*'];

console.log[maskedNumber];
// expected output: "************5581"

const str1 = 'Breaded Mushrooms';

console.log[str1.padEnd[25, '.']];
// expected output: "Breaded Mushrooms........"

const str2 = '200';

console.log[str2.padEnd[5]];
// expected output: "200  "
4
const str1 = '5';

console.log[str1.padStart[2, '0']];
// expected output: "05"

const fullNumber = '2034399002125581';
const last4Digits = fullNumber.slice[-4];
const maskedNumber = last4Digits.padStart[fullNumber.length, '*'];

console.log[maskedNumber];
// expected output: "************5581"

const str1 = 'Breaded Mushrooms';

console.log[str1.padEnd[25, '.']];
// expected output: "Breaded Mushrooms........"

const str2 = '200';

console.log[str2.padEnd[5]];
// expected output: "200  "
5,
const str1 = '5';

console.log[str1.padStart[2, '0']];
// expected output: "05"

const fullNumber = '2034399002125581';
const last4Digits = fullNumber.slice[-4];
const maskedNumber = last4Digits.padStart[fullNumber.length, '*'];

console.log[maskedNumber];
// expected output: "************5581"

const str1 = 'Breaded Mushrooms';

console.log[str1.padEnd[25, '.']];
// expected output: "Breaded Mushrooms........"

const str2 = '200';

console.log[str2.padEnd[5]];
// expected output: "200  "
6
const str1 = '5';

console.log[str1.padStart[2, '0']];
// expected output: "05"

const fullNumber = '2034399002125581';
const last4Digits = fullNumber.slice[-4];
const maskedNumber = last4Digits.padStart[fullNumber.length, '*'];

console.log[maskedNumber];
// expected output: "************5581"

const str1 = 'Breaded Mushrooms';

console.log[str1.padEnd[25, '.']];
// expected output: "Breaded Mushrooms........"

const str2 = '200';

console.log[str2.padEnd[5]];
// expected output: "200  "
9
$mystring = 'abc';
$findme   = 'a';
$pos = strpos[$mystring, $findme];

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th [first] character.
if [$pos === false] {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
0
$mystring = 'abc';
$findme   = 'a';
$pos = strpos[$mystring, $findme];

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th [first] character.
if [$pos === false] {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
6
$mystring = 'abc';
$findme   = 'a';
$pos = strpos[$mystring, $findme];

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th [first] character.
if [$pos === false] {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
7
$mystring = 'abc';
$findme   = 'a';
$pos = strpos[$mystring, $findme];

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th [first] character.
if [$pos === false] {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
8
$mystring = 'abc';
$findme   = 'a';
$pos = strpos[$mystring, $findme];

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th [first] character.
if [$pos === false] {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
9
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf[searchTerm];

console.log['The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst];
// expected output: "The index of the first "dog" from the beginning is 40"

console.log['The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf[searchTerm, [indexOfFirst + 1]]];
// expected output: "The index of the 2nd "dog" is 52"
0
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf[searchTerm];

console.log['The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst];
// expected output: "The index of the first "dog" from the beginning is 40"

console.log['The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf[searchTerm, [indexOfFirst + 1]]];
// expected output: "The index of the 2nd "dog" is 52"
1
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf[searchTerm];

console.log['The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst];
// expected output: "The index of the first "dog" from the beginning is 40"

console.log['The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf[searchTerm, [indexOfFirst + 1]]];
// expected output: "The index of the 2nd "dog" is 52"
2
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf[searchTerm];

console.log['The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst];
// expected output: "The index of the first "dog" from the beginning is 40"

console.log['The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf[searchTerm, [indexOfFirst + 1]]];
// expected output: "The index of the 2nd "dog" is 52"
3Array
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf[searchTerm];

console.log['The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst];
// expected output: "The index of the first "dog" from the beginning is 40"

console.log['The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf[searchTerm, [indexOfFirst + 1]]];
// expected output: "The index of the 2nd "dog" is 52"
4
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf[searchTerm];

console.log['The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst];
// expected output: "The index of the first "dog" from the beginning is 40"

console.log['The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf[searchTerm, [indexOfFirst + 1]]];
// expected output: "The index of the 2nd "dog" is 52"
5
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf[searchTerm];

console.log['The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst];
// expected output: "The index of the first "dog" from the beginning is 40"

console.log['The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf[searchTerm, [indexOfFirst + 1]]];
// expected output: "The index of the 2nd "dog" is 52"
7
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
40
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
var_dump[count[$a]];  // 3

$b[0]  = 7;
$b[5]  = 9;
var_dump[count[$b]];  // 2
2
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
var_dump[count[$a]];  // 3

$b[0]  = 7;
$b[5]  = 9;
var_dump[count[$b]];  // 2
3
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
var_dump[count[$a]];  // 3

$b[0]  = 7;
$b[5]  = 9;
var_dump[count[$b]];  // 2
6
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
var_dump[count[$a]];  // 3

$b[0]  = 7;
$b[5]  = 9;
var_dump[count[$b]];  // 2
7
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
var_dump[count[$a]];  // 3

$b[0]  = 7;
$b[5]  = 9;
var_dump[count[$b]];  // 2
8
$mystring = 'abc';
$findme   = 'a';
$pos = strpos[$mystring, $findme];

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th [first] character.
if [$pos === false] {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
0
const clothing = ['shoes', 'shirts', 'socks', 'sweaters'];

console.log[clothing.length];
// expected output: 4
8
const clothing = ['shoes', 'shirts', 'socks', 'sweaters'];

console.log[clothing.length];
// expected output: 4
9
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
00
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
01
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
02
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
03
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
04
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
05
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
06
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
07
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
08
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
09
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
10
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
11
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
12
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
13
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
14
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
15
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
16
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
17
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
18
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
19

Như một lẽ dĩ nhiên, không phải lúc nào cũng có cặp hàm tương ứng trong hai ngôn ngữ lập trình. Một số hàm có tồn tại ở PHP, nhưng lại không có ở JS và ngược lại. Chính vì lẽ đó, một vài thư viện ra đời để góp phần giải quyết vấn đề này.

Thư viện JS có thể kể đến trong việc hỗ trợ tích hợp các hàm của PHP:

const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
69 [//locutus.io/]

Ở chiều ngược lại, gần như các hàm của JS đều có thể được hỗ trợ bởi PHP. Với framework phổ biến nhất hiện nay là Laravel, có thể dễ dàng tìm thấy các hàm mở rộng thông qua hai class

const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
70 và
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split[' '];
console.log[words[3]];
// expected output: "fox"
71

Chủ Đề