← Back to PHP Course | Chapter 6: Strings | Lesson 7 of 8

PHP String Operations

String operations words को साथ glue करने और उन्हें बदलने के तरीके हैं। एक dot से join करना दो train cars को click करके एक लंबी train बनाने जैसा है।
Syntax
php
$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
<?php
// Declare `$greeting`, set to 'Hello'
$greeting = 'Hello';
$greeting .= ', World!';
// Print `$greeting` to the output
echo $greeting;
?>

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
<?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);
?>

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
<?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);
?>

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
<?php
// Declare `$input`, set to "  [email protected]  "
$input = "  [email protected]  ";
// Print "[" . trim($input) . "]" to the output
echo "[" . trim($input) . "]";
?>

Shuffling और Hashing

str_pad() किसी string में characters तब तक add करता है जब तक यह एक target length तक न पहुँच जाए, किसी report में numbers right-align करने या 7 को 007 में बदलने जैसे किसी ID को zero-pad करने जैसे output format करने के लिए उपयोगी।

उदाहरण: Shuffling and Hashing

php
<?php
// Print `str_pad("7", 3, "0", STR_PAD_LEFT)` to the output
echo str_pad("7", 3, "0", STR_PAD_LEFT);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. strings को == से compare करना जब strict type behavior मायने रखता हो, क्योंकि "0" == false और दूसरे loose comparisons surprising results दे सकते हैं; === इस्तेमाल करें।
  2. substr($s, 5, 3) इस्तेमाल करना और यह भूल जाना कि दूसरा argument start index है और तीसरा length, end position नहीं।
  3. यह भूल जाना कि strlen bytes count करता है, इसलिए multibyte characters को mb_strlen चाहिए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.