PHP is a server-side scripting language, that is used to develop Static websites or Dynamic websites or Web applications. PHP stands for Hypertext Pre-processor, that earlier stood for Personal Home Pages.
PHP scripts can only be interpreted on a server that has PHP installed. The client computers accessing the PHP scripts require a web browser only. A PHP file contains PHP tags and ends with the extension “.php”.
PHP is an open-source server-side scripting language that many devs use for web development. Easy to learn: PHP is not hard to learn for absolute beginners. PHP has a very supportive online community.
Q2. Differentiate between variables and constants in PHP?
Variables
Constants
The value of a variable can be changed during the execution.
The constant value can’t be changed during script execution.
Variables require compulsory usage of the $ sign at the start.
No dollar sign ($) is required before using a constant.
It is possible to define a variable by simple assignment.
Constants can’t be defined by simple assignments. They are defined using the define() function.
The default scope is the current access scope.
Constants can be accessed throughout without any scoping rules.
Q3. What is a session in PHP?
PHP Engine creates a logical object to preserve data across subsequent HTTP requests, which is known as session.
Sessions generally store temporary data to allow multiple PHP pages to offer a complete functional transaction for the same user.
Simply, it maintains data of an user (browser).
Q4. What does PEAR stands for?
PEAR means “PHP Extension and Application Repository”. It extends PHP and provides a higher level of programming for web developers.
PEAR provides a higher level of programming for web developers. It contains all kinds of PHP code snippets and libraries. It also provides you with a command-line interface to automatically install packages.
Q5. What was the old name of PHP?
The old name of PHP was Personal Home Page.
Q6. Is PHP a case-sensitive language?
PHP can be considered as a partial case-sensitive language. The variable names are completely case-sensitive but function names are not. Also, user-defined functions are not case-sensitive but the rest of the language is case-sensitive.
Ex:
User Defined functions in PHP can be defined in lowercase but later referred to in uppercase, and it would still function normally.
Q7. Explain the difference between static and dynamic websites?
In static websites, content can't be changed after running the script. You can't change anything on the site. It is predefined.
In dynamic websites, content of script can be changed at the run time. Its content is regenerated every time a user visit or reload. Google, yahoo and every search engine is the example of dynamic website.
Q8. Explain the difference between PHP4 and PHP5?
PHP4 doesn't support oops concept and uses Zend Engine 1.
PHP5 supports oops concept and uses Zend Engine 2.
Q9. How do you execute a PHP script from the command line?
Just use the PHP command line interface (CLI) and specify the file name of the script to be executed.
php script.php
Q10. What are the popular Content Management Systems (CMS) in PHP?
WordPress
Joomla
Magento
Drupal
Q11. What are the different types of variables present in PHP?
There are 8 primary data types in PHP.
Integers: Integers are whole numbers without a floating-point. Ex: 1505.
Doubles: Doubles are floating-point numbers. Ex: 17.540
Booleans: It represents two logical states- true or false.
NULL: NULL is a special type that only has one value, NULL.
Arrays: Array is a named and ordered collection of similar type of data. Ex: $fruits = array("mango", "orange", "banana")
Strings: Strings are a sequence of characters. Ex: 'Hello World!'
Objects: An instance of classes containing data and functions. Ex: $orange = new Fruit();
Resources: Resources are special variables that consist of references to resources external to PHP Ex: database connections.
Q12. How can we display the output directly to the browser?
To be able to display the output directly to the browser, we have to use the special tags <?= and ?>.
Q13. What are the popular frameworks in PHP?
CakePHP
CodeIgniter
Yii 2
Symfony
Zend Framework
Q14. Is multiple inheritance supported in PHP?
PHP supports only single inheritance, it means that a class can be extended from only one single class using the keyword 'extended'.
Q15. What are the rules for naming a PHP variable?
A variable must start with a dollar symbol, followed by the variable name. Ex: $price = 200; where price is a variable name.
Variable names must begin with a letter or underscore.
A variable name can consist of letters, numbers, or underscores. But you cannot use characters like + , – , % , & etc.
A PHP variable name cannot contain spaces.
PHP variables are case-sensitive. So $NAME and $name both are treated as different variables
Q16. What is the difference between “echo” and “print” in PHP?
Echo
Print
echo can output one or more strings.
print can only output one string and it always returns 1.
echo is faster than print because it does not return any value.
print is slower compared to echo.
If you want to pass more than one parameter to echo, a parenthesis should be used.
Use of parenthesis is not required with the argument list.
Q17. What is the meaning of a final class and a final method?
'final' is introduced in PHP5. Final class means that this class cannot be extended and a final method cannot be overridden.
Q18. What is "echo" in PHP?
PHP echo output one or more string. It is a language construct not a function. So the use of parentheses is not required. But if you want to pass more than one parameter to echo, the use of parentheses is required.
Q19. What is "print" in PHP?
PHP print output a string. It is a language construct not a function. So the use of parentheses is not required with the argument list. Unlike echo, it always returns 1.
Q20. What is the purpose of @ in PHP?
In PHP, @ is used for suppressing error messages. If any runtime error occurs on the line which consists @ symbol at the beginning, then the error will be handled by PHP.
Q21. What is the difference between $message and $$message?
$message stores variable data while $$message is used to store variable of variables.
$message stores fixed data whereas the data stored in $$message may be changed dynamically.
Q22. How can PHP and HTML interact?
PHP is a server-side language whereas HTML is a client-side language. So PHP executes on the server-side and gets its results as strings, objects, arrays, and then we use them to display its values in HTML.
Q23. Explain the importance of Parser in PHP?
A PHP parser is software that converts source code into the code that computer can understand. This means whatever set of instructions we give in the form of PHP code is converted into a machine-readable format by the parser.
Q24. How can PHP and Javascript interact?
PHP and Javascript cannot directly interact since PHP is a server side language and Javascript is a client-side language.
However, we can exchange variables since PHP can generate Javascript code to be executed by the browser and it is possible to pass specific variables back to PHP via the URL.
Q25. What is the use of the function ‘imagetypes()’?
imagetypes() gives the image format and types supported by the current version of GD-PHP.
Q26. What are the ways to define a constant in PHP?
PHP constants are name or identifier that can't be changed during execution of the script. PHP constants are defined in two ways.
define()
const()
Q27. What are the different types of Array in PHP?
Indexed Array
An array with a numeric key is known as the indexed array. Values are stored and accessed in linear order.
Associative Array
An array with strings for indexing elements is known as the associative array. Element values are stored in association with key values rather than in strict linear index order.
Multidimensional Array
An array containing one or more arrays within itself is known as a multidimensional array. The values are accessed using multiple indices.
Q28. What are magic constants in PHP?
PHP magic constants are predefined constants, which change based on their use. They start with a double underscore (__) and end with a double underscore (__).
Q29. What are the functions to be used to get the image’s properties (size, width, and height)?
The functions are getimagesize() for size, imagesx() for width and imagesy() for height.
Q30. Explain the different types of errors in PHP?
The 4 main types of errors in PHP are:
Notices
Notices are non-critical errors that can occur during the execution of the script. These are not visible to users. Ex: Accessing an undefined variable.
Warnings
These are more critical than notices. Warnings don’t interrupt the script execution. By default, these are visible to the user. Ex: include() a file that doesn’t exist.
Fatal
This is the most critical error type which, when occurs, immediately terminates the execution of the script. Ex: Accessing a property of a non-existent object or require() a non-existent file
Parse Error (Syntax)
Parse errors are caused by misused or missing symbols in a syntax. The compiler catches the error and terminates the script. Ex: Unclosed brackets or quotes, Missing or extra semicolons or parentheses, Misspellings
Q31. What are traits?
Traits are a mechanism that lets you create reusable code in PHP and similar languages where multiple inheritances are not supported. It’s not possible to instantiate it on its own.
A trait is intended to reduce the limitations of single inheritance by enabling a developer to reuse sets of methods freely in many independent classes living in different hierarchies of class.
trait TraitName {
// some code...
}
Q32. What are the different loops in PHP?
for
while
do-while
for each
Q33. How failures in execution are handled with include() and require() functions?
If the function require() cannot access the file then it ends with a fatal error. However, the include() function gives a warning, and the PHP script continues to execute.
Q34. What is the main difference between require() and require_once()?
require(), and require_once() perform the same task except that the second function checks if the PHP script is already included or not before executing it.
Q35. What is the most used method for hashing passwords in PHP?
The crypt() function is used for this functionality as it provides a large number of hashing algorithms that can be used. These algorithms include sha1, sha256, or md5 which are designed to be very fast and efficient.
Q36. What is the use of count() function in PHP?
The PHP count() function is used to count total elements in the array, or something an object.
Q37. How is it possible to set an infinite execution time for PHP script?
The set_time_limit(0) added at the beginning of a script sets to infinite the time of execution to not have the PHP error ‘maximum execution time exceeded.’
It is also possible to specify this in the php.ini file.
Q38. What are cookies? How to create cookies in PHP?
A cookie is a small record that the server installs on the client’s computer. They store data about a user on the browser. It is used to identify a user and is embedded on the user’s computer when they request a particular page. Each time a similar PC asks for a page with a program, it will send the cookie as well.
After verifying the user’s identity in encrypted form, cookies maintain the session id generated at the back end. It must reside in the browser of the machine. You can store only string values not object because you cannot access any object across the website or web apps.
A cookie is created with the setcookie() function.
Note:The setcookie() function must appear BEFORE the <html> tag.
Q45. What is the use of session_start() and session_destroy() functions in PHP?
The session_start() function is used to start a new session. Also, it can resume an existing session if it is stopped. In this particular case, the return will be the current session if resumed.
The session_destroy() function is used to destroy all of the session variables.
session_start();
session_destroy();
Q46. What is the array in PHP?
An array is used to store multiple values in a single value. In PHP, it orders maps of pairs of keys and values. It saves the collection of the data type.
array("Mango", "Banana", "Orange", "Papaya");
Q47. What is the function mysql_pconnect() useful for?
mysql_pconnect() ensure a persistent connection to the database, it means that the connection does not close when the PHP script ends.
This function is not supported in PHP 7.0 and above
Q48. How be the result set of Mysql handled in PHP?
The result set can be handled using
mysqli_fetch_array,
mysqli_fetch_assoc,
mysqli_fetch_object,
mysqli_fetch_row
Q49. Explain some of the PHP array functions?
There are many array functions in PHP
array()
array_change_key_case()
array_reverse()
array_keys()
count()
array_search()
array_map()
array_merge()
sort()
array_chunk()
array_filter()
array_intersect()
Q50. What is PDO in PHP?
PDO stands for PHP Data Object. PDO is a set of PHP extensions that provide a core PDO class and database, specific drivers.
The PDO extension can access any database which is written for the PDO driver. There are several PDO drivers available which are used for FreeTDS, Microsoft SQL Server, IBM DB2, Sybase, Oracle Call Interface, Firebird/Interbase 6 and PostgreSQL databases etc.
Earlier versions of PHP used the MySQL extension. However, this extension was deprecated in 2012.
GET method is used for requesting data from a specified resource.
POST is used for sending the data to the server as a package in a separate communication with the processing script.
GET method cannot be used for sending binary data like images or word documents
The POST method can be used to send ASCII as well as binary data like images and word documents
Data is sent in the form of URL parameters which are strings of name-value pairs separated by ampersands(&)
Data sent through the POST method will not be seen in the URL
This method must not be used if you have any sensitive information like a password to be sent to the server.
Sensitive information can be sent using this method.
You can use this method only for data that is not secure.
Data sent through this method is secure.
It can be used for submitting the form where the user can bookmark the result.
Submissions by form with POST cannot be bookmarked.
GET method is not safer since parameters may be stored in web server logs or browser history.
POST method is safer than GET because the parameters are not stored in web server logs or browser history.
Q52. How is it possible to know the number of rows returned in the result set?
The function mysqli_num_rows() returns the number of rows in a result set.
Q53. How to get the length of string?
The strlen() function is used to get the length of the string.
Q54. Explain type hinting in PHP?
In PHP, type hinting is used to specify the expected data type (arrays, objects, interface, etc.) for an argument in a function declaration. It was introduced in PHP 5.
Whenever the function is called, PHP checks if the arguments are of a user-preferred type or not. If the argument is not of the specified type, the run time will display an error and the program will not execute.
You can check other features below.
makes you code more stable, especially if the project is large
the code is more readable, cleaner and professional
gives you autocomplete in your IDE
you can more easily understand the code and how methods are working when you come to a new project
function sendEmail (Email $email) {
$email->send();
}
Q55. Explain some of the PHP string functions?
There are many array functions in PHP
strtolower()
strrev()
strlen()
lcfirst()
ucfirst()
ucwords()
strtoupper()
Q56. What is the difference between mysqli_fetch_object() and mysqli_fetch_array()?
The mysqli_fetch_object() function collects the first single matching record where mysqli_fetch_array() collects all matching records from the table in an array.
Q57. How to terminate the execution of a script in PHP?
To terminate the execution of the script in PHP, the exit() function is used. It is a built-in function that outputs a message and then terminates the current script.
$site = "https://programmingpath.in";
fopen($site,"r")
or exit("Unable to connect to $site");
Q58. How can you submit a form without a submit button?
You can use JavaScript submit() function to submit the form without explicitly clicking any submit button.
Q59. How can we access the data sent through the URL with the GET method?
To access the data sent via the GET method, we use $_GET
https://programmingpath.in?language=php
echo $_GET["language"]; //this will return 'php'
Q60. How can we access the data sent through the URL with the POST method?
To access the data sent this way, you use the $_POST array.
$_POST["var"];
Q61. What are the ways to include file in PHP?
include
require
Q62. How can we check the value of a given variable is a number?
It is possible to use the dedicated function, is_numeric() to check whether it is a number or not.
Q63. How do I check if a given variable is empty?
If we want to check whether a variable has a value or not, it is possible to use the empty() function.
Q64. What does the unlink() function mean?
The unlink() function simply deletes the file given as entry.
Q65. What does the unset() function mean?
The unset() function a variable undefined.
Q66. What is the method to register a variable into a session?
Session_register($ur_session_var);
Q67. Write syntax to open a file in PHP?
resource fopen( string $filename )
Q68. How is it possible to remove escape characters from a string?
The stripslashes function remove the escape characters before apostrophes in a string.
Q69. How do I escape data before storing it in the database?
The addslashes function escape data before storage into the database.
Q70. Is it possible to remove the HTML tags from data?
The strip_tags() function clean a string from the HTML tags.
Q71. How can we define a variable accessible in functions of a PHP script?
Using the global keyword.
Q72. How to write in a file in PHP?
PHP fwrite() and fputs() functions are used to write data into file.
Q73. How to delete file in PHP?
The unlink() function is used to delete a file in PHP.
Q74. How is it possible to return a value from a function?
A function returns a value using the instruction ‘return $val;’.
Q75. How to upload file in PHP?
The move_uploaded_file() function is used to upload file in PHP.
Q76. How can you pass a variable by reference?
We use an ampersand in front of it, as follows $var1 = &$var2
Q77. How is the ternary conditional operator used in PHP?
Expression_1 ? Expression_2 : Expression_3;
Q78. How to download file in PHP?
You can download file using readfile() function
int readfile ( string $filename )
Q79. How can you send email in PHP?
mail() function is used to send email in PHP.
bool mail($to, $subject, $message, $header);
Q80. What is the function func_num_args() used for?
func_num_args() is used to give the number of parameters passed into a function.
function foo() {
$numargs = func_num_args();
echo "Number of arguments: $numargs";
}
foo(1, 2, 3); //Number of arguments: 3
Q81. What is htaccess in PHP?
The .htaccess is a configuration file on Apache server. You can change configuration settings using directives in Apache configuration files like .htaccess and httpd.conf.
Q82. Explain PHP explode() function?
The PHP explode() function breaks a string into an array.
explode(separator, string, limit)
Q83. Explain PHP split() function?
split() function splits string into an array by regular expression.
$ip = "123.456.789.100"; // some IP address
$iparr = split ("\.", $ip);
print $iparr[0]; //123
Q84. If the variable $var1 is set to 10 and the $var2 is set to the character var1, what’s the value of $$var2?
$$var2 contains the value 10.
Q85. How can we get IP address of a client in PHP?
$_SERVER["REMOTE_ADDR"];
Q86. What is the meaning of a Persistent Cookie?
A persistent cookie is permanently stored in a cookie file on the browser's computer.
By default, cookies are temporary and are erased if we close the browser.
Q87. What does accessing a class via :: means?
:: is used to access static methods that do not require object initialization.
Q88. In PHP, objects are they passed by value or by reference?
Objects are passed by reference.
Q89. What are the data types in PHP?
Integer
Boolean
Float
String
Array
Object
Callable
Null
Resource
Q90. What’s the difference between __sleep and __wakeup?
__sleep returns the array of all the variables that need to be saved, while __wakeup retrieves them.
$variable3 will contain “Hello World”. The first code is faster than the second code especially for large large sets of data.
Q92. How to concatenate two strings in PHP?
To concatenate two string variables together, we use the dot (.) operator.
Q93. What is the difference between session_unregister() and session_unset()?
The session_unregister() function unregister a global variable from the current session and the session_unset() function frees all session variables.
Q94. What does $GLOBALS mean?
$GLOBALS is associative array including references to all variables which are currently defined in the global scope of the script.
Q95. What is a lambda function in PHP?
A lambda function is an anonymous PHP function that can be stored in a variable and passed as an argument to other functions or methods. A closure is a lambda function that is aware of its surrounding context
Q96. What does $_SERVER mean?
$_SERVER is an array including information created by the web server such as paths, headers, and script locations.
Q97. What does $_FILES means?
$_FILES is an associative array composed of items sent to the current script via the HTTP POST method.
Q98. How can we get the error when there is a problem to upload a file?
$_FILES[‘userfile’][‘error’] contains the error code associated with the uploaded file.
Q99. How can we change the maximum size of the files to be uploaded?
We can change the maximum size of files to be uploaded by changing upload_max_filesize in php.ini.
Q100. What is the difference between Exception::getMessage and Exception:: getLine?
Exception::getMessage lets us getting the Exception message and Exception::getLine lets us getting the line in which the exception occurred.
Q101. How can we determine whether a variable is set?
The boolean function isset determines if a variable is set and is not NULL.
Cookie Policy
This website uses cookie or similar technologies, to enhance your browsing experience and provide personalised recommendations. By continuing to use our website, you agree to our Cookie Policy.