PHP Interview Questions and Answers
Question - 41 : - How come the code works, but doesn’t for two-dimensional array of mine?
Answer - 41 : - Any time you have an array with more than one dimension, complex parsing syntax is required. print "Contents: {$arr[1][2]}" would’ve worked.
Question - 42 : - How can we register the variables into a session?
Answer - 42 : - session_register($session_var);
$_SESSION['var'] = 'value';
Question - 43 : - What is the difference between characters \023 and \x23?
Answer - 43 : - The first one is octal 23, the second is hex 23.
With a heredoc syntax, do I get variable substitution inside the heredoc contents?
Yes.
Question - 44 : - How can we submit form without a submit button?
Answer - 44 : - We can use a simple JavaScript code linked to an event trigger of any form field. In the JavaScript code, we can call the document.form.submit() function to submit the form. For example:
Question - 45 : - How can we create a database using PHP and mysql?
Answer - 45 : - We can create MySQL database with the use of mysql_create_db($databaseName) to create a database.
Question - 46 : - How many ways we can retrieve the date in result set of mysql using php?
Answer - 46 : - As individual objects so single record or as a set or arrays.
Question - 47 : - Can we use include ("abc.php") two times in a php page "makeit.php"?
Answer - 47 : - Yes.
Question - 48 : - what is the difference among echo, print and printf?
Answer - 48 : - 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:
and it will output the string "Welcome to techpreparations!" 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.
I am writing an application in PHP that outputs a printable version of driving directions. It contains some long sentences, and I am a neat freak, and would like to make sure that no line exceeds 50 characters. How do I accomplish that with PHP?
On large strings that need to be formatted according to some length specifications, use wordwrap() or chunk_split().
Question - 49 : - What’s the output of the ucwords function in this example?
Answer - 49 : - $formatted = ucwords("TECHPREPARATIONS IS COLLECTION OF INTERVIEW QUESTIONS");
print $formatted;
What will be printed is TECHPREPARATIONS IS COLLECTION OF INTERVIEW QUESTIONS.
ucwords() makes every first letter of every word capital, but it does not lower-case anything else. To avoid this, and get a properly formatted string, it’s worth using strtolower() first.
Question - 50 : - What’s the difference between htmlentities() and htmlspecialchars()?
Answer - 50 : - htmlspecialchars only takes care of <, >, single quote ‘, double quote " and ampersand. htmlentities translates all occurrences of character sequences that have different meaning in HTML.