Tech Mahindra PHP Most Frequently Asked Latest Interview Questions Answers
How To Process The Uploaded Files?
How to process the uploaded files? The answer is really depending on your application. For example:
You can attached the outgoing emails, if the uploaded files are email attachments.
You can move them to user's Web page directory, if the uploaded files are user's Web pages.
You can move them to a permanent directory and save the files names in the database, if the uploaded files are articles to be published on the Web site.
You can store them to database tables, if you don't want store them as files.
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 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 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 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 Take A Substring From A Given String?
If you know the position of a substring in a given string, you can take the substring out by the substr() function. Here is a PHP script on how to use substr():
<?php
$string = "beginning";
print("Position counted from left: ".substr($string,0,5)."\n");
print("Position counted form right: ".substr($string,-7,3)."\n");
?>
This script will print:
Position counted from left: begin
Position counted form right: gin
substr() can take negative starting position counted from the end of the string.
How To Join Multiple Strings Into A Single String?
If you multiple strings stored in an array, you can join them together into a single string with a given delimiter by using the implode() function. Here is a PHP script on how to use implode():
<?php
$date = array('01', '01', '2006');
$keys = array('php', 'string', 'function');
print("A formated date: ".implode("/",$date)."\n");
print("A keyword list: ".implode(", ",$keys)."\n");
?>
This script will print:
A formated date: 01/01/2006
A keyword list: php, string, function
What Is An Array In Php?
An array in PHP is really an ordered map of pairs of keys and values.
Comparing with Perl, an array in PHP is not like a normal array in Perl. An array in PHP is like an associate array in Perl. But an array in PHP can work like a normal array in Perl.
Comparing with Java, an array in PHP is not like an array in Java. An array in PHP is like a TreeMap class in Java. But an array in PHP can work like an array in Java.
How To Test If A Variable Is An Array?
Testing if a variable is an array is easy. Just use the is_array() function. Here is a PHP script on how to use is_array():
<?php
$var = array(0,0,7);
print("Test 1: ". is_array($var)."\n");
$var = array();
print("Test 2: ". is_array($var)."\n");
$var = 1800;
print("Test 3: ". is_array($var)."\n");
$var = true;
print("Test 4: ". is_array($var)."\n");
$var = null;
print("Test 5: ". is_array($var)."\n");
$var = "PHP";
print("Test 6: ". is_array($var)."\n");
print("\n");
?>
This script will print:
Test 1: 1
Test 2: 1
Test 3:
Test 4:
Test 5:
Test 6:
How To Retrieve Values Out Of An Array?
You can retrieve values out of arrays using the array element expression $array[$key].
Here is a PHP example script:
<?php $languages = array(); $languages["Zero"] = "PHP"; $languages["One"] = "Perl";
$languages["Two"] = "Java"; print("Array with inserted values:\n");
print_r($languages); ?>
This script will print:
Array with default keys:
The second value: Perl
Array with specified keys:
The third value: Java
How To Replace A Substring In A Given String?
If you know the position of a substring in a given string, you can replace that substring by another string by using the substr_replace() function. Here is a PHP script on how to use substr_replace():
<?php
$string = "Warning: System will shutdown in NN minutes!";
$pos = strpos($string, "NN");
print(substr_replace($string, "15", $pos, 2)."\n");
sleep(10*60);
print(substr_replace($string, "5", $pos, 2)."\n");
?>
This script will print:
Warning: System will shutdown in 15 minutes!
(10 minutes later)
Warning: System will shutdown in 5 minutes!
Like substr(), substr_replace() can take negative starting position counted from the end of the string.
How To Convert Strings To Upper Or Lower Cases?
Converting strings to upper or lower cases are easy. Just use strtoupper() or strtolower() functions. Here is a PHP script on how to use them:
<?php
$string = "PHP string functions are easy to use.";
$lower = strtolower($string);
$upper = strtoupper($string);
print("$lower\n");
print("$upper\n");
print("\n");
?>
This script will print:
php string functions are easy to use.
PHP STRING FUNCTIONS ARE EASY TO USE.
How To Process The Uploaded Files?
How to process the uploaded files? The answer is really depending on your application. For example:
You can attached the outgoing emails, if the uploaded files are email attachments.
You can move them to user's Web page directory, if the uploaded files are user's Web pages.
You can move them to a permanent directory and save the files names in the database, if the uploaded files are articles to be published on the Web site.
You can store them to database tables, if you don't want store them as files.
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.
Tech Mahindra PHP Most Frequently Asked Latest Interview Questions Answers |
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 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 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 Take A Substring From A Given String?
If you know the position of a substring in a given string, you can take the substring out by the substr() function. Here is a PHP script on how to use substr():
<?php
$string = "beginning";
print("Position counted from left: ".substr($string,0,5)."\n");
print("Position counted form right: ".substr($string,-7,3)."\n");
?>
This script will print:
Position counted from left: begin
Position counted form right: gin
substr() can take negative starting position counted from the end of the string.
How To Join Multiple Strings Into A Single String?
If you multiple strings stored in an array, you can join them together into a single string with a given delimiter by using the implode() function. Here is a PHP script on how to use implode():
<?php
$date = array('01', '01', '2006');
$keys = array('php', 'string', 'function');
print("A formated date: ".implode("/",$date)."\n");
print("A keyword list: ".implode(", ",$keys)."\n");
?>
This script will print:
A formated date: 01/01/2006
A keyword list: php, string, function
What Is An Array In Php?
An array in PHP is really an ordered map of pairs of keys and values.
Comparing with Perl, an array in PHP is not like a normal array in Perl. An array in PHP is like an associate array in Perl. But an array in PHP can work like a normal array in Perl.
Comparing with Java, an array in PHP is not like an array in Java. An array in PHP is like a TreeMap class in Java. But an array in PHP can work like an array in Java.
How To Test If A Variable Is An Array?
Testing if a variable is an array is easy. Just use the is_array() function. Here is a PHP script on how to use is_array():
<?php
$var = array(0,0,7);
print("Test 1: ". is_array($var)."\n");
$var = array();
print("Test 2: ". is_array($var)."\n");
$var = 1800;
print("Test 3: ". is_array($var)."\n");
$var = true;
print("Test 4: ". is_array($var)."\n");
$var = null;
print("Test 5: ". is_array($var)."\n");
$var = "PHP";
print("Test 6: ". is_array($var)."\n");
print("\n");
?>
This script will print:
Test 1: 1
Test 2: 1
Test 3:
Test 4:
Test 5:
Test 6:
How To Retrieve Values Out Of An Array?
You can retrieve values out of arrays using the array element expression $array[$key].
Here is a PHP example script:
<?php $languages = array(); $languages["Zero"] = "PHP"; $languages["One"] = "Perl";
$languages["Two"] = "Java"; print("Array with inserted values:\n");
print_r($languages); ?>
This script will print:
Array with default keys:
The second value: Perl
Array with specified keys:
The third value: Java
How To Replace A Substring In A Given String?
If you know the position of a substring in a given string, you can replace that substring by another string by using the substr_replace() function. Here is a PHP script on how to use substr_replace():
<?php
$string = "Warning: System will shutdown in NN minutes!";
$pos = strpos($string, "NN");
print(substr_replace($string, "15", $pos, 2)."\n");
sleep(10*60);
print(substr_replace($string, "5", $pos, 2)."\n");
?>
This script will print:
Warning: System will shutdown in 15 minutes!
(10 minutes later)
Warning: System will shutdown in 5 minutes!
Like substr(), substr_replace() can take negative starting position counted from the end of the string.
How To Convert Strings To Upper Or Lower Cases?
Converting strings to upper or lower cases are easy. Just use strtoupper() or strtolower() functions. Here is a PHP script on how to use them:
<?php
$string = "PHP string functions are easy to use.";
$lower = strtolower($string);
$upper = strtoupper($string);
print("$lower\n");
print("$upper\n");
print("\n");
?>
This script will print:
php string functions are easy to use.
PHP STRING FUNCTIONS ARE EASY TO USE.
Post a Comment