Tạo chuỗi trong php

PHP cho phép bạn làm việc với chuỗi theo nhiều cách khác nhau. Ví dụ: bạn có thể tìm kiếm một chuỗi cho một đoạn văn bản;

Trong hướng dẫn này, bạn tìm hiểu cách tạo chuỗi trong tập lệnh PHP của mình. Bạn nhìn vào

  • Tạo chuỗi có dấu nháy đơn và nháy kép
  • Tránh nhầm lẫn bằng cách sử dụng cú pháp dấu ngoặc nhọn
  • Trình tự thoát
  • Cách tạo chuỗi nhiều dòng
  • Sử dụng dấu phân cách của riêng bạn để tạo chuỗi

Tạo chuỗi sử dụng dấu nháy đơn và kép

Để bao gồm một chuỗi ký tự trong tập lệnh PHP của bạn, chỉ cần gửi chuỗi đó vào

  • Dấu nháy đơn — e. g.
    
    $favouriteBird = 'owl'; 
    echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
    
    4, hoặc
  • Dấu ngoặc kép — e. g.
    
    $favouriteBird = 'owl'; 
    echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
    
    5

Nếu bạn sử dụng dấu ngoặc đơn thì PHP sẽ đọc chuỗi chính xác như bạn đã nhập. Dấu ngoặc kép hoạt động theo cách tương tự, nhưng một số tính năng bổ sung cũng phát huy tác dụng

  • phân tích cú pháp biến. Tên biến trong chuỗi được thay thế bằng giá trị biến tương ứng
  • Nhân vật thoát. Bạn có thể sử dụng các chuỗi thoát để bao gồm các ký tự đặc biệt trong chuỗi

Phân tích cú pháp biến trong chuỗi trích dẫn kép

Đây là một ví dụ cho thấy hoạt động phân tích cú pháp biến


$myString = 'there'; 
echo 'Hello, $myString!
'; // Displays "Hello $myString!" echo "Hello, $myString!
"; // Displays "Hello there!"

Lưu ý cách, với dấu ngoặc đơn, văn bản


$favouriteBird = 'owl'; 
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
6 được diễn giải nguyên trạng, trong khi với dấu ngoặc kép,

$favouriteBird = 'owl'; 
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
6 được thay thế bằng nội dung của biến

$favouriteBird = 'owl'; 
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
8 [

$favouriteBird = 'owl'; 
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
9]

Đôi khi bạn gặp vấn đề với phân tích cú pháp biến - ví dụ


$favouriteBird = 'owl'; 
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 

Ở đây bạn muốn chèn giá trị của biến


$favouriteBird = 'owl';
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
echo "My favourite birds are ${favouriteBird}s"; // Displays "My favourite birds are owls" 
echo "My favourite birds are {$favouriteBird}s"; // Displays "My favourite birds are owls" 
0, nhưng PHP cho rằng bạn muốn nói đến biến

$favouriteBird = 'owl';
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
echo "My favourite birds are ${favouriteBird}s"; // Displays "My favourite birds are owls" 
echo "My favourite birds are {$favouriteBird}s"; // Displays "My favourite birds are owls" 
1 [không tồn tại]. Giải pháp là bọc tên biến [và, tùy chọn, ký hiệu đô la] trong dấu ngoặc nhọn

________số 8

Dấu ngoặc nhọn cũng cho phép bạn bao gồm các biểu thức phức tạp hơn trong chuỗi, chẳng hạn như phần tử mảng và thuộc tính đối tượng


$favouriteBird = 'owl'; 
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
1

Thoát ký tự trong chuỗi trích dẫn kép

Đây là một ví dụ thể hiện sự thoát khỏi ký tự


$favouriteBird = 'owl'; 
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
2

Với dấu ngoặc đơn, văn bản


$favouriteBird = 'owl';
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
echo "My favourite birds are ${favouriteBird}s"; // Displays "My favourite birds are owls" 
echo "My favourite birds are {$favouriteBird}s"; // Displays "My favourite birds are owls" 
2 được sử dụng nguyên trạng. Tuy nhiên, với dấu ngoặc kép,

$favouriteBird = 'owl';
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
echo "My favourite birds are ${favouriteBird}s"; // Displays "My favourite birds are owls" 
echo "My favourite birds are {$favouriteBird}s"; // Displays "My favourite birds are owls" 
2 được nhận dạng là chuỗi thoát cho ký tự tab, vì vậy tab được chèn vào chuỗi

Ngoài trình tự thoát “tab” [


$favouriteBird = 'owl';
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
echo "My favourite birds are ${favouriteBird}s"; // Displays "My favourite birds are owls" 
echo "My favourite birds are {$favouriteBird}s"; // Displays "My favourite birds are owls" 
2] vừa được hiển thị, còn có nhiều trình tự thoát khác mà bạn có thể sử dụng trong chuỗi trích dẫn kép. Dưới đây là một vài cái phổ biến


$favouriteBird = 'owl';
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
echo "My favourite birds are ${favouriteBird}s"; // Displays "My favourite birds are owls" 
echo "My favourite birds are {$favouriteBird}s"; // Displays "My favourite birds are owls" 
5Nạp dòng [ASCII 10]

$favouriteBird = 'owl';
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
echo "My favourite birds are ${favouriteBird}s"; // Displays "My favourite birds are owls" 
echo "My favourite birds are {$favouriteBird}s"; // Displays "My favourite birds are owls" 
6Lùi vận chuyển [ASCII 13]

$favouriteBird = 'owl';
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
echo "My favourite birds are ${favouriteBird}s"; // Displays "My favourite birds are owls" 
echo "My favourite birds are {$favouriteBird}s"; // Displays "My favourite birds are owls" 
2Tab ngang [ASCII 9]

$favouriteBird = 'owl';
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
echo "My favourite birds are ${favouriteBird}s"; // Displays "My favourite birds are owls" 
echo "My favourite birds are {$favouriteBird}s"; // Displays "My favourite birds are owls" 
8Tab dọc [ASCII 11]

$favouriteBird = 'owl';
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
echo "My favourite birds are ${favouriteBird}s"; // Displays "My favourite birds are owls" 
echo "My favourite birds are {$favouriteBird}s"; // Displays "My favourite birds are owls" 
9Nạp biểu mẫu [ASCII 12]

$favouriteBird = 'owl'; 
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
10Dấu gạch chéo ngược bằng chữ

$favouriteBird = 'owl'; 
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
11Ký hiệu chữ $ [đô la]

$favouriteBird = 'owl'; 
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
12Dấu ngoặc kép theo nghĩa đen

Ví dụ


$myString = 'there'; 
echo 'Hello, $myString!
'; // Displays "Hello $myString!" echo "Hello, $myString!
"; // Displays "Hello there!"
4

Kết quả


$myString = 'there'; 
echo 'Hello, $myString!
'; // Displays "Hello $myString!" echo "Hello, $myString!
"; // Displays "Hello there!"
5Sử dụng

$favouriteBird = 'owl'; 
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
13 để bao gồm một trích dẫn đơn theo nghĩa đen bên trong một chuỗi trích dẫn đơn. Để bao gồm chuỗi 2 ký tự theo nghĩa đen

$favouriteBird = 'owl'; 
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
13, hãy sử dụng

$favouriteBird = 'owl'; 
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
15

Tạo chuỗi nhiều dòng

Nếu bạn muốn chèn một chuỗi nhiều dòng vào mã PHP của mình, chỉ cần sử dụng các dòng mới để kết thúc mỗi dòng


$myString = 'there'; 
echo 'Hello, $myString!
'; // Displays "Hello $myString!" echo "Hello, $myString!
"; // Displays "Hello there!"
9

Sử dụng dấu phân cách của riêng bạn. heredoc và nowdoc

Bạn cũng có thể bao quanh một chuỗi với các dấu phân cách của riêng mình bằng cách sử dụng cú pháp heredoc của PHP


$favouriteBird = 'owl'; 
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
0

Cú pháp Heredoc rất hữu ích nếu chuỗi của bạn chứa hỗn hợp dấu nháy đơn và dấu nháy kép, vì nó giúp bạn không phải thoát khỏi chúng


$favouriteBird = 'owl'; 
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
1

Cú pháp Heredoc hoạt động giống như trích dẫn kép — các biến trong chuỗi được phân tích cú pháp và các chuỗi thoát được xử lý. Nếu bạn muốn tránh điều này, hãy sử dụng cú pháp nowdoc, tương đương với việc sử dụng dấu nháy đơn. Cú pháp Nowdoc giống như cú pháp heredoc, ngoại trừ việc bạn đặt dấu phân cách đầu tiên trong dấu nháy đơn


$favouriteBird = 'owl'; 
echo "My favourite birds are $favouriteBirds"; // Displays "My favourite birds are " 
0

Bây giờ bạn đã biết cách tạo chuỗi PHP. Bạn đã biết cách sử dụng dấu ngoặc đơn và dấu ngoặc kép để chèn ký tự chuỗi vào tập lệnh PHP của mình; . Mã hóa vui vẻ

Chủ Đề