Logo F2FInterview

PHP Interview Questions

Q   |   QA

There are 4 PHP functions you can use remove white space characters from the beginning and/or the end of a string:

trim() - Remove white space characters from the beginning and the end of a string.
ltrim() - Remove white space characters from the beginning of a string.
rtrim() - Remove white space characters from the end of a string.
chop() - Same as rtrim().

White space characters are defined as:

" " (ASCII 32 (0x20)), an ordinary space.
"" (ASCII 9 (0x09)), a tab.
" " (ASCII 10 (0x0A)), a new line (line feed).
" " (ASCII 13 (0x0D)), a carriage return.
"" (ASCII 0 (0x00)), the NULL-byte.
"x0B" (ASCII 11 (0x0B)), a vertical tab.
Here is a PHP script example of trimming strings:
<?php
$text = " Hello world! ";
$leftTrimmed = ltrim($text);
$rightTrimmed = rtrim($text);
$bothTrimmed = trim($text);
print("leftTrimmed = ($leftTrimmed) ");
print("rightTrimmed = ($rightTrimmed) ");
print("bothTrimmed = ($bothTrimmed) ");
?>

This script will print:

leftTrimmed = (Hello world!              )

rightTrimmed = (         Hello world!)

bothTrimmed = (Hello world!)

If you are using fgets() to read a line from a text file, you may want to use the chop() function to remove the new line character from the end of the line as shown in this PHP script:

<?php
$handle = fopen("/tmp/inputfile.txt", "r");
while ($line=fgets()) {
$line = chop($line);
# process $line here...
}
fclose($handle);
?>

If you are taking input values from users with a Web form, users may enter extra spaces at the beginning and/or the end of the input values. You should always use the trim() function to remove those extra spaces as shown in this PHP script:

<?php
$name = $_REQUEST("name");
$name = trim($name);
# $name is ready to be used...
?>

To find a substring in a given string, you can use the strpos() function. If you call strpos($haystack, $needle), it will try to find the position of the first occurrence of the $needle string in the $haystack string. If found, it will return a non-negative integer represents the position of $needle. Othewise, it will return a Boolean false. Here is a PHP script example of strpos():

<?php
$haystack1 = "2349534134345globalguideline16504381640386488129";
$haystack2 = "globalguideline234953413434516504381640386488129";
$haystack3 = "guideline234953413434516504381640386488129ggl";
$pos1 = strpos($haystack1, "globalguideline");
$pos2 = strpos($haystack2, "globalguideline");
$pos3 = strpos($haystack3, "globalguideline");
print("pos1 = ($pos1); type is " . gettype($pos1) . " ");
print("pos2 = ($pos2); type is " . gettype($pos2) . " ");
print("pos3 = ($pos3); type is " . gettype($pos3) . " ");
?>

This script will print:

pos1 = (13); type is integer
pos2 = (0); type is integer
pos3 = (); type is boolean

"pos3" shows strpos() can return a Boolean value

Because strpos() could two types of values, Integer and Boolean, you need to be careful about testing the return value. The best way is to use the "Identical(===)" operator. Do not use the "Equal(==)" operator, because it does not differentiate "0" and "false". Check out this PHP script on how to use strpos():

<?php
$haystack = "needle234953413434516504381640386488129";
$pos = strpos($haystack, "needle");
if ($pos==false) {
print("Not found based (==) test ");
} else {
print("Found based (==) test ");
}
if ($pos===false) {
print("Not found based (===) test ");
} else {
print("Found based (===) test ");
}
?>

This script will print:

Not found based (==) test
Found based (===) test

Of course, (===) test is correct. 

In order to link this F2FInterview's page as Reference on your website or Blog, click on below text area and pres (CTRL-C) to copy the code in clipboard or right click then copy the following lines after that paste into your website or Blog.

Get Reference Link To This Page: (copy below code by (CTRL-C) and paste into your website or Blog)
HTML Rendering of above code: