Basic PHP questions
What is the difference between static and dynamic websites?
In static websites, we cannot change the content, which is predefined and will not be affected by running any script. On the other hand, in dynamic websites content can be changed at the run time, since it is regenerated each time a user visits or reload a page.
Multiple job opportunities.
One code challenge.
Get rid of repetitive hiring processes for all the positions you apply to, and access many job offers by taking a single real-world assessment.
What is PHP, if compared to HTML?
HTML is not a programming language per se, but a language used to describe to a browser how to display text and other objects. Also, HTML works on the client computer where the page is viewed, not on the server that provides it.
On the other hand, PHP is a scripting language executed on the server that can be used (among other things) to create web pages written in HTML.
What does PEAR stand for in PHP?
PEAR is the acronym for “PHP Extension and Application Repository”. Then, it is a framework and repository for reusable PHP components that provides a higher level of programming for web developers. Basically, it contains all types of PHP code snippets and libraries that can be installed automatically using a command-line interface.
From the command line, how do you execute a PHP script?
We use the PHP command-line interface (CLI), specifying the script file name to be executed:
Does PHP support multiple inheritances?
No. PHP supports only single inheritance, so a class can be extended from only one single class. We must use the keyword ‘extended’ to do so.
What is final class and final method in PHP?
The ‘final’ attribute was been introduced in PHP5: final class means that this class cannot be extended, while final method implies that the method cannot be overridden.
How can we display text with a PHP script?
We can do that using two methods: echo and print:
<!--?php echo "Method 1"; print "Method 2"; ?-->
Explain what is "echo" in PHP
In PHP, echo output one or more strings. Since it is a language construct and not a function, the use of parentheses is not required. Nevertheless, if we want to pass more than one parameter to echo, we should use parentheses.
void echo ( string $arg1 [, string $... ] )
Explain the difference between "echo" and "print" in PHP
Echo outputs one or more strings, while print can only output one string and always returns one. Therefore, echo is faster since it does not return any value.
Do PHP and HTML interact?
Yes. We can generate HTML using PHP scripts, and also pass information from HTML to PHP.
Explain the difference between $message and $$message
While $message stores variable data and that data is fixed, $$message is used to store a variable of variables. Therefore, the data stored in $$message can be changed dynamically.
How can we define a constant in PHP?
In PHP, constants are a fixed name or identifiers that will not be changed during the execution of the script. They can be defined in two ways, using the define() or the const() function.
How many data types are there in PHP?
Data types are used in PHP to contain data or values. Categorized in 3 main types, PHP supports 8 primitive data types:
Scalar types (predefined): Boolean, integer, float, and string
Compound types (user-defined): Array and object
Special types: Resource and NULL
Can we set an infinite execution time for a PHP script? Why?
Normally, the maximum execution time for a PHP script is set to 30 seconds. If it takes longer than that, PHP will terminate the script and return an error.
However, when set_time_limit(0) is added at the beginning of a script, we can avoid the PHP error ‘maximum execution time exceeded’, since we set to infinite the time of execution. We can also change globally the maximum execution time, editing the max_execution_time directive in the php.ini file.
Explain the use of the header() function in PHP
The header() function sends a raw HTTP header to a client. We are not able to print any HTML element before using this function, so it should be called before sending the actual output.
Advanced PHP interview questions
Explain what is a lambda function in PHP
A lambda function is an anonymous function stored in a variable and given to another function or method as an argument. For instance, a closure is a lambda function aware of its context.
What is overriding?
In PHP, overloading is defining functions with similar signatures, but different parameters. Then, overriding is only pertinent to derived classes. In those, the parent class has defined a method but the derived class needs to override it. Therefore, we can only overload methods using the magic method __call in PHP.
What are “magic methods” in PHP?
In PHP, magic methods are tricks and hacks in object-oriented programming. These methods are identified by a two underscore prefix (__) and act as interceptors, automatically called if certain conditions are met.
Examples of magic methods in PHP are __construct(), __destruct(), __get(), __set(), __isset(), __unset(), __toString(), __set_state(),__clone(), __sleep(), __wakeup(), __call(),__callStatic(), __invoke(), or __autoload()
Describe what is encapsulation in PHP
Encapsulation is an Object-Oriented Programming concept in PHP, where we wrap data in a single unit. Another advantage of encapsulation is that we can make a class read only or write only, just providing the setter or getter method.
In PHP, what is polymorphism?
Polymorphism is a pattern in object-oriented programming, where classes have different functionality but share a common interface.
What is a cookie and why does PHP use it?
Cookies are little records that the server left on the client's browser to identify a user. So, when that user asks for a page, the browser sends the cookie as well to tell the server information about the user.
PHP makes and recovers cookie values to ease the user experience since the cookie maintains the session id generated at the back end. It must be taken into account that cookies only store string values since PHP cannot access objects across a website or web apps.
Cookies are URL particular, temporary and transitory.
How many types of errors do we find in PHP?
There are 4 types of errors in PHP:
Notices: non-critical errors that are not displayed to the user.
Warnings: a somehow serious but not critical error that does not terminate the script. It is informed to the user.
Fatal Errors: it results in immediate termination of the script.
Parse error: it is a syntax error when the PHP interpreter detects a missing element on the script.
What is the porpoise of the include_once statement in PHP?
It includes and evaluates a specified file while executing the script. It is similar to the include statement, but if the code from that file has been already been included, it will not be taken into consideration again. The include_once returns TRUE if the code has already been added.
Explain what is Exception Handling in PHP
Is an object-oriented way to deal with errors: exception handling change the flow of the code execution if a specified error condition (exception) occurs.
Explain the difference between require_once and require in PHP
While require() includes and evaluates a specific file, require_once() does that only if it has not been included before. The function require_once() is the one to be used if we want to include a file where we have stated lots of functions, for instance.
How do we run a debugging session in PHP?
First, we start the ide and open the file containing the source code to be debugged. Then, we set a breakpoint at each line where we want the debugger to pause, placing the cursor at the beginning of a line and pressing Ctrl-F8 or choosing Debug > Toggle Line Breakpoint.
Explain the use of the urlencode and urldecode functions
The PHP function urlencode(string) encodes a string to be used in a query part of a URL. It is used to avoid the string being confused with the URL itself. This function is used by the browser when it sends data to the web server.
On the other hand, urldecode(string) decodes the encoded values in the given string.
What is Trait in PHP?
A trait is a mechanism for code reuse in PHP, intended to reduce the limitations of the single inheritance. The trait enables the developer to reuse sets of methods in several independent classes, even in different class hierarchies.
What is the difference between traits and interfaces?
With interfaces, we must define the actual implementation of each method within each class that implements said interface. Therefore, we may have many classes implement the same interface with different behaviors.
Meanwhile, traits are just chunks of code that we inject into a class.
It is possible to share a single instance of a Memcache with several PHP projects?
Yes, Memcache is a memory store space, so we can run Memcache on more than one server. Also, we can run two different, completely independent Memcache processes on the same host. However, if the data is partitioned, it becomes necessary to know which instance must be used to get or to put the data.
How can Memcached be updated, when we make changes to PHP?
We can clear the Cache proactively, when an insert or update is made, or reset the Cache. This is similar to the clear method, but instead of deleting the keys and waiting for the next request to refresh the cache, the values are reset after the insert or update.
PHP Code Challenges
So, with these challenges, we have reached the end of this article on PHP Interview Questions. We hope that you feel now more confident and remember: make sure you study and practice as much as possible. Good luck with that interview ahead!
Multi-apply: How to apply to many job opportunities with one single Challenge
We have managed to get most of the companies we collaborate with to adopt our Rviewer challenges in their validation process as an official test.
And here's the bomb: This allows us to offer you, at last, Multi-Apply: with a single technical challenge you will be able to access more than one job offer. Yes, you heard it right: MANY job opportunities by taking a SINGLE code challenge.
Basics: What is PHP
This scripting language was created by Rasmus Lerdorf in 1995, and it has become one of the most widely-used open source general-purpose languages. It has proved especially useful for server-side web development since it generally runs on a web server to create dynamic web page contents or dynamic images.
PHP initially stands for Personal Home Page, but its meaning has changed to Hypertext Preprocessor. PHP has grown to support many databases, like generic ODBC, MySQL, Oracle, PostgreSQL, Solid, Sybase, and others.
With a syntax that resembles Perl and C, PHP is the backbone of most of the Content Management Systems (CMS) nowadays. Among them, excels:
Drupal: a CMS platform developed in PHP and distributed under the GNU (General Public License).
Joomla: yet another free and open-source content management system (CMS) to distribute web content, created by Open Source Matters, Inc.
Magento: an open-source E-trade programming for online business, made by Varien Inc.
WordPress: a free and open-source content management system (CMS) that includes a plug-in architecture and template system. It is widely used in blogging, mailing lists, forums, media displays, and online stores. It is claimed that 43% of the current websites use WordPress as CMS.
Then, it is clear that there is a great demand for PHP experts and lots of positions available. If you are looking forward to getting a job related to this scripting language, we have selected the most commonly asked PHP interview questions and the respective answers, at both basic and advanced levels. Furthermore, we had included several PHP Code Challenges that will help you to be more prepared for your interview ahead. So we hope that when you mastered our selection, you will be ready to excel in your PHP interview.