PHP Interview Questions and Answers
Question - 121 : - How many ways we can give the output to a browser?
Answer - 121 : - HTML output
PHP, ASP, JSP, Servlet Function
Script Language output Function
Different Type of embedded Package to output to a browser
Question - 122 : - What is the default session time in php and how can I change it?
Answer - 122 : - The default session time in php is until closing of browser
Question - 123 : - What changes I have to do in php.ini file for file uploading?
Answer - 123 : - Make the following line uncomment like:
; Whether to allow HTTP file uploads.
file_uploads = On
; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
upload_tmp_dir = C:\apache2triad\temp
; Maximum allowed size for uploaded files.
upload_max_filesize = 2M
Question - 124 : - How can I set a cron and how can I execute it in Unix, Linux, and windows?
Answer - 124 : - Cron is very simply a Linux module that allows you to run commands at predetermined times or intervals. In Windows, it's called Scheduled Tasks. The name Cron is in fact derived from the same word from which we get the word chronology, which means order of time.
The easiest way to use crontab is via the crontab command.
# crontab
This command 'edits' the crontab. Upon employing this command, you will be able to enter the commands that you wish to run. My version of
Linux uses the text editor vi. You can find information on using vi here.
The syntax of this file is very important – if you get it wrong, your crontab will not function properly. The syntax of the file should be as follows:
minutes hours day_of_month month day_of_week command
All the variables, with the exception of the command itself, are numerical constants. In addition to an asterisk (*), which is a wildcard that allows any value, the ranges permitted for each field are as follows:
Minutes: 0-59
Hours: 0-23
Day_of_month: 1-31
Month: 1-12
Weekday: 0-6
We can also include multiple values for each entry, simply by separating each value with a comma.
command can be any shell command and, as we will see momentarily, can also be used to execute a Web document such as a PHP file.
So, if we want to run a script every Tuesday morning at 8:15 AM, our mycronjob file will contain the following content on a single line:
15 8 * * 2 /path/to/scriptname
This all seems simple enough, right? Not so fast! If you try to run a PHP script in this manner, nothing will happen (barring very special configurations that have PHP compiled as an executable, as opposed to an Apache module). The reason is that, in order for PHP to be parsed, it needs to be passed through Apache. In other words, the page needs to be called via a browser or other means of retrieving
Web content. For our purposes, I'll assume that your server configuration includes wget, as is the case with most default configurations. To test your configuration, log in to shell. If you're using an RPM-based system (e.g. Redhat or Mandrake), type the following:
Question - 125 : - How many ways I can redirect a PHP page?
Answer - 125 : - Here are the possible ways of php page redirection.
1. Using Java script:
'; echo 'window.location.href="'.$filename.'";'; echo ''; echo ''; echo ''; echo ''; } } redirect('http://maosjb.com'); ?>
2. Using php function: header("Location:http://maosjb.com ");
List out different arguments in PHP header function?
void header ( string string [, bool replace [, int http_response_code]])
Question - 126 : - What type of headers have to be added in the mail function to attach a file?
Answer - 126 : - $boundary = '--' . md5( uniqid ( rand() ) );
$headers = "From: \"Me\"\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"";
Question - 127 : - What is the difference between Reply-to and Return-path in the headers of a mail function?
Answer - 127 : - Reply-to: Reply-to is where to delivery the reply of the mail.
Return-path: Return path is when there is a mail delivery failure occurs then where to delivery the failure notification.
Question - 128 : - How to store the uploaded file to the final location?
Answer - 128 : - move_uploaded_file ( string filename, string destination)
This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by destination.
If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.
If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE. Additionally, a warning will be issued.
Question - 129 : - Explain about Type Juggling in php?
Answer - 129 : - PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined by the context in which that variable is used. That is to say, if you assign a string value to variable $var, $var becomes a string. If you then assign an integer value to $var, it becomes an integer.
An example of PHP's automatic type conversion is the addition operator '+'. If any of the operands is a float, then all operands are evaluated as floats, and the result will be a float. Otherwise, the operands will be interpreted as integers, and the result will also be an integer. Note that this does NOT change the types of the operands themselves; the only change is in how the operands are evaluated.
$foo += 2; // $foo is now an integer (2)
$foo = $foo + 1.3; // $foo is now a float (3.3)
$foo = 5 + "10 Little Piggies"; // $foo is integer (15)
$foo = 5 + "10 Small Pigs"; // $foo is integer (15)
If the last two examples above seem odd, see String conversion to numbers.
If you wish to change the type of a variable, see settype().
If you would like to test any of the examples in this section, you can use the var_dump() function.
Note: The behavior of an automatic conversion to array is currently undefined.
Since PHP (for historical reasons) supports indexing into strings via offsets using the same syntax as array indexing, the example above leads to a problem: should $a become an array with its first element being "f", or should "f" become the first character of the string $a? The current versions of PHP interpret the second assignment as a string offset identification, so $a becomes "f", the result of this automatic conversion however should be considered undefined. PHP 4 introduced the new curly bracket syntax to access characters in string, use this syntax instead of the one presented above:
Question - 130 : - How can I embed a java programme in php file and what changes have to be done in php.ini file?
Answer - 130 : - There are two possible ways to bridge PHP and Java: you can either integrate PHP into a Java Servlet environment, which is the more stable and efficient solution, or integrate Java support into PHP. The former is provided by a SAPI module that interfaces with the Servlet server, the latter by this Java extension.
The Java extension provides a simple and effective means for creating and invoking methods on Java objects from PHP. The JVM is created using JNI, and everything runs in-process.
Example Code:
getProperty('java.version') . ''; echo 'Java vendor=' . $system->getProperty('java.vendor') . ''; echo 'OS=' . $system->getProperty('os.name') . ' ' . $system->getProperty('os.version') . ' on ' . $system->getProperty('os.arch') . ' '; // java.util.Date example $formatter = new Java('java.text.SimpleDateFormat', "EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz"); echo $formatter->format(new Java('java.util.Date')); ?>
The behaviour of these functions is affected by settings in php.ini.
Table 1. Java configuration options
Name
Default
Changeable
java.class.path
NULL
PHP_INI_ALL
Name Default Changeable
java.home
NULL
PHP_INI_ALL
java.library.path
NULL
PHP_INI_ALL
java.library
JAVALIB
PHP_INI_ALL