Interview Questions on PHP
31. Explain function overriding?
Overriding :- The process of replacing parent class function with child class function is nothing but function overriding.
Ex :-
<?php
class Foo {
function myFoo() {
return "Foo";
}
}
class Bar extends Foo {
function myFoo() {
return "Bar";
}
}
$foo = new Foo;
$bar = new Bar;
echo($foo->myFoo()); //"Foo"
echo($bar->myFoo()); //"Bar"
?>
32. What is the difference b/w method overloading and method overriding?
In Over Loading Signature must be different.
In OverRiding signature must be same.
33. How to get another website output into a variable?
Use below function :-
$var_name = file_get_contents('http://www.websitename.com');
34. How to open file in write mode?
$myfile = fopen("webdictionary.txt", "w");
35. What is diff b/w r+,w+ and a+ modes?
'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
'r+' Open for reading and writing; place the file pointer at the beginning of the file.
'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() only affects the reading position, writes are always appended.
36. Write a php code to copy one file contents to another file?
copy ( string $source_file_path , string $dest+file_path ).
Ex :-
<?php
$file = 'example.txt';
$newfile = 'example.txt.bak';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
?>
37. How to write key & values in php?
Example code :-
$array = array(
'key1' => 'value1',
'key2' => 'value2' ,
'key3' => 'value3' ,
'key4' => 'value4',
'key5' => 'value5');
38. What is the difference b/w include and require_once?
a. difference between include and require; when a file is included with the include statement and PHP cannot find it, the script will continue to execute.
b. require statement, the echo statement will not be executed because the script execution dies after the require statement returned a fatal error.
39. How to reverse a string in php?
Syntax :- strrev( string $string );
Ex :-
<?php
echo strrev("Hello world!"); // outputs "!dlrow olleH"
?>
40. How to get substring in php?
Syntax :- substr( string $string );
Ex :-
<?php
echo substr("Hello world",6);
?>