Literal string adalah notasi untuk mewakili nilai string dalam teks program komputer. Di PHP, string dapat dibuat dengan tanda kutip tunggal, tanda kutip ganda atau menggunakan sintaks heredoc atau nowdoc
In this code example, we create two strings and assign them to $a
and $b
variables. We print them with the echo
keyword.
The first string is created with the double quote delimiters, the second one
with single quotes.
PHP string heredoc
The heredoc preserves the line breaks and other whitespace (including
indentation) in the text. The heredoc is created with <<<
followed by a delimiting identifier, followed, starting on the next line, by the
text to be quoted, and then closed by the same identifier on its own line.
The closing identifier must not be indented. It can only contain alphanumeric
characters and underscores, and must start with a non-digit character or
underscore.
The example prints an example of a direct speech.
$ php heredoc.php
"That is just as I intended." Vautrin said. "You know quite well what
you are about. Good, my little eaglet! You are born to command, you
are strong, you stand firm on your feet, you are game! I respect you."
Nowdoc ditentukan mirip dengan heredoc, tetapi tidak ada penguraian yang dilakukan di dalam nowdoc. Nowdoc diidentifikasi dengan urutan
The $quantity
variable is replaced with its value in the string
output.
$ php interpolation.php
There are 5 roses in the vase
_0 yang sama yang digunakan untuk heredocs, tetapi pengidentifikasi yang mengikuti dilampirkan
. g. <<<'TEKS'The example prints three sentences using the nowdoc syntax.
$ php nowdoc.php
Fear is among the greatest obstacles which prevent us from enjoying life
to its fullest extent. Since of the most commonly held fears among
people are the fear of heights and the fear of falling from heights.
Rock climbing is a fantastic way to conquer these fears.
Interpolasi string PHP
Variabel diinterpolasi dalam string yang diapit oleh tanda kutip ganda
The $quantity
variable is replaced with its value in the string
output.
$ php interpolation.php
There are 5 roses in the vase
_Kurung kurawal dapat digunakan ketika nama variabel berada di sebelah karakter lain
Without the curly braces, the PHP interpreter would look for the $item_names
variable, which does not exist.
Advertisements$ php curly_braces.php
There are 5 roses in the vase
_PHP menggunakan operator titik
The $quantity
variable is replaced with its value in the string
output.
$ php interpolation.php
There are 5 roses in the vase
1 untuk menggabungkan stringphp > echo "PHP " . "language\n";
PHP language
_Contoh menggabungkan dua string
php > $a = "Java ";
php > $a .= "language\n";
php > echo $a;
Java language
PHP juga mendukung operator gabungan
The $quantity
variable is replaced with its value in the string
output.
$ php interpolation.php
There are 5 roses in the vase
_2Karakter pelarian PHP
Karakter pelarian adalah karakter tunggal yang ditunjuk untuk meminta interpretasi alternatif pada karakter berikutnya dalam karakter
php> echo " bbb\raaa";
aaabbb
_The carriage return
The $quantity
variable is replaced with its value in the string
output.
$ php interpolation.php
There are 5 roses in the vase
3 adalah karakter kontrol untuk akhir baris kembali ke awal barisThe new line is a control characters which begins a new line of text.
$ php strophe.php
Incompatible, it don't matter though
'cos someone's bound to hear my cry
Speak out if you do
You're not easy to find
php> echo "Towering\tinferno\n";
Towering inferno
Tab horizontal menempatkan spasi di antara teks
In this code example, we create two strings and assign them to $a
and $b
variables. We print them with the echo
keyword.
The first string is created with the double quote delimiters, the second one
with single quotes.
PHP string heredoc
The heredoc preserves the line breaks and other whitespace (including
indentation) in the text. The heredoc is created with <<<
followed by a delimiting identifier, followed, starting on the next line, by the
text to be quoted, and then closed by the same identifier on its own line.
The closing identifier must not be indented. It can only contain alphanumeric
characters and underscores, and must start with a non-digit character or
underscore.
The example prints an example of a direct speech.
$ php heredoc.php
"That is just as I intended." Vautrin said. "You know quite well what
you are about. Good, my little eaglet! You are born to command, you
are strong, you stand firm on your feet, you are game! I respect you."
0Tanda kutip tunggal dan ganda dapat disarangkan. Atau jika kita hanya menggunakan tanda kutip tunggal, kita dapat menggunakan garis miring terbalik untuk menghindari arti standar dari tanda kutip tunggal
IklanIn this code example, we create two strings and assign them to $a
and $b
variables. We print them with the echo
keyword.
The first string is created with the double quote delimiters, the second one
with single quotes.
PHP string heredoc
The heredoc preserves the line breaks and other whitespace (including
indentation) in the text. The heredoc is created with <<<
followed by a delimiting identifier, followed, starting on the next line, by the
text to be quoted, and then closed by the same identifier on its own line.
The closing identifier must not be indented. It can only contain alphanumeric
characters and underscores, and must start with a non-digit character or
underscore.
The example prints an example of a direct speech.
$ php heredoc.php
"That is just as I intended." Vautrin said. "You know quite well what
you are about. Good, my little eaglet! You are born to command, you
are strong, you stand firm on your feet, you are game! I respect you."
_1Tanda dolar
The $quantity
variable is replaced with its value in the string
output.
$ php interpolation.php
There are 5 roses in the vase
_4 juga memiliki arti khusus dalam PHP; . Jika sebuah variabel digunakan di dalam string, itu diinterpolasi, i. e. nilai variabel yang digunakan. Untuk menggemakan nama variabel, kita lolos dari The $quantity
variable is replaced with its value in the string
output.
$ php interpolation.php
There are 5 roses in the vase
_4 karakter The $quantity
variable is replaced with its value in the string
output.
$ php interpolation.php
There are 5 roses in the vase
6PHP memiliki sejumlah besar fungsi bawaan yang berguna dan berguna yang dapat digunakan untuk bekerja dengan string
In this code example, we create two strings and assign them to $a
and $b
variables. We print them with the echo
keyword.
The first string is created with the double quote delimiters, the second one
with single quotes.
PHP string heredoc
The heredoc preserves the line breaks and other whitespace (including
indentation) in the text. The heredoc is created with <<<
followed by a delimiting identifier, followed, starting on the next line, by the
text to be quoted, and then closed by the same identifier on its own line.
The closing identifier must not be indented. It can only contain alphanumeric
characters and underscores, and must start with a non-digit character or
underscore.
The example prints an example of a direct speech.
$ php heredoc.php
"That is just as I intended." Vautrin said. "You know quite well what
you are about. Good, my little eaglet! You are born to command, you
are strong, you stand firm on your feet, you are game! I respect you."
_2Di sini kita menggunakan tiga fungsi. Fungsi
The $quantity
variable is replaced with its value in the string
output.
$ php interpolation.php
There are 5 roses in the vase
7 mengembalikan sejumlah karakter dalam string. The $quantity
variable is replaced with its value in the string
output.
$ php interpolation.php
There are 5 roses in the vase
8 mengubah karakter menjadi huruf besar, dan The $quantity
variable is replaced with its value in the string
output.
$ php interpolation.php
There are 5 roses in the vase
9 mengubah karakter menjadi
In this code example, we create two strings and assign them to $a
and $b
variables. We print them with the echo
keyword.
The first string is created with the double quote delimiters, the second one
with single quotes.
PHP string heredoc
The heredoc preserves the line breaks and other whitespace (including
indentation) in the text. The heredoc is created with <<<
followed by a delimiting identifier, followed, starting on the next line, by the
text to be quoted, and then closed by the same identifier on its own line.
The closing identifier must not be indented. It can only contain alphanumeric
characters and underscores, and must start with a non-digit character or
underscore.
The example prints an example of a direct speech.
$ php heredoc.php
"That is just as I intended." Vautrin said. "You know quite well what
you are about. Good, my little eaglet! You are born to command, you
are strong, you stand firm on your feet, you are game! I respect you."
_3Selanjutnya, kita membahas fungsi
Without the curly braces, the PHP interpreter would look for the $item_names
variable, which does not exist.
Advertisements$ php curly_braces.php
There are 5 roses in the vase
_0In this code example, we create two strings and assign them to $a
and $b
variables. We print them with the echo
keyword.
The first string is created with the double quote delimiters, the second one
with single quotes.
PHP string heredoc
The heredoc preserves the line breaks and other whitespace (including
indentation) in the text. The heredoc is created with <<<
followed by a delimiting identifier, followed, starting on the next line, by the
text to be quoted, and then closed by the same identifier on its own line.
The closing identifier must not be indented. It can only contain alphanumeric
characters and underscores, and must start with a non-digit character or
underscore.
The example prints an example of a direct speech.
$ php heredoc.php
"That is just as I intended." Vautrin said. "You know quite well what
you are about. Good, my little eaglet! You are born to command, you
are strong, you stand firm on your feet, you are game! I respect you."
_4Fungsi mengembalikan bagian dari string. Parameter pertama adalah string yang ditentukan. Parameter kedua adalah awal dari substring. Parameter ketiga adalah opsional. Ini adalah panjang dari substring yang dikembalikan. Standarnya adalah mengembalikan hingga akhir string
Fungsi
Without the curly braces, the PHP interpreter would look for the $item_names
variable, which does not exist.
Advertisements$ php curly_braces.php
There are 5 roses in the vase
1 mengulangi string beberapa kaliIn this code example, we create two strings and assign them to $a
and $b
variables. We print them with the echo
keyword.
The first string is created with the double quote delimiters, the second one
with single quotes.
PHP string heredoc
The heredoc preserves the line breaks and other whitespace (including
indentation) in the text. The heredoc is created with <<<
followed by a delimiting identifier, followed, starting on the next line, by the
text to be quoted, and then closed by the same identifier on its own line.
The closing identifier must not be indented. It can only contain alphanumeric
characters and underscores, and must start with a non-digit character or
underscore.
The example prints an example of a direct speech.
$ php heredoc.php
"That is just as I intended." Vautrin said. "You know quite well what
you are about. Good, my little eaglet! You are born to command, you
are strong, you stand firm on your feet, you are game! I respect you."
5Pada contoh berikutnya, kami memodifikasi string secara acak
In this code example, we create two strings and assign them to $a
and $b
variables. We print them with the echo
keyword.
The first string is created with the double quote delimiters, the second one
with single quotes.
PHP string heredoc
The heredoc preserves the line breaks and other whitespace (including
indentation) in the text. The heredoc is created with <<<
followed by a delimiting identifier, followed, starting on the next line, by the
text to be quoted, and then closed by the same identifier on its own line.
The closing identifier must not be indented. It can only contain alphanumeric
characters and underscores, and must start with a non-digit character or
underscore.
The example prints an example of a direct speech.
$ php heredoc.php
"That is just as I intended." Vautrin said. "You know quite well what
you are about. Good, my little eaglet! You are born to command, you
are strong, you stand firm on your feet, you are game! I respect you."
_6Ini adalah contoh keluaran dari skrip
Without the curly braces, the PHP interpreter would look for the $item_names
variable, which does not exist.
Advertisements$ php curly_braces.php
There are 5 roses in the vase
2IklanFungsi
Without the curly braces, the PHP interpreter would look for the $item_names
variable, which does not exist.
Advertisements$ php curly_braces.php
There are 5 roses in the vase
3 digunakan untuk membagi string menjadi beberapa bagian. Ini mengembalikan array bagian string terpisah. Fungsi Without the curly braces, the PHP interpreter would look for the $item_names
variable, which does not exist.
Advertisements$ php curly_braces.php
There are 5 roses in the vase
4 menggabungkan elemen array dengan stringIn this code example, we create two strings and assign them to $a
and $b
variables. We print them with the echo
keyword.
The first string is created with the double quote delimiters, the second one
with single quotes.
PHP string heredoc
The heredoc preserves the line breaks and other whitespace (including
indentation) in the text. The heredoc is created with <<<
followed by a delimiting identifier, followed, starting on the next line, by the
text to be quoted, and then closed by the same identifier on its own line.
The closing identifier must not be indented. It can only contain alphanumeric
characters and underscores, and must start with a non-digit character or
underscore.
The example prints an example of a direct speech.
$ php heredoc.php
"That is just as I intended." Vautrin said. "You know quite well what
you are about. Good, my little eaglet! You are born to command, you
are strong, you stand firm on your feet, you are game! I respect you."
_7Di sini kita membagi teks dengan fungsi
Without the curly braces, the PHP interpreter would look for the $item_names
variable, which does not exist.
Advertisements$ php curly_braces.php
There are 5 roses in the vase
_3. Fungsi ini akan memotong string menjadi potongan-potongan setiap kali menemukan titik Without the curly braces, the PHP interpreter would look for the $item_names
variable, which does not exist.
Advertisements$ php curly_braces.php
There are 5 roses in the vase
6 karakterIn this code example, we create two strings and assign them to $a
and $b
variables. We print them with the echo
keyword.
The first string is created with the double quote delimiters, the second one
with single quotes.
PHP string heredoc
The heredoc preserves the line breaks and other whitespace (including
indentation) in the text. The heredoc is created with <<<
followed by a delimiting identifier, followed, starting on the next line, by the
text to be quoted, and then closed by the same identifier on its own line.
The closing identifier must not be indented. It can only contain alphanumeric
characters and underscores, and must start with a non-digit character or
underscore.
The example prints an example of a direct speech.
$ php heredoc.php
"That is just as I intended." Vautrin said. "You know quite well what
you are about. Good, my little eaglet! You are born to command, you
are strong, you stand firm on your feet, you are game! I respect you."
_8
In this code example, we create two strings and assign them to $a
and $b
variables. We print them with the echo
keyword.
The first string is created with the double quote delimiters, the second one
with single quotes.
PHP string heredoc
The heredoc preserves the line breaks and other whitespace (including
indentation) in the text. The heredoc is created with <<<
followed by a delimiting identifier, followed, starting on the next line, by the
text to be quoted, and then closed by the same identifier on its own line.
The closing identifier must not be indented. It can only contain alphanumeric
characters and underscores, and must start with a non-digit character or
underscore.
The example prints an example of a direct speech.
$ php heredoc.php
"That is just as I intended." Vautrin said. "You know quite well what
you are about. Good, my little eaglet! You are born to command, you
are strong, you stand firm on your feet, you are game! I respect you."
_9Outputnya tidak maksimal. Kami akan mengubahnya agar terlihat lebih rapi
IklanThe example prints three sentences using the nowdoc syntax.
$ php nowdoc.php
Fear is among the greatest obstacles which prevent us from enjoying life
to its fullest extent. Since of the most commonly held fears among
people are the fear of heights and the fear of falling from heights.
Rock climbing is a fantastic way to conquer these fears.
_0Kami berhasil memberikan keluaran yang diformat lebih bagus
Array karakter PHP
Sebuah string dalam PHP adalah sebuah array dari karakter
The example prints three sentences using the nowdoc syntax.
$ php nowdoc.php
Fear is among the greatest obstacles which prevent us from enjoying life
to its fullest extent. Since of the most commonly held fears among
people are the fear of heights and the fear of falling from heights.
Rock climbing is a fantastic way to conquer these fears.
1Sebuah string didefinisikan. Ini berisi sebelas karakter
The example prints three sentences using the nowdoc syntax.
$ php nowdoc.php
Fear is among the greatest obstacles which prevent us from enjoying life
to its fullest extent. Since of the most commonly held fears among
people are the fear of heights and the fear of falling from heights.
Rock climbing is a fantastic way to conquer these fears.
2Kami mengulangi string dengan for loop. Ukuran string ditentukan dengan fungsi
The $quantity
variable is replaced with its value in the string
output.
$ php interpolation.php
There are 5 roses in the vase
7. Fungsi Without the curly braces, the PHP interpreter would look for the $item_names
variable, which does not exist.
Advertisements$ php curly_braces.php
There are 5 roses in the vase
8 mengembalikan nilai ASCII dari sebuah karakter. Kami menggunakan notasi indeks array untuk mendapatkan karakterThe example prints three sentences using the nowdoc syntax.
$ php nowdoc.php
Fear is among the greatest obstacles which prevent us from enjoying life
to its fullest extent. Since of the most commonly held fears among
people are the fear of heights and the fear of falling from heights.
Rock climbing is a fantastic way to conquer these fears.
_3Pemformatan string atau interpolasi string adalah penempatan dinamis berbagai nilai ke dalam string
IklanThe example prints three sentences using the nowdoc syntax.
$ php nowdoc.php
Fear is among the greatest obstacles which prevent us from enjoying life
to its fullest extent. Since of the most commonly held fears among
people are the fear of heights and the fear of falling from heights.
Rock climbing is a fantastic way to conquer these fears.
_4Pada contoh berikutnya, kita melewatkan float dan nilai string
The example prints three sentences using the nowdoc syntax.
$ php nowdoc.php
Fear is among the greatest obstacles which prevent us from enjoying life
to its fullest extent. Since of the most commonly held fears among
people are the fear of heights and the fear of falling from heights.
Rock climbing is a fantastic way to conquer these fears.
5Kami mungkin tidak menyukai fakta bahwa angka pada contoh sebelumnya memiliki 6 tempat desimal secara default. Kita dapat mengontrol jumlah tempat desimal dalam penentu pemformatan
The example prints three sentences using the nowdoc syntax.
$ php nowdoc.php
Fear is among the greatest obstacles which prevent us from enjoying life
to its fullest extent. Since of the most commonly held fears among
people are the fear of heights and the fear of falling from heights.
Rock climbing is a fantastic way to conquer these fears.
6Contoh berikut menunjukkan opsi pemformatan lainnya
IklanThe example prints three sentences using the nowdoc syntax.
$ php nowdoc.php
Fear is among the greatest obstacles which prevent us from enjoying life
to its fullest extent. Since of the most commonly held fears among
people are the fear of heights and the fear of falling from heights.
Rock climbing is a fantastic way to conquer these fears.
_7Contoh berikutnya mencetak tiga kolom angka
IklanThe example prints three sentences using the nowdoc syntax.
$ php nowdoc.php
Fear is among the greatest obstacles which prevent us from enjoying life
to its fullest extent. Since of the most commonly held fears among
people are the fear of heights and the fear of falling from heights.
Rock climbing is a fantastic way to conquer these fears.
_8Untuk memperbaikinya, kami menggunakan penentu lebar. Penspesifikasi lebar menentukan lebar minimal objek. Jika objek lebih kecil dari lebarnya, maka diisi dengan spasi
Bagaimana string digunakan dalam PHP?
Menurut fungsi string dalam php, dalam bahasa pemrograman, fungsi string digunakan untuk memodifikasi string atau menanyakan pengetahuan tentang string (beberapa . Fungsi panjang (string) adalah contoh paling dasar dari fungsi string. Panjang literal string dikembalikan oleh fungsi ini.
Bagaimana cara kerja fungsi string?
Dengan fungsi string, Anda dapat membuat ekspresi di Access yang memanipulasi teks dalam berbagai cara . Misalnya, Anda mungkin ingin menampilkan hanya sebagian dari nomor seri pada formulir. Atau, Anda mungkin perlu menggabungkan (menggabungkan) beberapa string, seperti nama belakang dan nama depan.
Bagaimana cara membaca string di PHP?
Pendekatan 2. Menggunakan metode strlen() – Metode strlen() digunakan untuk menghitung panjang string yang ditentukan dalam PHP. Iterasi loop for diterapkan menggunakan panjang string, dan karakter indeks ke-i dicetak setiap waktu. Kompleksitas waktu sama dengan metode sebelumnya.
Bagaimana cara mendeklarasikan variabel string di PHP?
Variabel dimulai dengan tanda $, diikuti dengan nama variabel . Nama variabel harus dimulai dengan huruf atau karakter garis bawah. Nama variabel tidak boleh dimulai dengan angka. Nama variabel hanya boleh berisi karakter alfanumerik dan garis bawah (A-z, 0-9, dan _ )