PHP String Interview Questions & Answers
PHP Strings Interview Questions & Answers list available here.
Here, you can read about PHP String related interview questions & answers. These PHP String FAQs is related to string functions such as substr(), strlen(), str_replace(), trim(), strpos() etc.
It is recommended for students to practice exercises and review interview questions to prepare for the job well.
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:
- Integers: are whole numbers, without a decimal point, like 2567
- Doubles: are floating-point numbers, like 1.5 or 134.153
- Booleans: have only two possible values either true or false
- NULL: is a special type that only has one value: NULL
- Strings: is a sequence of characters, like “Hello PHP!”
- Arrays: stores multiple values in one single variable
- Objects: instances of user-defined classes
- Resources: are special variables that hold references to resources such as database calls
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 slightly faster than double quotes but a single quote does not parse variables. Here is the difference between a single quote and double quote string in PHP:
Single quotes
- The simplest method to declare a string is using single quotes. They are faster because everything written inside single quotes is treated as a plain string.
- 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 the same variable name but not its value.
- Using single-quoted string, PHP will not evaluate most escaped characters except a single quote with a backslash
(\')
. For example, If we want to output single quoted word inside single-quoted string:echo 'This is \' sample\' string';
Double quote
- It will parse various escaped characters, regular expressions, and variables in the strings.
- 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"
. - Because of parsing escaped characters & variables, this is slower than single quoted string.
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)
Example:
$sample_string = ' Welcome to TutorialsClass.com ';
$clean_string = trim($sample_string);
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.
<?php
echo strpos("The color of apple is red.","apple");
?>
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.
<?php
echo ucwords("welcome to the php tutorials");
?>
Run : http://ideone.com/83vvyF
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.
Example
<?php
echo str_replace("Hello", "Hi", "Hello PHP!");
?>
In this example, "Hello"
word will be replaced by "Hi"
in the given string.
Run : http://ideone.com/AFs0DW
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);
Example:
echo strlen("Hello world!");
12
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.
Example:
<?php
$string=" Interview Question and Answers for your help ";
$var1=trim($string);
echo $var1;
$var2=ltrim($string);
echo $var2;
$var3=rtrim($string);
echo $var3;
?>