PHP Strings का Introduction
In this page:
$single = 'text, $variable is not replaced';
$double = "text, $variable is replaced";
$joined = $first . " " . $second;
$joined .= " more text";
Single Quoted Strings
Single-quoted strings सबसे कम काम करती हैं: वे $variable references evaluate नहीं करतीं या ज़्यादातर escape sequences process नहीं करतीं, लगभग हर चीज़ exactly वैसे ही print करते हुए जैसे type की गई।
यह simplicity उन्हें मामूली रूप से तेज़ और plain, static text के लिए सही choice बनाती है।
उदाहरण: Single Quoted Strings
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
// Print 'Hello, $name' to the output
echo 'Hello, $name';
?>
Login to try C/C++/Java/PHP code in the editor
Double Quoted Strings
Double-quoted strings अपने contents को actively process करती हैं: अंदर का कोई भी $variable उसकी current value से replace हो जाता है (interpolation), और \n और \t जैसे escape sequences literal backslash-n की तरह print होने के बजाय एक real newline या tab में convert हो जाते हैं।
उदाहरण: Double Quoted Strings
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
// Print "Hello, $name\n" to the output
echo "Hello, $name\n";
?>
Login to try C/C++/Java/PHP code in the editor
String Concatenation
. operator दो strings को end-to-end जोड़कर एक बना देता है — $greeting = 'Hello, ' . $name; — और .= वही काम करते हुए साथ ही result को वापस left-hand variable में reassign भी कर देता है, जो पहले से इसमें था उसके अंत में जोड़ते हुए।
उदाहरण: String Concatenation
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
// Declare `$greeting`, set to `'Hello, ' . $name`
$greeting = 'Hello, ' . $name;
// Print `$greeting` to the output
echo $greeting;
?>
Login to try C/C++/Java/PHP code in the editor
String Length Check करना
strlen() किसी string में bytes की total संख्या return करता है, हर character गिनते हुए spaces और punctuation सहित।
multi-byte characters (accented letters, emoji) वाली strings के लिए, strlen() visible characters के बजाय bytes count करता है, जो display-length logic के लिए इस पर भरोसा करने से पहले जानने लायक है।
उदाहरण: Checking String Length
<?php
// Declare `$text`, set to "Hello!"
$text = "Hello!";
// Print `strlen($text)` to the output
echo strlen($text);
?>
Login to try C/C++/Java/PHP code in the editor
Single Characters Access करना
आप square-bracket indexing इस्तेमाल करके किसी string में एक single character पढ़ (या overwrite कर) सकते हैं, exactly किसी array की तरह — $word[0] पहला character लेता है। PHP strings zero-indexed हैं, इसलिए पहला character position 0 पर बैठता है, 1 पर नहीं।
उदाहरण: Accessing Single Characters
<?php
// Declare `$word`, set to "Hello"
$word = "Hello";
// Print `$word[0]` to the output
echo $word[0];
?>
Login to try C/C++/Java/PHP code in the editor
- Single quotes इस्तेमाल करना और variables के replace होने की उम्मीद करना, ताकि
'Hello $name'$nameको literally print करे। - strings जोड़ने के लिए
+इस्तेमाल करना, जो PHP में arithmetic करता है; इसके बजाय.इस्तेमाल करें। "é"जैसे multi-byte text परstrlen()इस्तेमाल करना और characters की उम्मीद करना, जबकि यह bytes count करता है;mb_strlen()इस्तेमाल करें।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: