This page contains PHP interview questions and answers.
require statement is used to include a file. And if we use require multiple times then we will include a given file multiple times.
require_once statement is similar to the require statement but in this case a file in not included again if it is already included.
In the following example we have a
sample.php file and we are including it in the
<?php echo "Hello from sample.php file."; ?>
<?php require 'sample.php'; require 'sample.php'; require 'sample.php'; ?>
hello.php file will print "Hello from sample.php file." 3 times.
If we use
require_once then we will get the message only once.
<?php require_once 'sample.php'; require_once 'sample.php'; require_once 'sample.php'; ?>
Both are used to include files but
require will exit with fatal error if it fails to include the file. Whereas,
include will move on with the execution even if it fails to include the file.
In the following example we are using
include to include
dummy.php file which does not exists.
<?php include 'dummy.php'; echo "some text message..."; ?>
The above code gives the following output.
Warning: include(dummy.php): failed to open stream: No such file or directory in /Users/yusufshakeel/Sites/php-example/include-file.php on line 2 some text message...
In the following code we are using
require and it will give fatal error.
<?php require 'dummy.php'; echo "some text message..."; ?>
Fatal error: require(): Failed opening required 'dummy.php' in /Users/yusufshakeel/Sites/php-example/require-file.php on line 2
For this we can use the
<?php echo "IP Address: " . $_SERVER["REMOTE_ADDR"]; ?>
PHP code snippet:
echo printf('Hello World');
The output of the above code is
printf() will execute which will print
Hello World. Then
echo will print 11 which is the returned value of printf function.
printf function returns the length of the outputted string.
$x = 10; $y = 'x'; $$y = 20; echo $x . ' ' . $y;
The above code will print
$$y translate into
$$y = 20; means we are assigning 20 value to variable $x.
unlink to delete a file from the file system.
unset to set the variable to
Example: Following code will delete a file.
Example: Following code will set the variable to undefined.
$x = 10; unset($x);
There are three types of error in PHP.
This is a non-critical error and occurs during execution of the PHP script.
Example: When accessing an undefined variable we will get a notice error.
Important than Notice error and occurs during script execution. Though the execution does not stops when a warning error occurs.
Example: A warning error will occur when trying to
include a file that does not exists.
Fatal error is a very important error type. When it occurs it terminates the execution of the script.
Example: When trying to
require a file that does not exists then a fatal error will occur and the script will stop executing.