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

PHP Strings का Introduction

एक string text का एक टुकड़ा है, किसी ribbon पर लिखे एक word या sentence जैसा। PHP आपको इसे single quotes में लिखने देता है, जो इसे plain रखते हैं, या double quotes में, जो variables भर देते हैं।
Syntax
php
$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
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
// Print 'Hello, $name' to the output
echo 'Hello, $name';
?>

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
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
// Print "Hello, $name\n" to the output
echo "Hello, $name\n";
?>

String Concatenation

. operator दो strings को end-to-end जोड़कर एक बना देता है — $greeting = 'Hello, ' . $name; — और .= वही काम करते हुए साथ ही result को वापस left-hand variable में reassign भी कर देता है, जो पहले से इसमें था उसके अंत में जोड़ते हुए।

उदाहरण: String Concatenation

php
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
// Declare `$greeting`, set to `'Hello, ' . $name`
$greeting = 'Hello, ' . $name;
// Print `$greeting` to the output
echo $greeting;
?>

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
<?php
// Declare `$text`, set to "Hello!"
$text = "Hello!";
// Print `strlen($text)` to the output
echo strlen($text);
?>

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
<?php
// Declare `$word`, set to "Hello"
$word = "Hello";
// Print `$word[0]` to the output
echo $word[0];
?>
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. Single quotes इस्तेमाल करना और variables के replace होने की उम्मीद करना, ताकि 'Hello $name' $name को literally print करे।
  2. strings जोड़ने के लिए + इस्तेमाल करना, जो PHP में arithmetic करता है; इसके बजाय . इस्तेमाल करें।
  3. "é" जैसे multi-byte text पर strlen() इस्तेमाल करना और characters की उम्मीद करना, जबकि यह bytes count करता है; 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.