10 May 2011

interview questions: PHP strings


Contributed by:Viji.V

Q: How to reverse the given string?


Answer Use the following code:
$len= strlen($str);
for($i=($len-1); $i>=0; $i++)
{
echo $str[$i];
}
http://www.w3schools.com/PHP/php_ref_string.asp


http://www.mycomputerforum.com/tutorials/php_strings.html


http://www.plus2net.com/php_tutorial/php_string_functions.php


Q: Why strip_tags() method is used?

Answer: strip_tags- It strips the string from HTML/PHP tags.


http://www.w3schools.com/PHP/php_ref_string.asp

http://www.tizag.com/phpT/strings.php

http://www.mycomputerforum.com/tutorials/php_strings.html

Q: What is the functionality of ucword?

Answer: ucword() converts first latter of all words into upper case in the given string. Ex: ucword("dinesh saini");
Output: Dinesh Saini

http://www.plus2net.com/php_tutorial/php_string_functions.php

http://www.php.net/manual/en/function.ucwords.php

http://www.php.net/manual/en/function.ucwords.php

Q: How to replace a string to another string?

Answer: str_replace() is used to replace a string to the desired string.
Ex. str_replace("string to search","string to replace with","string to be subjected").


http://www.w3schools.com/PHP/php_ref_string.asp

http://www.tizag.com/phpT/strings.php

http://www.mycomputerforum.com/tutorials/php_strings.html

Q: What are trim(), ltrim() and rtrim()?

Answer:
- trim() removes space from the given string.
- ltrim() removes space from the left side of the sting.
- rtrim() removes space from the right side of the sting

http://www.tizag.com/phpT/strings.php

http://www.plus2net.com/php_tutorial/php_string_functions.php



Contributed by :Saritha G Pillai

Q. What is the difference between strstr and stristr?


Answer:  - strstr() returns part of a given string from the first occurrence of a given substring to the end of the string.
Example: strstr(”This is my example”,”my”) will return “my example”.
- stristr() is similar to strstr() except that it is case insensitive.
Example: strstr(”This is my example”, ”My”) will return “my example”.



http://www.w3schools.com/PHP/php_ref_string.asp


http://www.mycomputerforum.com/tutorials/php_strings.html


http://www.plus2net.com/php_tutorial/php_string_functions.php



Q. What are strtolower() and strtoupper()?

Answer:  strtolower() converts the given string to lower case characters.
Example: strtolower(”MY Example”) will return “my example”.
- strtoupper() converts the given string to upper case characters.
Example: strstr(” my example”) will return “MY EXAMPLE”.



http://www.w3schools.com/PHP/php_ref_string.asp


http://www.tizag.com/phpT/strings.php


http://www.mycomputerforum.com/tutorials/php_strings.html

Q. How to replace a stirng to another string?


Answer:  str_replace() is used to replace a string to the desired string.
Ex. str_replace("string to search","string to replace with","string to be subjected").

http://www.tizag.com/phpT/strings.php


http://www.w3schools.com/php/func_string_str_replace.asp


http://www.mycomputerforum.com/tutorials/php_strings.html


Q.  What is strpos()?

Answer: strpos() is used to find the position of a string in the given string.



http://www.w3schools.com/PHP/php_ref_string.asp


http://www.tizag.com/phpT/strings.php


http://www.mycomputerforum.com/tutorials/php_strings.html

Q. For Printing Out Strings, There Are Echo, Print And Printf. Explain The Differences.

Answer: echo is the most primitive of them, and just outputs the contents following the construct to the screen. print is also a construct (so parentheses are optional when calling it), but it returns TRUE on successful output and FALSE if it was unable to print out the string. However, you can pass multiple parameters to echo, like:

<?php echo ‘Welcome ‘, ‘to’, ‘ ‘, ‘fyicenter!’; ?>

and it will output the string “Welcome to fyicenter!” print does not take multiple parameters. It is also generally argued that echo is faster, but usually the speed advantage is negligible, and might not be there for future versions of PHP. printf is a function, not a construct, and allows such advantages as formatted output, but it’s the slowest way to print out data out of echo, print and printf.

http://www.plus2net.com/php_tutorial/php_string_functions.php


http://www.php.net/manual/en/function.echo.php

Q. How Can We Extract String “Abc.Com” From A String "Mailto:info@Abc.Com?Subject=Feedback” Using Regular Expression Of PHP?

Answer:  $text = “mailto:info@abc.com?subject=Feedback”;
preg_match(‘|.*@([^?]*)|’, $text, $output);
echo $output[1];

http://www.tizag.com/phpT/strings.php


http://www.mycomputerforum.com/tutorials/php_strings.html


http://www.plus2net.com/php_tutorial/php_string_functions.php

Q. What’s The Difference Between Htmlentities() And Htmlspecialchars()?

Answer:  htmlspecialchars only takes care of <, >, single quote ‘, double quote ” and ampersand. htmlentities translates all occurrences of character sequences that have different meaning in HTML.

http://www.w3schools.com/PHP/php_ref_string.asp


http://www.tizag.com/phpT/strings.php


http://www.php.net/manual/en/function.htmlentities.php

Q.Why doesn’t the following code print the newline properly?

Answer: <?php $str = ‘Hello, there.\nHow are you?\nThanks for visiting techpreparation’; print $str; ?>

Because inside the single quotes the \n character is not interpreted as newline, just as a sequence of two characters – \ and n.

http://www.tizag.com/phpT/strings.php


http://php.net/manual/en/language.types.string.php


Q. Would you initialize your strings with single quotes or double quotes?



Answer:  Since the data inside the single-quoted string is not parsed for variable substitution, it’s always a better idea speed-wise to initialize a string with single quotes, unless you specifically need variable substitution.

http://www.w3schools.com/PHP/php_string.asp

http://www.tizag.com/phpT/strings.php

http://php.net/manual/en/language.types.string.php

Q. How can we extract string ‘abc.com ‘ from a string http://info@abc.com using regular expression of php?

Answer: We can use the preg_match() function with “/.*@(.*)$/” as

the regular expression pattern. For example:

preg_match(“/.*@(.*)$/”,”http://info@abc.com”,$data);

echo $data[1];

http://www.tizag.com/phpT/strings.php


http://www.plus2net.com/php_tutorial/php_string_functions.php

Q.Would you initialize your strings with single quotes or double quotes?

Answer: Single quote strings are executed faster than double quotes When we use single quote for string then php will not parse the things between that quote. It simply assign as it is.But when we use double quotes then it will parse for variables and other things between double quotes.

http://www.tizag.com/phpT/strings.php


http://php.net/manual/en/language.types.string.php

Q.Will comparison of string "10" and integer 11 work in PHP?

Answer:  Yes, internally PHP will cast everything to the integer type, so numbers 10 and 11 will be compared.

http://www.tizag.com/phpT/strings.php


http://php.net/manual/en/language.types.string.php


Q. With a heredoc syntax, do I get variable substitution inside the heredoc contents?

Answer: Yes.

http://www.w3schools.com/PHP/php_string.asp


http://php.net/manual/en/language.types.string.ph

For the latest IT jobs visit  www.ipsrjobs.com 

No comments:

Post a Comment