Job interview Questions in PHP

Posted by Stephen thangaraj at 00:51

What Are the Special Characters You Need to Escape in Single-Quoted Stings?
There are two special characters you need to escape in a single-quote string: the single quote (') and the back slash (\). Here is a PHP script example of single-quoted strings:
<?php 
echo 'Hello world!'; 
echo 'It\'s Friday!'; 
echo '\\ represents an operator.'; 
?>
This script will print:
Hello world!It's Friday!\ represents an operator.
Can You Specify the "new line" Character in Single-Quoted Strings?
You can not specify the "new line" character in a single-quoted string. If you don't believe, try this script:
<?php 
echo '\n will not work in single quoted strings.'; 
?>
This script will print:
\n will not work in single quoted strings.

How Many Escape Sequences Are Recognized in Single-Quoted Strings?
There are 2 escape sequences you can use in single-quoted strings:
  • \\ - Represents the back slash character.
  • \' - Represents the single quote character.


What Are the Special Characters You Need to Escape in Double-Quoted Stings?
There are two special characters you need to escape in a double-quote string: the double quote (") and the back slash (\). Here is a PHP script example of double-quoted strings:
<?php 
echo "Hello world!"; 
echo "Tom said: \"Who's there?\""; 
echo "\\ represents an operator."; 
?>
This script will print:
Hello world!Tom said: "Who's there?"\ represents an operator.

How Many Escape Sequences Are Recognized in Double-Quoted Strings?
There are 12 escape sequences you can use in double-quoted strings:
  • \\ - Represents the back slash character.
  • \" - Represents the double quote character.
  • \$ - Represents the dollar sign.
  • \n - Represents the new line character (ASCII code 10).
  • \r - Represents the carriage return character (ASCII code 13).
  • \t - Represents the tab character (ASCII code 9).
  • \{ - Represents the open brace character.
  • \} - Represents the close brace character.
  • \[ - Represents the open bracket character.
  • \] - Represents the close bracket character.
  • \nnn - Represents a character as an octal value.
  • \xnn - Represents a character as a hex value.

How To Include Variables in Double-Quoted Strings?
Variables included in double-quoted strings will be interpolated. Their values will be concatenated into the enclosing strings. For example, two statements in the following PHP script will print out the same string:
<?php 
$variable = "and";
echo "part 1 $variable part 2\n"; 
echo "part 1 ".$variable." part 2\n"; 
?>
This script will print:
part 1 and part 2
part 1 and part 2

How Many Ways to Include Variables in Double-Quoted Strings?
There are 3 formats to include variables in double-quoted strings:
  • "part 1 $variable part 2" - This is the simplest format to include a variable in a string. The variable name starts with the dollar sign and ends at the first character that can not be used in variable name. Space is good character to end a variable name.
  • "part 1${variable}part 2" - This format helps you to clearly end the variable name. The variable name starts at dollar sign before the open brace (${) and ends at the close brace (}).
  • "part 1{$variable}part 2" - This format is also called complex format. You use this format to specify any complex variable expression in the same way as in a normal statement. The variable expression starts at ({$) followed by a variable name and ends at (}).
Here is a PHP script example of different ways to include variables in double-quoted strings:
<?php 
$beer = 'Heineken';
echo "$beer's taste is great.\n";
echo "He drank some ${beer}s and water.\n"; 
echo "She drank some {$beer}s and water.\n"; 
?>
This script will print:
Heineken's taste is great.
He drank some Heinekens and water.
She drank some Heinekens and water.

How Many Ways to Include Array Elements in Double-Quoted Strings?
There are 2 formats to include array elements in double-quoted strings:
  • "part 1 $array[key] part 2" - This is called simple format. In this format, you can not specify the element key in quotes.
  • "part 1 {$array['key']} part 2" - This is called complex format. In this format, the array element expression is specified in the same way as in a normal statement.
Here is a PHP script example of different ways to include variables in double-quoted strings:
<?php 
$fruits = array('strawberry' => 'red', 'banana' => 'yellow');
echo "A banana is $fruits[banana].\n";
echo "A banana is {$fruits['banana']}.\n";
?>
This script will print:
A banana is yellow.
A banana is yellow.
"A banana is $fruits['banana'].\n" will give you a syntax error.

How To Access a Specific Character in a String?
Any character in a string can be accessed by a special string element expression:
  • $string{index} - The index is the position of the character counted from left and starting from 0.
Here is a PHP script example:
<?php 
$string = 'It\'s Friday!';
echo "The first character is $string{0}\n"; 
echo "The first character is {$string{0}}\n"; 
?>
This script will print:
The first character is It's Friday!{0}
The first character is I

How To Assigning a New Character in a String?
The string element expression, $string{index}, can also be used at the left side of an assignment statement. This allows you to assign a new character to any position in a string. Here is a PHP script example:
<?php 
$string = 'It\'s Friday?';
echo "$string\n";
$string{11} = '!';
echo "$string\n";
?>
This script will print:
It's Friday?
It's Friday!

How to Concatenate Two Strings Together?
You can use the string concatenation operator (.) to join two strings into one. Here is a PHP script example of string concatenation:
<?php 
echo 'Hello ' . "world!\n";
?>
This script will print:
Hello world!
How To Compare Two Strings with Comparison Operators?
PHP supports 3 string comparison operators, <, ==, and >, that generates Boolean values. Those operators use ASCII values of characters from both strings to determine the comparison results. Here is a PHP script on how to use comparison operators:
<?php
$a = "PHP is a scripting language.";
$b = "PHP is a general-purpose language.";
if ($a > $b) {
  print('$a > $b is true.'."\n");
} else {
  print('$a > $b is false.'."\n");
}
if ($a == $b) {
  print('$a == $b is true.'."\n");
} else {
  print('$a == $b is false.'."\n");
}
if ($a < $b) {
  print('$a < $b is true.'."\n");
} else {
  print('$a < $b is false.'."\n");
}
?>
This script will print:
$a > $b is true.
$a == $b is false.
$a < $b is false.

How To Convert Numbers to Strings?
In a string context, PHP will automatically convert any numeric value to a string. Here is a PHP script examples:
<?php 
print(-1.3e3);
print("\n");
print(strlen(-1.3e3));
print("\n");
print("Price = $" . 99.99 . "\n");
print(1 . " + " . 2 . " = " . 1+2 . "\n");
print(1 . " + " . 2 . " = " . (1+2) . "\n");
print(1 . " + " . 2 . " = 3\n");
print("\n");
?>
This script will print:
-1300
5
Price = $99.99
3
1 + 2 = 3
1 + 2 = 3
The print() function requires a string, so numeric value -1.3e3 is automatically converted to a string "-1300". The concatenation operator (.) also requires a string, so numeric value 99.99 is automatically converted to a string "99.99". Expression (1 . " + " . 2 . " = " . 1+2 . "\n") is a little bit interesting. The result is "3\n" because concatenation operations and addition operation are carried out from left to right. So when the addition operation is reached, we have "1 + 2 = 1"+2, which will cause the string to be converted to a value 1.

How To Convert Strings to Numbers?
In a numeric context, PHP will automatically convert any string to a numeric value. Strings will be converted into two types of numeric values, double floating number and integer, based on the following rules:
  • The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero).
  • If the valid numeric data contains '.', 'e', or 'E', it will be converted to a double floating number. Otherwise, it will be converted to an integer.
Here is a PHP script example of converting some examples:
<?php 
$foo = 1 + "10.5";
echo "\$foo=$foo; type is " . gettype ($foo) . "\n";
$foo = 1 + "-1.3e3";
echo "\$foo=$foo; type is " . gettype ($foo) . "\n";
$foo = 1 + "bob-1.3e3";
echo "\$foo=$foo; type is " . gettype ($foo) . "\n";
$foo = 1 + "bob3";
echo "\$foo=$foo; type is " . gettype ($foo) . "\n";
$foo = 1 + "10 Small Pigs";
echo "\$foo=$foo; type is " . gettype ($foo) . "\n";
$foo = 4 + "10.2 Little Piggies";
echo "\$foo=$foo; type is " . gettype ($foo) . "\n";
$foo = "10.0 pigs " + 1;
echo "\$foo=$foo; type is " . gettype ($foo) . "\n";
$foo = "10.0 pigs " + 1.0;
echo "\$foo=$foo; type is " . gettype ($foo) . "\n";
?>
This script will print:
$foo=11.5; type is double
$foo=-1299; type is double
$foo=1; type is integer
$foo=1; type is integer
$foo=11; type is integer
$foo=14.2; type is double
$foo=11; type is double
$foo=11; type is double

How To Get the Number of Characters in a String?
You can use the "strlen()" function to get the number of characters in a string. Here is a PHP script example of strlen():
<?php 
print(strlen('It\'s Friday!'));
?>
This script will print:
12

How To Remove White Spaces from the Beginning and/or the End of a String?
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.
  • "\t" (ASCII 9 (0x09)), a tab.
  • "\n" (ASCII 10 (0x0A)), a new line (line feed).
  • "\r" (ASCII 13 (0x0D)), a carriage return.
  • "\0" (ASCII 0 (0x00)), the NULL-byte.
  • "\x0B" (ASCII 11 (0x0B)), a vertical tab.

Here is a PHP script example of trimming strings:
<?php
$text = "\t \t Hello world!\t \t ";
$leftTrimmed = ltrim($text);
$rightTrimmed = rtrim($text);
$bothTrimmed = trim($text);
print("leftTrimmed = ($leftTrimmed)\n");
print("rightTrimmed = ($rightTrimmed)\n");
print("bothTrimmed = ($bothTrimmed)\n");
?> 
This script will print:
leftTrimmed = (Hello world!              )
rightTrimmed = (                 Hello world!)
bothTrimmed = (Hello world!)
 
How To Remove the New Line Character from the End of a Text Line?
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);
?> 

How To Remove Leading and Trailing Spaces from User Input Values?
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...
?> 

How to Find a Substring from a Given String?
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 = "2349534134345pickzycenter16504381640386488129";
$haystack2 = "pickzycenter234953413434516504381640386488129";
$haystack3 = "center234953413434516504381640386488129pickzy";
$pos1 = strpos($haystack1, "pickzycenter");
$pos2 = strpos($haystack2, "pickzycenter");
$pos3 = strpos($haystack3, "pickzycenter");
print("pos1 = ($pos1); type is " . gettype($pos1) . "\n");
print("pos2 = ($pos2); type is " . gettype($pos2) . "\n");
print("pos3 = ($pos3); type is " . gettype($pos3) . "\n");
?>
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.



click the Below link download this file 


If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to my regular Email Updates. Subscribe Now!


Kindly Bookmark and Share it:

YOUR ADSENSE CODE GOES HERE

0 comments:

Have any question? Feel Free To Post Below:

Blog Archive

 

© 2011. All Rights Reserved | Interview Questions | Template by Blogger Widgets

Home | About | Top