PHP String Operations
In this page:
$result = $first . $second; // concatenation
$string .= "more text"; // append
strlen($string);
ucfirst($string);
Strings Compare करना
PHP strings concatenation के लिए . (dot) operator support करती हैं, आपको छोटे टुकड़ों से एक लंबी string बनाने देते हुए, और right side पर इसका नाम दोबारा type किए बिना किसी मौजूदा variable में in place append करने के लिए .=।
उदाहरण: Comparing Strings
<?php
// Declare `$greeting`, set to 'Hello'
$greeting = 'Hello';
$greeting .= ', World!';
// Print `$greeting` to the output
echo $greeting;
?>
Login to try C/C++/Java/PHP code in the editor
Substrings Extract करना
strlen() bytes में किसी string की length return करता है, जो UTF-8 जैसे multi-byte encodings के लिए मायने रखता है जहाँ एक single visible character (मान लीजिए, एक accented letter या emoji) एक से ज़्यादा byte occupy कर सकता है, इसलिए actual characters count करने के लिए mb_strlen() safer choice है।
उदाहरण: Extracting Substrings
<?php
// Declare `$text`, set to "café"
$text = "café";
// Print `strlen($text) . "\n"` to the output
echo strlen($text) . "\n";
// Print `mb_strlen($text)` to the output
echo mb_strlen($text);
?>
Login to try C/C++/Java/PHP code in the editor
Word Count Helper
strtoupper(), strtolower(), और ucfirst() जैसे case-conversion functions comparisons या display के लिए text normalize करते हैं, जैसे किसी name field को save करने से पहले उसका पहला letter capitalize करना या case-insensitive lookups के लिए एक search query को lowercase force करना।
उदाहरण: Word Count Helper
<?php
// Declare `$name`, set to "alice"
$name = "alice";
// Print `ucfirst($name) . "\n"` to the output
echo ucfirst($name) . "\n";
// Print `strtoupper($name)` to the output
echo strtoupper($name);
?>
Login to try C/C++/Java/PHP code in the editor
Prefix और Suffix Check करना
trim(), ltrim(), और rtrim() किसी string के सिरों से whitespace (या दूसरे specified characters) हटाते हैं, जो user input validate या store करने से पहले ज़रूरी है, क्योंकि एक stray leading space चुपचाप एक equality check या database lookup तोड़ सकता है।
उदाहरण: Checking Prefix and Suffix
<?php
// Declare `$input`, set to " [email protected] "
$input = " [email protected] ";
// Print "[" . trim($input) . "]" to the output
echo "[" . trim($input) . "]";
?>
Login to try C/C++/Java/PHP code in the editor
Shuffling और Hashing
str_pad() किसी string में characters तब तक add करता है जब तक यह एक target length तक न पहुँच जाए, किसी report में numbers right-align करने या 7 को 007 में बदलने जैसे किसी ID को zero-pad करने जैसे output format करने के लिए उपयोगी।
उदाहरण: Shuffling and Hashing
<?php
// Print `str_pad("7", 3, "0", STR_PAD_LEFT)` to the output
echo str_pad("7", 3, "0", STR_PAD_LEFT);
?>
Login to try C/C++/Java/PHP code in the editor
- strings को
==से compare करना जब strict type behavior मायने रखता हो, क्योंकि"0" == falseऔर दूसरे loose comparisons surprising results दे सकते हैं;===इस्तेमाल करें। substr($s, 5, 3)इस्तेमाल करना और यह भूल जाना कि दूसरा argument start index है और तीसरा length, end position नहीं।- यह भूल जाना कि
strlenbytes count करता है, इसलिए multibyte characters कोmb_strlenचाहिए।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: