PHP Most Important practice Questions and Answers
Ketan Raval
Chief Technology Officer (CTO) Teleview Electronics | Expert in Software & Systems Design & RPA | Business Intelligence | AI | Reverse Engineering | IOT | Ex. S.P.P.W.D Trainer
Php Most important questions and answers.
The PHP Hypertext pre-proecssor is a programming language that allows web developers to create dynamic content that interests with databases. PHP is basically used for developing web based software applications.
Question 2. What Is A Session?
Answer:
A session is a logical object created by the PHP engine to allow you to preserve data across subsequent HTTP requests.
There is only one session object available to your PHP scripts at any time. Data saved to the session by a script can be retrieved by the same script or another script when requested from the same visitor.
Sessions are commonly used to store temporary data to allow multiple PHP pages to offer a complete functional transaction for the same visitor.
Question 3. What Is Meant By Pear In Php?
Answer:
PEAR is the next revolution in PHP. This repository is bringing higher level programming to PHP. PEAR is a framework and distribution system for reusable PHP components. It eases installation by bringing an automated wizard, and packing the strength and experience of PHP users into a nicely organised OOP library. PEAR also provides a command-line interface that can be used to automatically install “packages”.
Question 4. How Can We Know The Number Of Days Between Two Given Dates Using Php?
Answer:
Simple arithmetic:
$date1 = date(‘Y-m-d’);
$date2 = ‘2006–07–01’;
$days = (strtotime() — strtotime()) / (60 * 60 * 24);
echo “Number of days since ‘2006–07–01’: $days”;
Question 5. How Can We Repair A Mysql Table?
Answer:
The syntex for repairing a mysql table is:
REPAIR TABLE tablename
REPAIR TABLE tablename QUICK
REPAIR TABLE tablename EXTENDED
This command will repair the table specified.
If QUICK is given, MySQL will do a repair of only the index tree.
If EXTENDED is given, it will create index row by row.
Question 6. What Is The Difference Between $message And $$message?
Answer:
$message is a simple variable whereas $$message is a variable’s variable,which means
value of the variable. Example:
$user = ‘bob’
is equivalent to
$message = ‘user’;
$$message = ‘bob’;
Question 7. What Is A Persistent Cookie?
Answer:
A persistent cookie is a cookie which is stored in a cookie file permanently on the browser’s computer. By default, cookies are created as temporary cookies which stored only in the browser’s memory. When the browser is closed, temporary cookies will be erased. You should decide when to use temporary cookies and when to use persistent cookies based on their differences:
· Temporary cookies can not be used for tracking long-term information.
· Persistent cookies can be used for tracking long-term information.
· Temporary cookies are safer because no programs other than the browser can access them.
· Persistent cookies are less secure because users can open cookie files see the cookie values.
Question 8. How Do You Define A Constant?
Answer:
Via define() directive, like define (“MYCONSTANT”, 100);
Question 9. What Are The Differences Between Require And Include, Include_once?
Answer:
require_once() and include_once() are both the functions to include and evaluate the specified file only once. If the specified file is included previous to the present call occurrence, it will not be done again.
But require() and include() will do it as many times they are asked to do.
Question 10. What Is Meant By Urlencode And Urldecode?
Answer:
urlencode() returns the URL encoded version of the given string. URL coding converts special characters into % signs followed by two hex digits. For example: urlencode(“10.00%”) will return “10%2E00%25”. URL encoded strings are safe to be used as part of URLs.
urldecode() returns the URL decoded version of the given string.
Question 11. How To Get The Uploaded File Information In The Receiving Script?
Answer:
Once the Web server received the uploaded file, it will call the PHP script specified in the form action attribute to process them. This receiving PHP script can get the uploaded file information through the predefined array called $_FILES. Uploaded file information is organized in $_FILES as a two-dimensional array as:
$_FILES[$fieldName][‘name’] — The Original file name on the browser system.
$_FILES[$fieldName][‘type’] — The file type determined by the browser.
$_FILES[$fieldName][‘size’] — The Number of bytes of the file content.
$_FILES[$fieldName][‘tmp_name’] — The temporary filename of the file in which
the uploaded file was stored on the server.
$_FILES[$fieldName][‘error’] — The error code associated with this file upload.
The $fieldName is the name used in the .
Question 12. What Is The Difference Between Mysql_fetch_object And Mysql_fetch_array?
Answer:
MySQL fetch object will collect first single matching record where mysql_fetch_array will collect all matching records from the table in an array.
Question 13. How Can I Execute A Php Script Using Command Line?
Answer:
Just run the PHP CLI (Command Line Interface) program and provide the PHP script file name as the command line argument. For example, “php myScript.php”, assuming “php” is the command to invoke the CLI program.
Be aware that if your PHP script was written for the Web CGI interface, it may not execute properly in command line environment.
Question 14. I Am Trying To Assign A Variable The Value Of 0123, But It Keeps Coming Up With A Different Number, What’s The Problem?
Answer:
PHP Interpreter treats numbers beginning with 0 as octal. Look at the similar PHP interview questions for more numeric problems.
Question 15. Would I Use Print “$a Dollars” Or “{$a} Dollars” To Print Out The Amount Of Dollars In This Example?
Answer:
In this example it wouldn’t matter, since the variable is all by itself, but if you were to print something like “{$a},000,000 mln dollars”, then you definitely need to use the braces.
Question 16. What Are The Different Tables Present In Mysql? Which Type Of Table Is Generated When We Are Creating A able In The Following Syntax: Create Table Employee(eno Int(2),ename Varchar(10))?
Answer:
Total 5 types of tables we can create
1. MyISAM
2. Heap
3. Merge
4. INNO DB
5. ISAM
MyISAM is the default storage engine as of MySQL 3.23. When you fire the above
create query MySQL will create a MyISAM table.
Question 17. How To Create A Table?
Answer:
If you want to create a table, you can run the CREATE TABLE statement as shown in the following sample script:
<?php
include “mysql_connection.php”;
$sql = “CREATE TABLE fyi_links (“
. “ id INTEGER NOT NULL”
. “, url VARCHAR(80) NOT NULL”
. “, notes VARCHAR(1024)”
. “, counts INTEGER”
. “, time TIMESTAMP DEFAULT sysdate()”
. “)”;
if (mysql_query($sql, $con)) {
print(“Table fyi_links created.n”);
} else {
print(“Table creation failed.n”);
}
mysql_close($con);
?>
Remember that mysql_query() returns TRUE/FALSE on CREATE statements. If you run this script, you will get something like this:
Table fyi_links created.
Question 18. How Can We Encrypt The Username And Password Using Php?
Answer:
You can encrypt a password with the following Mysql>SET
PASSWORD=PASSWORD(“Password”);
Question 19. How Do You Pass A Variable By Value?
Answer:
Just like in C++, put an ampersand in front of it, like $a = &$b.
Question 20. What Is The Functionality Of The Functions Strstr() And Stristr()?
Answer: string strstr ( string haystack, string needle ) returns part of haystack string from the first occurrence of needle to the end of haystack. This function is case-sensitive.
stristr() is idential to strstr() except that it is case insensitive.
Question 21. When Are You Supposed To Use Endif To End The Conditional Statement?
Answer:
When the original if was followed by : and then the code block without braces.
领英推荐
Question 22. How Can We Send Mail Using Javascript?
Answer:
No. There is no way to send emails directly using JavaScript.
But you can use JavaScript to execute a client side email program send the email using the “mailto” code. Here is an example:
function myfunction(form)
{
tdata=document.myform.tbox1.value;
location=”mailto:[email protected]?subject=…”;
return true;
}
Question 23. What Is The Functionality Of The Function 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. For example: strstr(“[email protected]”,”@”) will return “@example.com”.
stristr() is idential to strstr() except that it is case insensitive.
Question 24. What Is The Difference Between Ereg_replace() And Eregi_replace()?
Answer:
eregi_replace() function is identical to ereg_replace() except that it ignores case distinction when matching alphabetic characters.
Question 25. How Do I Find Out The Number Of Parameters Passed Into Function9?
Answer:
func_num_args() function returns the number of parameters passed in.
Question 26. What Is The Purpose Of The Following Files Having Extensions: Frm, Myd, And Myi? What These Files Contain?
Answer:
In MySQL, the default table type is MyISAM.
Each MyISAM table is stored on disk in three files. The files have names that begin with
the table name and have an extension to indicate the file type.
The ‘.frm’ file stores the table definition.
The data file has a ‘.MYD’ (MYData) extension. The index file has a ‘.MYI’ (MYIndex) extension.
Question 27. If The Variable $a Is Equal To 5 And Variable $b Is Equal To Character A, What’s The Value Of $$b?
Answer:
5, it’s a reference to existing variable.
Question 28. How To Protect Special Characters In Query String?
Answer:
If you want to include special characters like spaces in the query string, you need to protect them by applying the urlencode() translation function. The script below shows how to use urlencode():
<?php
print(“<html>”);
print(“<p>Please click the links below”
.” to submit comments about FYICenter.com:</p>”);
$comment = ‘I want to say: “It\’s a good site! :->”’;
$comment = urlencode($comment);
print(“<p>”
.”<a href=\”processing_forms.php?name=Guest&comment=$comment\”>”
.”It’s an excellent site!</a></p>”);
$comment = ‘This visitor said: “It\’s an average site! :-(“‘;
$comment = urlencode($comment);
print(“<p>”
.’<a href=”processing_forms.php?’.$comment.’”>’
.”It’s an average site.</a></p>”);
print(“</html>”);
?>
Question 29. Are Objects Passed By Value Or By Reference?
Answer:
Everything is passed by value.
Question 30. What Are The Differences Between Drop A Table And Truncate A Table?
Answer:
DROP TABLE table_name?— This will delete the table and its data.
TRUNCATE TABLE table_name?— This will delete the data of the table, but not the table definition.
Question 31. What Are The Differences Between Get And Post Methods In Form Submitting, Give The Case Where We Can Use Get And We Can Use Post Methods?
Answer:
When you want to send short or small data, not containing ASCII characters, then you can use GET” Method. But for long data sending, say more then 100 character you can use POST method.
Once most important difference is when you are sending the form with GET method. You can see the output which you are sending in the address bar. Whereas if you send the form with POST” method then user can not see that information.
Question 32. How Do You Call A Constructor For A Parent Class?
Answer:
parent::constructor($value).
Question 33. What Are The Different Types Of Errors In Php?
Answer:
Here are three basic types of runtime errors in PHP:
Notices:?These are trivial, non-critical errors that PHP encounters while executing a script — for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all — although you can change this default behavior.
Warnings:?These are more serious errors — for example, attempting to include() a file which does not exist. By default, these errors are displayed to the user, but they do not result in script termination.
Fatal errors:?These are critical errors — for example, instantiating an object of a nonexistent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP’s default behavior is to display them to the user when they take place.
Internally, these variations are represented by twelve different error types.
Question 34. What’s The Special Meaning Of __sleep And __wakeup?
Answer:
__sleep returns the array of all the variables than need to be saved, while __wakeup retrieves them.
Question 35. How Can We Submit A Form Without A Submit Button?
Answer:
If you don’t want to use the Submit button to submit a form, you can use normal hyper links to submit a form. But you need to use some JavaScript code in the URL of the link. For example:
<a href=”javascript: document.myform.submit();”>Submit Me</a>.
Question 36. 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.
Question 37. What Is The Difference Between The Functions Unlink And Unset?
Answer:
unlink() is a function for file system handling. It will simply delete the file in context.
unset() is a function for variable management. It will make a variable undefined.
Question 38. How Come The Code Works, But Doesn’t For Two-dimensional Array Of Mine?
Answer:
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 39. How Can We Register The Variables Into A Session?
Answer:
session_register($session_var);
$_SESSION[‘var’] = ‘value’;
Question 40. What Is The Difference Between Characters \023 And \x23?
Answer:
The first one is octal 23, the second is hex 23.
Question 41. How Can We Submit Form Without A Submit Button?
Answer:
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: <input type=button value=”Save” onClick=”document.form.submit()”>.
Question 42. How Can We Create A Database Using Php And Mysql?
Answer:
We can create MySQL database with the use of mysql_create_db($databaseName) to create a database.
Question 43. How Many Ways We Can Retrieve The Date In Result Set Of Mysql Using Php?
Answer:
As individual objects so single record or as a set or arrays.
Question 44. How Many Ways Can We Get The Value Of Current Session Id?
Answer:
session_id() returns the session id for the current session.
Question 45. Can We Use Include (“abc.php”) Two Times In A Php Page “makeit.php”?
Answer:
Yes.
Question 46. What’s The Difference Between Include And Require?
Answer:
It’s how they handle failures. If the file is not found by require(), it will cause a fatal error and halt the execution of the script. If the file is not found by include(), a warning will be issued, but execution will continue.
Question 47. Explain The Ternary Conditional Operator In Php?
Answer:
Expression preceding the ? is evaluated, if it’s true, then the expression preceding the : …
THIS QUESTION AND ANSWERS ARE TAKEN FROM THE BOOK