String Interview Questions in PHP

PHP String Interview Questions
Download PHP String Interview Questions PDF

Below are the list of Best PHP String Interview Questions and Answers

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.

The int or integer used for a variable into an integer 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; ?>

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

Example:-

echo strlen(“creative writing”);

Some list escape characters in PHP there are:-

  • \’
  • \”
  • \\
  • \n
  • \t
  • \r

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));

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

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.

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

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.

The String function used For manipulation the string in PHP.

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

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

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.

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;

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 );

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.

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

Example

<?php

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

?>

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.

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

Example:

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