PHP String

PHP

PHP Strings का उपयोग sequence of characters (text) को स्टोर करने के लिए किया जाता हैं। एक string को बनाने का सबसे सरल तरीका उसे single quotation marks (' ') या double quotation marks (" ") से घेराव कर दे। लेकिन दोनों अलग तरह से काम करते हैं।

Single quotes से घिरा हुआ strings शाब्दिक रूप से व्यवहार करता है, पूरी तरह से जैसा है वैसा ही डिस्प्ले करता हैं। यानी की अगर Single quotes से घिरा हुआ कोई variable है तो वह variable को ही डिस्प्ले करता है ना की उसकी वैल्यू को।

Double quotes से घिरा हुआ strings किसी भी variable की वैल्यू को डिस्प्ले करता है। इसके साथ साथ यह कुछ escape-sequence को भी interpret करता हैं।

Example: PHP Strings
<?php
$variable = 'Morning';
echo "Good $variable <br>"; // Result: Good Morning
echo 'Good $variable <br>'; // Result: Good $variable
echo "<br>";
echo "Good\tMorning <br>"; // Result: Good Morning
echo 'Good\tMorning <br>'; // Result: Good\tMorning
?>

Special Characters

बैकस्लैश ( \ ) के साथ शुरू होने वाले कुछ character को special escape characters के साथ बदल दिया जाता है। निचे कुछ escape characters दिए गए हैं।

Character Result
\" Replaced by a single double-quote (")
\\ Replaced by a single backslash (\)
\n Replaced by the newline character
\r Replaced by the carriage-return character
\t Replaced by the tab character
\$ Replaced by the dollar sign ($)

String Concatenation

दो या दो से अधिक String को जोड़ने को ही Concatenation कहा जाता है। Concatenation हमेसा एक dot (.) Operator की सहायता से किया जाता है।

Example: PHP Strings Concatenation
<?php
$string1 = "Good";
$string2 = "Morning";
echo $string1 . $string2; // Result: GoodMorning
echo "<br>";
echo $string1 . " " . $string2; // Result: Good Morning
?>

PHP String Functions

PHP में पहले से ही बने बनाये कई functions है जिसका अपना-अपना, अलग-अलग कार्य होते जैसे की strlen(), strpos(), strtoupper(), strtolower(), etc. इसी तरह के और भी सारे String फंक्शन के लिए, दिए गए लिंक पर जाये PHP String Function.

  1. strlen()        : String की लम्बाई पता करने के लिए।
  2. strtoupper() : String को UPPERCASE में बदलने के लिए।
  3. strtolower() : String को lowercase में बदलने के लिए।
  4. strpos()        : एक String में से किसी character की position पता करने के लिए।
Example:1 PHP Strings Function
<?php
$string = "Good Morning";
echo strlen($string); //Result: 12
echo "<br>";
echo strtoupper($string); //Result: GOOD MORNING
echo "<br>";
echo strtolower($string); //Result: good morning
?>

ऊपर दिए गए example में string की length 12 है क्योँ की PHP में खाली स्थान (white-space) की भी गिन्ती होती हैं।


Example:2 PHP Strings Function strpos()
<?php
$string = "Good Morning";
echo strpos($string, "Morning"); //Result: 5
echo "<br>";
echo strpos($string, "b"); //Result: will be blank
echo "<br>";
echo strpos($string, "o"); //Result: 1
?>

जैसे ही कोई मैच मिल जाता है एक String में, strpos() सबसे पहले मिला हुआ position को दिखता है जैसे की तीसरे कोड में देख सकते है। अगर कोई मैच नहीं मिलता है तो, output में कुछ भी नहीं दिखाई देगा क्योँ की जिस charactor को ढूंड रहा था वह है ही नहीं जैसे की दुसरे कोड में देख सकते हैं।

Note: PHP में positioning हमेसा 0, से शुरु होते है, जैसे 0123456.....
Article By: Brajlal Prasad
Created on: 16 Feb 2023  4996  Views
 Print Article
Report Error

If you want to report an error, or any suggestion please send us an email to [email protected]