Top PHP Interview Questions And Answers
Criteria | PHP | Java |
Deployment area | Server-side scripting | General purpose programming |
Language type | Dynamic typed | Static typed |
Rich set of APIs | No | Yes |
crypt () function is used to create one way encryption. It takes one input string and one optional parameter. The function is defined as: crypt (input_string, salt), where input_string consists of the string that has to be encrypted and salt is an optional parameter. PHP uses DES for encryption. The format is as follows:
A form can be posted or submitted without the button in the following ways:
1. On OnClick event of a label in the form, a JavaScript function can be called to submit the form.
Example:
document.form_name.submit()
2. Using a Hyperlink: On clicking the link, JavaScript function can be called.
Example:
A form can be submitted in these other ways without using submit button.
- Submitting a form by clicking a link
- Submitting a form by selecting an option from drop down box with the invocation of onChange event
- Using java script : document.form.submit();
- Using header(“location:page.php”);
- Default time allowed for the PHP scripts to execute is 30 secs mentioned in the php.inifile. The function used is set_time_limit(int sec). If the value passed is ‘0’, it takes unlimited time. It should be noted that if the default timer is set to 30 sec, and 20 sec is specified in set_time_limit(), the script will run for 45 seconds.
- This time can be increased by modifying the max_execution_time in secs. The time must be changed keeping the environment of the server. This is because modifying the execution time will affect all the sites hosted by the server.
- The script execution time can be increased by
- Using sleep() function in PHP script
- Using set_time_limit() function
- The default limit is 30 seconds. The time limit can be set to zero to impose no time limit to pause.
- Zend Engine is used internally by PHP as a compiler and runtime engine. PHP Scripts are loaded into memory and compiled into Zend opcodes.
- These opcodes are executed and the HTML generated is sent to the client.
- The Zend Engine provides memory and resource management, and other standard services for the PHP language. Its performance, reliability and extensibility played a significant role in PHP’s increasing popularity.
The PDF functions in PHP can create PDF files using the PDFlib library Version 6. PDFlib offers an object-oriented API for PHP 5 in addition to the function-oriented API for PHP 4.
There is also the » Panda module. FPDF is a PHP class, which allows generating PDF files with pure PHP (without using the PDFlib library.)
F from FPDF stands for Free: you may use it for any kind of usage and modify it to suit your needs. FPDF requires no extension (except zlib to activate compression and GD for GIF support) and works with PHP4 and PHP5.
- Zend Engine 3 performance improvements and 64-bit integer support on Windows
- uniform variable syntax AST-based compilation process
- added Closure::call()
- bitwise shift consistency across platforms
- (null coalesce) operator
- Unicode codepoint escape syntax
- return type declarations
- and scalar type (integer, float, string and boolean) declarations.
- htaccess files are configuration files of Apache Server that provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.
- These .htaccess files are used to change the functionality and features of Apache web server.
For instance, htaccess file is used for url rewrite.
–> It is used to make the site password protected.
–> .htaccess file can restrict some ip addresses so that on restricted ip addresses, the site will not open.
- Magic methods are member functions that are available to all the instance of class. Magic methods always start with “__”. Eg. __construct.
- All magic methods need to be declared as public
- To use a method, it should be defined within the class or program scope
- Various Magic Methods used in PHP 5 are: __construct() __destruct() __set() __get() __call() __toString() __sleep() __wakeup() __isset() __unset() __autoload() __clone().
PEAR is an acronym for “PHP Extension and Application Repository” The purpose of PEAR is to provide:
- A structured library of open-sourced code for PHP users
- A system for code distribution and package maintenance
- A standard style for writing code in PHP
- PHP Foundation Classes (PFC)
- PHP Extension Community Library (PECL)
- A website, mailing lists and download mirrors to support the PHP/PEAR community
soundex()
The soundex() function calculates the soundex key of a string. A soundex key is a four character long alphanumeric strings that represents English pronunciation of a word. The soundex() function can be used for spelling applications.
$str= “hello”; Echo soundex($str); ?>
metaphone()
the metaphone() function calculates the metaphone key of a string. A metaphone key represents how a string sounds if pronounced by an English person. This function can also be used for spelling applications.
echo metaphone(“world”); ?>
Smarty is a template engine written in PHP. Typically, these templates will include variables —like {$variable} — and a range of logical and loop operators to allow adaptability within of the template.
13. What is Memcache?
Memcache is a technology that caches objects in memory such that your web application can get to them really fast. It is used by sites such as Digg.com, Facebook.com and NowPublic.com and is widely recognized as an essential ingredient in scaling any LAMP.
- 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.
- Remember that if your PHP script was written for the Web CGI interface, it may not execute properly in command line environment.
Advanced Questions
To scrap the data from website, Website must be public and open for scrapable.
In the blow code, just update the CURLOPT_URL to which websites data you want to scrap.
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.web-technology-experts-notes.in/"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); $output = curl_exec($ch); curl_close($ch); echo $output;
- $message is used to store variable data. $$message can be used to store variable of a variable. Data stored in $message is fixed while data stored in $$message can be changed dynamically.
Example:
$var1 = ‘Variable 1’ $var1= ‘variable2’ This can be interpreted as $ Variable 1=‘variable2’; For me to print value of both variables, I will write $var1 $($var1)
- $message is a variable and $$message is a variable of another variable.
Example
$Message = "YOU"; $you= "Me"; echo $message //Output:- you echo $message //output :-Me
$$message allows the developer to change the name of the variable dynamically.
Urlencode can be used to encode a string that can be used in a url. It encodes the same way as posted data from web page is encoded. It returns the encoded string.
Syntax:
urlencode (string $str )
urlencode () is the function that can be used conveniently to encode a string before using in a query part of a URL. This is a convenient way for passing variables to the next page.
Syntax:
urldecode (string $str )
urldecode() is the function that is used to decode the encoded string. Urldecode can be used to decode a string. Decodes any %## encoding in the given string (Inserted by urlencode.)
header(‘Content-Type: text/html; charset=utf-8’);
Xdebug: – It uses the DBGp debugging protocol for debugging.
The debug information that Xdebug can provide includes the following:
- stack and function traces in error messages with:
- full parameter display for user defined functions
- function name, file name and line indications
- support for member functions
- memory allocation
- protection for infinite recursions
Xdebug also provides:
- profiling information for PHP scripts
- code coverage analysis
- Capabilities to debug your scripts interactively with a debugger front-end.[4]
Xdebug is also available via PECL
6. How can I execute an anonymous function?
call_user_func(function() { echo ‘anonymous function called.’; });
There are different methods through which we can send mails in PHP. They are as follows:
- PHP mail() function
It implicitly sends a message to SMTP server, which is configured in the php.ini file. This function is used by the base class of MIME message composing and sending package. - SMTP server relay
They are used to relay the messages to an intermediate SMTP server. This server stores the messages temporarily and will try to deliver them in the destination SMTP server. - Sending urgent messages by doing direct delivery to the destination SMTP server
A variable named direct_delivery is provided by the smtp_message_class sub-class, which connects to the destination SMTP server and sends the message directly.
- Include Net/DNS.php file in the beginning of the script
- Create an object for DNS resolver by using $ndr = Net_DNS_Resolver()
- Query the ip address using $ndr->search(“somesite.com”,”A”) and assign to a relevant variable. Ex: $result
- Now, display the value of $result
$startTime= microtime(true);
/** Write here you code to check **/
/** Write here you code to check **/
$endTime= microtime(true);
echo ‘Time Taken to execute the code:’.$endTime-$startTime
We can resolve these errors through php.ini file or through .htaccess file.
- From php.ini file, increase the max_execution_time =360 (or more according to need)
and change memory_limit =128M (or more according to need) - From php file, we can increase time by writing ini_set(‘max_execution_time’,360 ) at top of php page to increase the execution time.And to change memory_limit write ini_set(‘memory_limit ,128M )
- From .htaccess file, we can increase time and memory by:
We can retrieve data in the result set of MySQL using PHP in four Ways
- mysqli_fetch_row >> Get a result row as an enumerated array
- mysqli_fetch_array >> Fetch a result row as associative and numeric array
- mysqli_fetch_object >> Returns the current row of a result set as an object
- mysqli_fetch_assoc >> Fetch a result row as an associative array
mysqli_fetch_object() is similar to mysqli_fetch_array(), with one difference –
an object is returned instead of an array, which implies that that we can only access the data by the field names, and not by their offsets (numbers are illegal property names).
How can we destroy a session in PHP
Yes, we can include (“xyz.php”) more than one time in any page. But it creates a problem when a xyz.php file contains some function declaration- an error occurs due to an already present function in this file. Otherwise, there is no problem, for instance if you want to show same content two times in the page then you must include it two times.
mysqladmin -u root -p password “newpassword”
Once the Web server received the uploaded file, it will call the PHP script specified in the form action attribute to process them. This received PHP script can get the uploaded file information through a predefined array called $_FILES. Uploaded file information is organized in $_FILES as a two-dimensional array as:
- $_FILES[$fieldName][‘name’] : Original file name on the browser system.
- $_FILES[$fieldName][‘type’] : the file type determined by the browser.
- $_FILES[$fieldName][‘size’] : Number of bytes of the file content.
- $_FILES[$fieldName][‘tmp_name’] : a temporary filename of the file in which the uploaded file was stored on the server.
- $_FILES[$fieldName][‘error’] an error code associated with this file upload.
- The $fieldName is the name used in the <INPUT,>.
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():
We can destroy a session by:
To delete a specific session variable, we use
17. What will be the output of following?
function changevalue(&$y) { $y = $y + 7; } $num = 8; changevalue($num); echo $num;
It would be: 15
Reference will take the value and will add 5 to it.