This article will give you an overview of PHP basic and intermediate level 20 questions and answers that may be helpful for you in an interview or online test.
Question: What is the difference between $var and $$var?
They are both variables. But $var is a variable with a fixed name. $$var is a variable of variables.
Question: What MD5 encryption techniques in PHP
md5 PHP implements the md5hash algorithm using the md5 function,
Example: $encrypted_text = md5 ('your-message-to-encrypt);
md5 is a hashing technique. You cannot decrypt it back.Hashing means, once you are converted it to a encrypted code, you cannot go back! But you can still compare the md5 encrypted value with the another md5 encrypted value to check matches (mostly in the case of password verification and all!)
Question: What is the use of the function htmlentities()?
htmlentities() Convert all applicable characters to HTML entities This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.
Question: How to delete a file from the system
unlink() deletes the given file from the file system.
Question: What are the different types of errors in PHP ?
1. Parse Error (Syntax Error)
2. Fatal Error
3. Warning Error
4. Notice Error
1. Parse Errors (syntax errors)
The parse error occurs if there is a syntax mistake in the script; the output is Parse errors.
Common reason of syntax errors are:
- Unclosed quotes
- Missing or Extra parentheses
- Unclosed braces
- Missing semicolon
2. Fatal Errors
Fatal errors are caused when PHP understands what you've written, however what you're asking it to do can't be done. Fatal errors stop the execution of the script. If you are trying to access the undefined functions, then the output is a fatal error.
echo "PHP Code Hub";
3. Warning Errors
Warning errors will not stop execution of the script. The main reason for warning errors are to include a missing file or using the incorrect number of parameters in a function.
include ("user.php"); (if user.php is missing)
4. Notice Errors
Notice that an error is the same as a warning error i.e. in the notice error execution of the script does not stop. Notice that the error occurs when you try to access the undefined variable, then produce a notice error.
$name ="PHP Code Hub";
Question: What is sql injection ?
SQL injection is a code injection technique, used to attack data-driven applications, in which nefarious SQL statements are inserted into an entry field for execution.
Question: How many open modes available when a file open in PHP?
- r : Open a file for read only. File pointer starts at the beginning of the file.
- w : Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file.
- a : Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist.
- x : Creates a new file for write only. Returns FALSE and an error if file already exists.
- r+ : Open a file for read/write. File pointer starts at the beginning of the file.
- w+ : Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file.
- a+ : Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist.
- x+ : Creates a new file for read/write. Returns FALSE and an error if file already exists.
Question: How to find the position of the first occurrence of a substring in a string?
strpos() is used to find the position of the first occurrence of a substring in a string.
Question: Rules for PHP variables.
A variable starts with the $ sign, followed by the name of the variable.
A variable name must start with a letter or the underscore character.
A variable name cannot start with a number.
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
Question: What are the different types of PHP variables?
Integers − are whole numbers, without a decimal point, like 4195.
Doubles − are floating-point numbers, like 3.14159 or 49.1.
Booleans − have only two possible values either true or false.
NULL − is a special type that only has one value: NULL.
Strings − are sequences of characters, like 'PHP supports string operations.'
Arrays − are named and indexed collections of other values.
Objects − are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.
Resources − are special variables that hold references to resources external to PHP (such as database connections).
Question: What are PHP magic constants?
__LINE__ : The current line number of the file.
__FILE__ : The full path and filename of the file (absolute path).
__DIR__ : The directory of the file. If used inside an include, the directory of the included file is returned.
__FUNCTION__ : The function name (This constant returns the function name as it was declared (case-sensitive)).
__CLASS__ : The class name. (this constant returns the class name as it was declared (case-sensitive)).
__METHOD__ : The class method name. (The method name is returned as it was declared (case-sensitive)).
__TRAIT__ : The trait name.
__NAMESPACE__ : The name of the current namespace.
Question : What is the purpse $_PHP_SELF variable?
The PHP default variable $_PHP_SELF is used for the PHP script name and when you click "submit" button then same PHP script will be called.
Question: What is the difference between single quoted string and double quoted string?
Singly quoted strings are treated almost literally, whereas doubly quoted strings replace variables with their values as well as specially interpreting certain character sequences.
Question: How will you get the size of a file in php?
The files's length can be found using the filesize() function which takes the file name as its argument and returns the size of the file expressed in bytes.
Question: How will you check if a file exists or not using php?
File's existence can be confirmed using file_exist() function.
Question: What are the php super global variables?
Several predefined variables in PHP are "superglobals", which means they are available in all scopes throughout a script. There is no need to do global $variable; to access them within functions or methods.
These superglobal variables are:
Question: What is the purpose of getdate() function?
The function getdate() optionally accepts a time stamp and returns an associative array containing information about the date.
Output will be:
[seconds] => 36
[minutes] => 52
[hours] => 10
[mday] => 9
[wday] => 6
[mon] => 7
[year] => 2016
[yday] => 190
[weekday] => Saturday
[month] => July
 => 1468054356
Question: Case sensitive and case-insensitive in PHP
1. PHP is partially case sensitive.
2. Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.
3. Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration.
4. PHP class names are case-insensitive.
$obj = new MyClass;
$obj = new MYCLASS;