PHP String Interview Questions & Answers

PHP Strings Interview Questions & Answers list available here.

Here, you can read about PHP String related Interview Questions & Answers – This list contains PHP String related FAQs based on functions and operations. – Get a list of Top PHP Strings Questions for Job Preparation.

What are the different data types in PHP?

PHP data type is an attribute that tells about the type of data needs to be stored. There are 8 data types in PHP:

  1. Integers: are whole numbers, without a decimal point, like 2567
  2. Doubles: are floating-point numbers, like 1.5 or 134.153
  3. Booleans: have only two possible values either true or false
  4. NULL: is a special type that only has one value: NULL
  5. Strings: is a sequence of characters, like “Hello PHP!”
  6. Arrays: stores multiple values in one single variable
  7. Objects: instances of user-defined classes
  8. Resources: are special variables that hold references to resources such as database call

How will you concatenate two strings in PHP?

To concatenate two string variables together, use the dot (.) operator.

What is the difference between single quote and double quote string in PHP?

PHP single quotes execute slight faster than double quotes but single quote does not parse variables. Here is the difference between single quote and double quote string in PHP:

Single quotes

  1. The simplest method to declare a string is using single quotes. They are faster because everything written inside single quotes is treated as plain string.
  2. This is useful when we need to output exactly as it is written in single quotes. If we place a variable inside single quotes, it will output as a same variable name but not its value.
  3. Using single quoted string, PHP will not evaluate most escaped characters except single quote with backslash (\’). For an example, If we want to output single quoted word inside single quoted string: echo ‘This is \’sample\’ string’;

Double quote

  1. It will parse various escaped characters, regular expressions, and variables in the strings.
  2. We can also use curly braces to wrap variables if additional word needs to attach with variable value output. For example if we have variable name $number (with its value 100) and you what to echo “The 100s kids are there”. You can wrap variable with curly braces such as: “The {$number}s kids are there”.
  3. Because of parsing escaped characters & variables, this is slower than single quoted string.

Note: If there is no variable or escaped characters in string, use single quote else double quote.

Why we use PHP String functions trim(), ltrim() and rtrim()?

PHP trim related functions are used to remove white spaces or other given characters from the beginning and end of the strings.

  • trim() removes space & predefined characters from both sides of given string.
  • ltrim() removes space & predefined characters from the left side of the sting.
  • rtrim() removes space & predefined characters from the right side of the sting.

These functions are helpful when we want to clean strings from unwanted spaces or characters.

Syntax: trim(string,charlist)


What PHP Function is used to find position of a string?

PHP strpos() function is used to find the first occurrence of substring in the given string.

Output: 13

The word ‘apple’ is found at the position ’13’. Remember that the position of string/character start at 0, and not 1. This function is very useful to find certain patterns or words in a string.

What is the use of ucwords() PHP String function?

PHP ucwords() function is used to capitalized the first letter of each word in String.

Run :

What is the use of str_replace() PHP String function?

The PHP str_replace() function is used to search & replace some words in a strings.

In this example, “Hello” word will be replaced by “Hi” in the given string.

Run :

How to find String Length in PHP?

The PHP strlen() function returns the length of a string. String length count will include space & special characters as well.
Syntax: strlen(string);



How can we remove the white spaces from string in PHP?

There are three functions in PHP to remove the white spaces from the string.
trim()-It removes whitespaces from the left and right side of the string.
ltrim()-It removes whitespaces from the left side of the string.
rtrim()-It removes whitespaces from the right side of the string.