PHP String Interview Questions And Answers Online

1) What is String in PHP?

The String is a collection or set of characters in sequence in PHP. The String in PHP is used to save and manipulate like an update, delete and read the data.

2) How to cast a php string to integer?

The int or integer used for a variable into an integer in PHP.

3) How to perform string concatenation in PHP?

The string concatenation means two strings connect together. The dot ( . ) sign used in PHP to combine two string.

Example:-

<?php $stringa = ” creative “;
$stringb = “writing “;
echo $stringa . $stringb; ?>

4) How to get php string length?

To determine the string length in PHP, strlen() function used.

Example:-

echo strlen(“creative writing”);

5) List some escape characters in PHP?

Some list escape characters in PHP there are:-

  • \
  • n
  • t
  • r

6) How to convert special characters to unicode in php?

There is function json_encode() which is converted special characters to Unicode in PHP.

Example:-

$stringa = ” I am good at smiling”;
print_r(json_encode($stringa));

7) How to add double quotes in string in php?

The double quotes in string using ( ) sign and write content under this sign. The double quotes convert variable as value.

An example is below:-

$str8 = ” need to read data where has to go “;
echo $str 8
output: double quotes in the string
echo “ PHP work $str8
output: PHP work need to read data where has to go

8) Explain how php string interpolation is done?

The interpolation is adding variables in among a string data. PHP parses the interpolate variables and replaces this variable with own value while processing the string.

9) How to convert string to an array in PHP?

The str_split function used to partition of the string into equal length. Then the string part becomes an array element. The array element contains the remaining end string.

$str1 = ‘abcdefghijklmnopqrstuvwxyzpq’;
$split1 = str_split($str1, 5);
print_r($split1);

OUTPUT of the following code

  • [0] = abcde
  • [1] = fghij
  • [2] = klmno
  • [3] = pqrst
  • [4] = uvwxy
  • [5] = zpq

10) What is Heredoc in PHP?

The Heredoc in PHP syntax is a write multi-line string inside PHP without using the single quote and double quotes delimiters. It works using <<< symbol and a token is a mark the end of the string.

11) List some PHP library string functions?

The String function used For manipulation the string in PHP.

  • Strlen()
  • Strcpy()
  • Strcat()
  • Strcmp()
  • Strwr()
  • Strupr()

12) What is use of strtoupper in PHP?

The strtoupper() function in PHP allows converting the lowercase alphabet into the uppercase alphabet in the string.

13) How to find capital letter in string php?

To find out the capital letter, the ctype_upper() function is used in PHP. In the string, check the character is in uppercase or not. If a character is uppercase then return True otherwise False.

14) Write the syntax of php mb_strtolower function?

PHP mb_strtolower function is used to converts uppercase character into lowercase in the string.

$str1 = “I am good “;
$str1 = mb_strtolower($str1);
echo $str1;

mb_strtolower function does not affect the Unicode characters also.

$str = “Τάχιστη ηξ βαφής ψημένη γη, δρασκελίζει κυνός”;
$str = mb_strtolower($str, ‘UTF-8’);
echo $str;

15) How to convert array to string in php?

The array converts into a string in PHP using implode() function. The example shows the below.

$list_arr = array( ‘food’, ‘bread’, ‘bazar’ );
echo implode( “, “, $list_arr );

16) What is difference between stripos and strpos?

The stripos and strops are finding the position of the first occurrence of a string in another string.

The strops() function case sensitive or stripos() function is not case sensitive.

17) How to reverse a string in PHP?

You can use PHP inbuilt function strrev() to reverse a string.

Example

<?php

$string=”priyag”;
echo $rev_string= strrev($string); // Output gayirp

?>

18) What is use of TRIM function in PHP?

The trim() function in the PHP used to remove space and predefined function. This function removes white space and other arguments like a new line, brake line from left to right in the string.

19) How to add line break in PHP string?

To line break nl2br() function and n command use in PHP string.

Example:

echo nl2br( “ creative writing n sensitive reading”);

Leave a Comment

Your email address will not be published. Required fields are marked *