PHP Basics · Beginner Level · Chapter 7

PHP Strings क्या है?
Complete Guide in Hindi

PHP Strings की पूरी जानकारी हिंदी में — String declare करना, Quotes, Escape sequences, 20+ String Functions, Heredoc, Nowdoc। Real examples के साथ।

📝 String Basics 🔧 20+ Functions ✂️ substr/strpos 🔄 explode/implode ⏱️ 10 min read
100+PHP String Functions
strlenString की length निकालो
0String index शुरू होता है
" "Double quotes — variable expand

📋 इस Article में क्या-क्या है (Table of Contents)

  1. String क्या है?
  2. Single vs Double Quotes
  3. Escape Sequences
  4. Heredoc & Nowdoc
  5. String Length & Case Functions
  6. String Search Functions
  7. String Replace Functions
  8. String Cut Functions
  9. String Split & Join
  10. Quick Reference + FAQ
1
String क्या है?

String characters का एक sequence है — letters, numbers, spaces, symbols कुछ भी। PHP में string को quotes में लिखते हैं। String PHP का सबसे ज़्यादा use होने वाला data type है।

Websites में जो भी text दिखता है — user का नाम, email, product description, error messages — सब strings हैं। PHP में 100 से ज़्यादा built-in string functions हैं जो text को manipulate करने में help करती हैं।
String = Text data। "Rahul", "hello@email.com", "Delhi 110001" — सब strings हैं। PHP में strings बहुत powerful हैं।
STRING DECLARE करना
<?php
$naam = "Rahul Kumar"; // double quotes
$city = 'Delhi'; // single quotes
$email = "rahul@email.com";
$number = "9876543210"; // number भी string हो सकता है
$empty = ""; // empty string

echo $naam; // Rahul Kumar
echo strlen($naam); // 11 (characters count)
?>

2
Single vs Double Quotes — बड़ा फर्क है!
PHP में strings दो तरह से लिख सकते हैं — single quotes (' ') और double quotes (" ")। दोनों में एक important फर्क है जो हर PHP developer को पता होना चाहिए।

✅ Double Quotes " " — Variables expand होते हैं

$naam = "Rahul";

// Variable directly expand होता है
echo "Hello $naam!";
// Output: Hello Rahul!

// Complex — curly braces use करो
echo "Name: {$naam}s";
// Output: Name: Rahuls

⚠️ Single Quotes ' ' — Variables expand नहीं

$naam = "Rahul";

// Variable literally print होता है
echo 'Hello $naam!';
// Output: Hello $naam!

// Escape sequences भी नहीं
echo '\n';
// Output: \n (literally)
QUOTES — Performance और Use Cases
<?php
$product = "Laptop";
$price = 45000;

// Double quotes — variable interpolation
echo "Product: $product, Price: ₹$price";
// Product: Laptop, Price: ₹45000

// Single quotes — faster (no variable parsing)
echo 'यह static text है, variables नहीं बदलते';

// Quote के अंदर quote — दूसरी type use करो
echo "He said 'Hello'"; // double में single
echo 'She said "Hi"'; // single में double
?>
💡 Performance Tip: Single quotes थोड़े faster होते हैं क्योंकि PHP को variable parsing नहीं करनी पड़ती। Static text के लिए single quotes prefer करो। Variables हों तो double quotes या concatenation दोनों work करते हैं।

3
Escape Sequences — Special Characters
कुछ characters को string में directly नहीं लिख सकते — जैसे newline, tab, या quote। इनके लिए escape sequences use होते हैं — backslash (\) से शुरू होते हैं। Double quotes में ही काम करते हैं।
SequenceमतलबExample
\nNewline (नई लाइन)"Line1\nLine2"
\tTab (खाली जगह)"Name:\tRahul"
\\Backslash (\)"C:\\Users\\Rahul"
\"Double quote"He said \"Hi\""
\'Single quote'It\'s a book'
\$Dollar sign"Price: \$500"
\rCarriage returnWindows line ending
\0Null characterBinary data में
ESCAPE SEQUENCES — Examples
<?php
// \n — newline (HTML में br tag ज़रूरी होगा)
echo "पहली लाइन\nदूसरी लाइन\nतीसरी लाइन";

// \t — tab spacing
echo "नाम:\tRahul\nशहर:\tDelhi";

// \" — double quote inside string
echo "उसने कहा \"नमस्ते!\"";
// Output: उसने कहा "नमस्ते!"

// \$ — literal dollar sign
echo "Price: \$500";
// Output: Price: $500

// \\ — backslash in Windows paths
echo "Path: C:\\Users\\Rahul\\Documents";
?>

4
Heredoc & Nowdoc — Multi-line Strings
जब बहुत लंबी multi-line string लिखनी हो — जैसे HTML template, email body — तो Heredoc और Nowdoc use होते हैं। Quotes की झंझट नहीं।
<<<EOT
Heredoc

Double quotes जैसा — variables expand होते हैं। Long HTML templates के लिए best।

<<<'EOT'
Nowdoc

Single quotes जैसा — variables expand नहीं होते। Static templates के लिए।

HEREDOC — Multi-line with Variables
<?php
$naam = "Rahul";
$city = "Delhi";
$score = 95;

// Heredoc — variables expand होते हैं
$html = <<<EOT
<div class="card">
  <h2>$naam</h2>
  <p>शहर: $city</p>
  <p>Score: $score%</p>
</div>
EOT;

echo $html;

// Nowdoc — variables expand नहीं होते
$template = <<<'EOT'
Hello $naam, यह literally print होगा।
EOT;
echo $template; // Hello $naam, यह literally print होगा।
?>
Heredoc Rule: Closing identifier (EOT) हमेशा line के एकदम शुरू में लिखो — कोई space या tab नहीं। EOT के बाद semicolon लगाओ। EOT की जगह कोई भी word use कर सकते हो — HTML, SQL, TEXT, END।

5
String Length & Case Functions
यह functions string की length check करने और uppercase/lowercase convert करने के लिए use होते हैं — form validation और display formatting में बहुत common।
strlen()
String Length

String में characters की count देता है।

strlen("Hello") → 5
strtoupper()
Uppercase बनाना

पूरी string को CAPITAL letters में।

strtoupper("hello") → HELLO
strtolower()
Lowercase बनाना

पूरी string को small letters में।

strtolower("HELLO") → hello
ucfirst()
First Letter Capital

पहले character को uppercase।

ucfirst("rahul") → Rahul
ucwords()
Every Word Capital

हर word का पहला letter uppercase।

ucwords("rahul kumar") → Rahul Kumar
trim()
Whitespace हटाना

शुरू और अंत से spaces हटाता है।

trim(" hello ") → "hello"
LENGTH & CASE — Real World Examples
<?php
$naam = " rahul kumar "; // spaces के साथ

// Form input clean करना
$clean = trim($naam); // "rahul kumar"
$proper = ucwords($clean); // "Rahul Kumar"
echo $proper; // Rahul Kumar

// Password length check
$password = "mypass123";
if (strlen($password) < 8) {
  echo "Password कम से कम 8 characters का होना चाहिए!";
}

// Email को lowercase बनाना
$email = "Rahul@Gmail.COM";
$email = strtolower(trim($email));
echo $email; // rahul@gmail.com

// ltrim और rtrim — सिर्फ एक side से
echo ltrim(" hello "); // "hello " (left trim)
echo rtrim(" hello "); // " hello" (right trim)
?>

6
String Search Functions — ढूँढना
Search functions string में कोई word या character ढूँढने के लिए use होते हैं — email validation, keyword search, URL checking में बहुत useful।
strpos()
Position ढूँढना

String में substring की पहली position। नहीं मिले तो false।

strpos("Hello World", "World") → 6
strrpos()
Last Position

Substring की last (आखिरी) position।

strrpos("abcabc", "c") → 5
str_contains()
Contains Check (PHP 8)

String में substring है या नहीं — true/false।

str_contains("Hello", "ell") → true
str_starts_with()
Starts With (PHP 8)

String किसी से शुरू होती है?

str_starts_with("Hello", "He") → true
str_ends_with()
Ends With (PHP 8)

String किसी से खत्म होती है?

str_ends_with("hello.php", ".php") → true
substr_count()
Count Occurrences

String में substring कितनी बार आई।

substr_count("hello", "l") → 2
SEARCH FUNCTIONS — Examples
<?php
$email = "rahul@gmail.com";
$url = "https://hindinotespoint.com";

// strpos — position ढूँढना
$pos = strpos($email, "@");
echo $pos; // 5

// strpos — false check (=== use करो!)
if (strpos($email, "@") !== false) {
  echo "Valid email format";
}

// PHP 8 — str_contains (much cleaner!)
if (str_contains($email, "@")) {
  echo "@ है email में";
}

// str_starts_with — URL check
if (str_starts_with($url, "https")) {
  echo "Secure URL ✓";
}

// str_ends_with — file extension check
$file = "photo.jpg";
if (str_ends_with($file, ".jpg") || str_ends_with($file, ".png")) {
  echo "Valid image file";
}
?>
⚠️ strpos का trap: strpos() false return करता है जब नहीं मिले, लेकिन 0 return करता है जब पहली position पर मिले। if (strpos($s, "x")) position 0 को भी false मान लेगा! हमेशा !== false use करो।

7
String Replace Functions — बदलना
Replace functions string में कोई word या pattern ढूँढकर उसे replace करते हैं — content filtering, URL slug बनाना, templates fill करना।
str_replace() — सबसे ज़्यादा use होने वाला
<?php
// Basic replace
$text = "Hello World";
echo str_replace("World", "PHP", $text);
// Hello PHP

// Case-insensitive replace
$msg = "I love JAVA and java is great";
echo str_ireplace("java", "PHP", $msg);
// I love PHP and PHP is great

// Array से multiple replace एक साथ
$search = ["bad", "worst", "ugly"];
$replace = ["good", "best", "beautiful"];
$review = "This is bad and ugly product";
echo str_replace($search, $replace, $review);
// This is good and beautiful product

// URL slug बनाना
$title = "PHP Strings Tutorial in Hindi";
$slug = strtolower(str_replace(" ", "-", $title));
echo $slug; // php-strings-tutorial-in-hindi
?>
substr_replace() — Position पर Replace
<?php
// Phone number mask करना
$phone = "9876543210";
$masked = substr_replace($phone, "XXXXXX", 0, 6);
echo $masked; // XXXXXX3210

// nl2br — newlines को HTML br में convert
$msg = "Line 1\nLine 2\nLine 3";
echo nl2br($msg); // Line 1<br>Line 2<br>Line 3
?>

8
String Cut Functions — काटना
substr() string का कोई भी हिस्सा निकाल सकता है — जैसे कैंची से text काटना। Blog preview, username से first name निकालना, file extension — सब में use होता है।
substr() — String का हिस्सा निकालना
<?php
$str = "HindiNotesPoint";
// 0123456789...

// substr(string, start, length)
echo substr($str, 0, 5); // Hindi (0 से 5 chars)
echo substr($str, 5, 5); // Notes (5 से 5 chars)
echo substr($str, 10); // Point (10 से end तक)
echo substr($str, -5); // Point (आखिरी 5 chars)

// Blog preview — 100 chars + "..."
$content = "PHP एक बहुत powerful language है जिससे बड़ी-बड़ी websites बनती हैं।";
if (strlen($content) > 50) {
  echo substr($content, 0, 50) . "...";
}

// Email से username निकालना
$email = "rahul@gmail.com";
$atPos = strpos($email, "@");
$username = substr($email, 0, $atPos);
echo $username; // rahul
?>

9
explode() & implode() — Split और Join
explode() string को array में तोड़ता है। implode() (या join) array को string में जोड़ता है। यह PHP के सबसे useful string functions हैं।
explode() — String को Array में तोड़ना
<?php
// CSV data process करना
$csv = "Rahul,Priya,Amit,Neha";
$names = explode(",", $csv);
print_r($names);
// Array ( [0] => Rahul [1] => Priya [2] => Amit [3] => Neha )

// Date split करना
$date = "2025-12-25";
$parts = explode("-", $date);
echo "Year: " . $parts[0]; // 2025
echo "Month: " . $parts[1]; // 12
echo "Day: " . $parts[2]; // 25

// URL path split
$path = "/php/strings/functions";
$segments = explode("/", trim($path, "/"));
print_r($segments); // [php, strings, functions]
?>
implode() — Array को String में जोड़ना
<?php
$tags = ["PHP", "Laravel", "MySQL", "HTML"];

// Array को comma-separated string बनाना
echo implode(", ", $tags);
// PHP, Laravel, MySQL, HTML

// SQL query में use
$ids = [1, 2, 3, 4, 5];
$query = "SELECT * FROM users WHERE id IN (" . implode(",", $ids) . ")";
echo $query; // SELECT * FROM users WHERE id IN (1,2,3,4,5)

// Path बनाना
$parts = ["home", "user", "documents"];
echo implode("/", $parts); // home/user/documents
?>
💡 join() = implode(): join() function implode() का alias है — दोनों same काम करते हैं। PHP documentation में implode recommend करता है। explode() में तीसरा parameter limit देता है — कितने pieces में तोड़ना है।

10
Quick Reference — String Functions एक नज़र में
FunctionकामExample → Result
strlen()Length निकालनाstrlen("Hello") → 5
strtoupper()UPPERCASEstrtoupper("hi") → HI
strtolower()lowercasestrtolower("HI") → hi
ucfirst()First letter capitalucfirst("rahul") → Rahul
ucwords()Every word capitalucwords("rahul kumar") → Rahul Kumar
trim()Whitespace हटानाtrim(" hi ") → "hi"
strpos()Position ढूँढनाstrpos("Hello", "l") → 2
str_contains()Contains check (PHP8)str_contains("Hi", "H") → true
str_replace()Replace करनाstr_replace("a","b","cat") → cbt
substr()हिस्सा निकालनाsubstr("Hello", 1, 3) → ell
explode()String → Arrayexplode(",","a,b,c") → [a,b,c]
implode()Array → Stringimplode("-",[a,b,c]) → a-b-c
str_repeat()Repeat करनाstr_repeat("ab", 3) → ababab
str_word_count()Words countstr_word_count("Hi there") → 2
str_pad()Padding add करनाstr_pad("5", 3, "0", STR_PAD_LEFT) → 005
nl2br()\n → <br>HTML display के लिए
htmlspecialchars()XSS से बचानाUser input को safe बनाना
sprintf()Formatted stringsprintf("%.2f", 3.1) → 3.10

?
अक्सर पूछे जाने वाले सवाल (FAQ)
Q: PHP में string index 0 से शुरू होता है या 1 से?
0 से। PHP (और लगभग सभी languages) में string indexing zero-based है। "Hello" में H=0, e=1, l=2, l=3, o=4। इसीलिए substr("Hello", 0, 1) → "H" और substr("Hello", 1, 1) → "e"। Negative index भी काम करता है — substr("Hello", -1) → "o" (आखिरी character)।
Q: User input को safe कैसे print करें?
हमेशा htmlspecialchars() use करो — यह XSS (Cross-Site Scripting) attacks से बचाता है। echo htmlspecialchars($userInput); — यह <script> जैसे HTML tags को &lt;script&gt; में convert करता है जो browser execute नहीं करेगा। यह web security का basic rule है।
Q: PHP में string को reverse कैसे करें?
strrev() function से। echo strrev("Hello"); → "olleH"। Palindrome check करने के लिए: $str === strrev($str) — अगर true है तो palindrome है। यह interview question में अक्सर आता है।
Q: Number को formatted string कैसे बनाएं?
number_format() और sprintf() दोनों useful हैं। number_format(1234567.891, 2, '.', ',') → "1,234,567.89" — यह price display के लिए perfect है। sprintf("%.2f", 3.14159) → "3.14"। printf() directly print करता है, sprintf() string return करता है।
Q: Multibyte strings (Hindi, Chinese) के लिए क्या करें?
Hindi/Urdu/Chinese जैसी multibyte languages के लिए mb_ prefix वाले functions use करो — mb_strlen(), mb_substr(), mb_strtolower() आदि। Regular strlen() Hindi characters को सही count नहीं करता क्योंकि एक Hindi character multiple bytes लेता है। mb_strlen("नमस्ते") → 6 (सही), strlen("नमस्ते") → 18 (bytes count)।
Q: String को integer या float में convert कैसे करें?
Type casting use करो: (int)"42abc" → 42, (float)"3.14xyz" → 3.14। या intval(), floatval() functions। PHP automatically string को number में convert करता है arithmetic operations में — "10" + 5 → 15। लेकिन explicit casting safer है और code intention clear करता है।

निष्कर्ष (Conclusion)

PHP Strings सबसे ज़्यादा use होने वाला data type है। User input, database data, HTML output — सब strings में होता है। 100+ string functions की ज़रूरत नहीं याद रखने की — 15-20 important functions daily use होते हैं।

Double quotes में variables expand होते हैं — single quotes में नहीं।

trim() + strtolower() — user input clean करने के लिए हमेशा use करो।

strpos() में === false use करो — position 0 को false मत मानो।

PHP 8 के str_contains/starts_with/ends_with बहुत cleaner हैं।

explode/implode — CSV data, URLs, tags के लिए daily use होते हैं।

htmlspecialchars() — user input print करते समय हमेशा use करो।

🚀 अगला कदम: Strings clear हो गए? अब अगले chapter में PHP Arrays सीखें — Indexed Array, Associative Array, Multidimensional Array, और 20+ Array Functions। PHP में सबसे powerful data structure।