Introduction
- The string is the core-components part of Php for creating and manipulating strings in various ways.
Definition
- A string is a sequence of characters of arbitrary length.
Characteristics
- String literals/contents are denoted by either single or double quotes. For example – ‘Employee’ or “Employee” or “512” or ‘512’.
- To check whether two strings are equal, the == (double equals) comparison operator is used.
- String Escape Sequence :
- Double quotes symbols are also used in a variety of string escapes to insert special characters in the program.
- Escape Sequence – Character Represented
- Double quotes symbols are also used in a variety of string escapes to insert special characters in the program.
\” – Double quotes
\n – New Line
\t – Tab
\\ – Backslash
\$ – Dollar sign
\{ – Left brace
\} – Right brace
\[ – Left bracket
\] – Right bracket
- String Concatenation Operator :
- The concatenation operator appends the multiple operands as one string value.
- For example –
- String Printing in Php :
- There are four ways to print strings in Php. These are –
- echo statement :
- Echo is considered a language construct (not as a true function) hence, echo can be used without the () symbol.
- Due to construct, it can’t be used with an if statement.
- Print single or multiple string output at a time.
- For example –
- echo “India”;
- echo (“India”);
- echo “First”, “Second”, “Third”; [Output: FirstSecondThird]
- echo (“First”, “Second”, “Third”); [Output: Parse Error]
- print() statement :
- Print only one string output at a time.
- Due to function, it can be used with an if statement.
- For example –
- print “India”; [Output: Parse Error, due to print is a function & needed ( )]
- print(“India”); [Output: India]
- if (print(“Yes”)) { echo “We are Indian”;}
- print(“First”, “Second”, “Third”); [Output: Parse Error, due to multiple outputs]
- printf() statement :
- This statement prints a formatted string by inserting a value by substituting the value into a template similar to C.
- printf(“%6d\n”,7);
printf(“%3d\n”,7);
printf(“%03d\n”,7);
printf(“%.2f\n”,453.21546);
printf(“%.2f\n”,453.21246);
printf(“%.3f\n”,453.21546);
printf(“%.5f\n”,453.2);
printf(“The hexadecimal value of %d is %x\n”, 300,300);
printf(“%04d-%04d-%d\n”,05,11,2025);
printf(“The interest amount is %.3f%%\n”,25.32);
printf(“I\’ve %d rupees\n”,2500);
printf(“\”The total amount is\” %d rupees”,2500);
- echo statement :
- There are four ways to print strings in Php. These are –
Output:
7
7
007
453.22
453.21
453.215
453.20000
The hexadecimal value of 300 is 12c
0005-0011-2025
The interest amount is 25.320%
I’ve 2500 rupees
“The total amount is” 2500 rupees
-
- print_r() statement :
- Print the contents of arrays, objects, and others in more or less human-readable form.
- It is useful for debugging purposes.
- sprintf() statement:
- The sprintf() function helps in writing a formatted string.
- Example:
- $num = 1947;
$str = “India”;
$val = sprintf(“%s became free in %u year.”,$str,$num);
echo $val; //Output: India became free in the 1947 year. - $num = 15;
$str = $num;
$val = sprintf(“The Decimal value of %d is %b.”,$num,$str);
echo $val; //Output: The Decimal value of 15 is 1111.
- $num = 1947;
- print_r() statement :
In-built String Function in Php
- PHP has several in-built functions regarding string to compare, search, replace, and trim strings, for working with HTTP, HTML, and SQL encodings. Some common in-built string functions are –
- is_string():
- An in-built function is_string() is used to test whether a value is a string. The syntax is -(is_string($String_variablename)).
- bin2hex():
- This function converts a string of ASCII characters into hexadecimal values.
- Syntax: bin2hex(string)
- Example: echo bin2hex(“Hello India”); //[Output: 48656c6c6f20496e646961]
- chr():
- This function returns a character from the specified ASCII value. The ASCII value can be specified in decimal, octal, or hex values. Octal values are written by a leading 0, while hex values are written by a leading 0x.
- Syntax: chr(ascii value)
- Example:
- echo chr(56) ; // decimal value [Output: 8]
- echo chr(041) ; // Octal value [Output: !]
- echo chr(0x48); // Hex value [Output: H]
- echo():
- This function displays the output.
- echo is not actually a function hence no () symbol is used.
- echo is faster than print() function.
- Syntax: echo variable_name/string_message.
- Example:
- echo “Hello India”;
- echo ‘Our ‘,’India ‘,’is ‘,’Great ‘; //[Our India is Great]
- $str=”Hello India”;
echo $str;
- md5():
- This function converts or encrypts the given string in hash form [in binary or 32-bit hex format] using RSA security algorithm.
- Syntax: md5(string_name/string_variable)
- Example: echo md5(“Hello India”); //Output: 6ad0525bedec9965301c0c5e51f64a92
- nl2br():
- This function helps to insert new line or break the line where \n present in a string.
- \n works with this function.
- Syntax: nl2br(string1 \n string2 \n string n)
- Example: echo nl2br(“Welcome \n 2023”);
- ord():
- The ord() function returns the numeric ASCII value of the first character of a string.
- Syntax: ord(string)
- Example: echo ord(“Hello India”); //Output: 72
- print() & printf():
- Mention in the above paragraph.
- strcmp():
- This function compares two strings and gives result as a numeric value. If it is 0, strings are equal and if greater or less than 0 are not equal.
- It is case-sensitive.
- Example:
- $str1=”India”;
$str2=”India”;
$str3=”America”;
$str4=”india”;
echo strcmp($str1,$str2).”<br>”; //Output=0
echo strcmp($str1,$str3).”<br>”; //Output=8
echo strcmp($str1,$str4).”<br>”; //Output=-32
- $str1=”India”;
- strcasecmp() is similar as strcmp() but not case sensitive.
- strncmp() is similar to strcmp() but compares up to n length and is case sensitive whereas strncasecmp() is not case sensitive.
- strlen():
- This function is used to find out the length/characters of a string including space.
- Syntax:
- strlen(string_value/string_variable).
- Example:
- echo strlen(“Hello India”); // [Output: 11]
-
$x=”Hello India”;
- str_pad():
- This function adds some extra characters as Pad to the right side of the string, up to a new length as per need.
- Example:
- $str = “Hello India”;
echo str_pad($str,23,”N”); //Output: Hello IndiaNNNNNNNNNNNN
- $str = “Hello India”;
- strpos():
- This function is used to search for a specific word/text from a sentence or string. If a match occurs, it gives the character position as the index value of the first match. If no match is found, it returns FALSE.
- The position counting starts from the beginning/first letter of the string taking index value 0, not from 1.
- Syntax:
- strpos(string_value/string_variable, search_text).
- Example:
- echo strpos(“Hello India”, “India”); // [Output: 6]
-
$x=”Hello India”;
echo strpos(x, “India”); // [Output: 6]
- str_replace():
- This function replaces some characters with some other given characters in a string.
- This is case sensitive function.
- Another function is str_ireplace() which is not case-sensitive.
- Syntax:
- str_replace(find_letter, replace_letter, string_sentence);
- Example:
- echo str_replace(“India”, “World”, “Hello India”); //[ Output: Hello World]
- strrev():
- This function is used to reverse a given string.
- Syntax:
- strrev(string_value/string_variable).
- Example:
- echo strrev(“Hello India”);
-
$x=”Hello India”;
strrev($x);
- strstr():
- This function replaces certain characters or words from a string.
- It is case-sensitive.
- Example:
- echo strstr(“Hello India” “India” “World”); //Output: Hello World
- echo strstr(“Hello India” “i” “o”); //Output: Hello Indoa
- $arr2 = array(“Hello” => “Our”, “World” => “Universe”); // In Array
echo strtr(“Hello World”,$arr2); //output: Our Universe
- strtolower():
- This function converts all characters to lowercase letters.
- Example:
- echo strtolower(“Hello INdiA.”); //output: hello india
- strtoupper():
- This function converts all characters to uppercase letters.
- Example:
- echo strtoupper(“Hello INdiA.”); //output: HELLO INDIA
- NB:
- lcfirst() – This function converts the first character of a string/sentence to lowercase letters.
- ucfirst() – This function converts the first character of a string/sentence to uppercase letters.
- ucwords() – This function converts the first character of each word in a string/sentence to uppercase.
- str_word_count():
- This function counts the total number of words present in a string/sentence.
- Syntax:
- str_word_count(string_value/string_variable).
- Example:
- echo str_word_count(“Hello India”);
-
$x=”Hello India”;
str_word_count($x);
- substr():
- This function extracts part of the string as per the given position value.
- Example:
- echo substr(“Hello India”,6) //output: India
- echo substr(“Hello India”,9) // output: ia
- echo substr(“Hello India”,-3) // output: dia
- echo substr(“Hello world”,-8) // output: lo world
- echo substr(“Hello world”,3,8) //output: lo wo
- echo substr(“Hello world”,0,5) // output: Hello
- echo substr(“Hello world”,7,7) //output: orld
- echo substr(“Hello world”, 0,-6) //output: Hello
- echo substr(“Hello world”,-10,-4) //output: ello w
- trim():
- This function removes extra whitespace [null, new line, horizontal tab, vertical tab, space etc.] from both sides/ends of the string.
- Syntax: trim(string_name/string_variable)
- Example: echo trim(” Hello India “); //Output: Hello India
- NB: rtrim() and ltrim() function removes extra whitespace from the right side and left side of the string.
0 Comments