PHP मा स्ट्रिङहरू

PHP मा इको र छाप्नुहोस्
PHP मा सशर्त बयान

A string is a series of characters that is used to store and modify text data. Letters, numbers, symbols, and spaces can all be included in a string. Strings in PHP are surrounded by single or double quotes. In this article we discuss the most common PHP strings functions.

उदाहरण:

$greeting = "Hello World";
$name = 'John Doe';
$age = "30";

$greeting, $name, and $age are all strings in the above example. The पहिलो two are surrounded by डबल quotations, whereas the अन्तिम is surrounded by single quotes.

Strings can be concatenated using the concatenation operator(.) and edited with string functions such as str_replace, स्ट्रि_स्पिट, स्ट्रिओस, र धेरै अन्य।

$name = "John";
$lastname = "Doe";
$fullname = $name." ".$lastname;

In the above example, $name and $lastname are strings that are concatenated to generate the variable $fullname using the concatenation अपरेटर.

PHP provides a variety of built-in string functions that make common operations like concatenation, सब्स्ट्रिंग extraction, and प्रतिस्थापन सीधा।

  1. स्ट्रिङ संयोजन:
  • दुई वा बढी स्ट्रिङहरू सँगै जोड्नको लागि कन्केटनेसन अपरेटर (।) प्रयोग गरिन्छ।
$string1 = "Hello";
$string2 = " World";
$string3 = $string1 . $string2;
echo $string3; // "Hello World"

यो .= operator can be used to append a string to the अन्त of an existing string.

$string1 = "Hello";
$string1 .= " World";
echo $string1; // "Hello World"

यो implode() method joins an array of strings into a single string by specifying a delimiter.

$array = array("apple", "banana", "orange");
$delimiter = ", ";
$string = implode($delimiter, $array);
echo $string; // "apple, banana, orange"
  1. स्ट्रिङ लम्बाइ र सबस्ट्रिङ:
$string = "Hello World";
$length = strlen($string);
echo $length; // 11

To extract a substring from a string, use the substr() function. It requires three parameters: the string, the beginning location, and the substring length.

$string = "Hello World";
$substring = substr($string,

प्रश्नोत्तर

Q: What is a string in PHP?

A: In PHP, a string is a sequence of characters used to represent text. It can contain any combination of letters, numbers, and special characters and can be contained in single or double quotations.

Q: How do I make a string?

A: In PHP, a string is produced by surrounding a sequence of characters in single or double quotes.

$string1 = "Hello, World!";
$string2 = 'Hello, World!';

Q: How can I concatenate two strings in PHP?

A: The concatenation operator can be used to join two strings.(.).

$string1 = "Hello, ";
$string2 = "World!";
$string3 = $string1 . $string2; // "Hello, World!"

Q: How can I find the length of a string in PHP?

A: The built-in function कडा आलोचना in PHP may be used to determine the length of a string.().

$string = "Hello, World!";
$length = strlen($string); // 13

Q: How can I find a specific character within a string in PHP?

A: The built-in function स्ट्रिओस can be used to find specific characters inside a string(). The स्थान of the first occurrence of a specified character inside a string is returned by this method.

$string = "Hello, World!";
$position = strpos($string, "W"); // 7

Q: How can I replace a specific part of a string in PHP?

A: The built-in function str_replace can be used to replace specific elements of a string(). This function replaces all instances of a given value within a string with a different value.

$string = "Hello, World!";
$new_string = str_replace("World", "PHP", $string); // "Hello, PHP!"

Q: How can I convert a string to uppercase or lowercase in PHP?

A: A string can be converted to uppercase using the built-in function strtoupper(), र बिल्ट-इन प्रकार्य प्रयोग गरेर सानो अक्षर गर्न strtolower().

$string = "Hello, World!";
$uppercase = strtoupper($string); // "HELLO, WORLD!"
$lowercase = strtolower($string); // "hello, world!"

Q: How can I check if a string starts or ends with a specific character in PHP?

A: You can use strpos() to see whether a string begins with a given character and substr() in conjunction with strpos() to see if a string finishes with a specific character.

$string = "Hello, World!";
$startsWith = strpos($string, "H") === 0; // true
$endsWith = strpos($string, "!") === strlen($string)-1; // true

Q: How can I split a string into an array of substrings in PHP?

A: The built-in function विस्फोट() may be used to break a text into an array of substrings. This function divides a string into an array using the delimiter supplied.

व्यायाम गर्नुहोस्:

  1. स्ट्रिङको लम्बाइ कसरी पत्ता लगाउने?
  2. How do you find the position of a substring in a string?
  3. How do you replace a substring in a string?
  4. How do you convert a string to uppercase or lowercase?
  5. How do you compare two strings?
  6. How do you trim whitespace from a string?
  7. How do you split a string into an array?
  8. How do you concatenate strings?

जवाफहरू:

  1. The length of a string can be found using the strlen() function. For example: strlen(“hello world”);
  2. The position of a substring in a string can be found using the strpos() function. For example: strpos(“hello world”, “world”);
  3. A substring in a string can be replaced using the str_replace() function. For example: str_replace(“world”, “PHP”, “hello world”);
  4. A string can be converted to uppercase or lowercase using the strtoupper() or strtolower() function. For example: strtoupper(“Hello World”);
  5. Two strings can be compared using the strcmp() function. For example: strcmp(“Hello”,”hello”);
  6. Whitespace can be trimmed from a string using the trim() function. For example: trim(” Hello World “);
  7. A string can be split into an array using the explode() function. For example: explode(” “, “Hello World”);
  8. Strings can be concatenated using the concatenation operator (.). For example: “Hello” . ” ” . “World”; or using the function implode()
PHP मा इको र छाप्नुहोस्
PHP मा सशर्त बयान
en English
X
माथि स्क्रोल गर्नुहोस्