Hi in this post we are going to continue discussing Elroi PHP Fresher interview questions with answers[Elroi PHP Fresher Interview Questions with Answers – PART 1]. Let us explore the questions;
Elroi PHP Fresher Interview Questions with Answers–PART 2
7.What is the difference between echo and print?when we use?
We use Print_r to print array and echo to output the data on screen. The difference between the two are as follows;
ECHO | |
· In PHP, echo is not a function but a language construct. | · In PHP, print is a mere function in that it returns a value. |
· Echo is a statement i.e used to display the output. it can be used with parentheses echo or without parentheses echo. | · Print is also a statement i.e used to display the output. it can be used with parentheses print( ) or without parentheses print. |
· Echo can pass multiple string separated as ( , ) | · Using print can doesn’t pass multiple argument |
· Echo doesn’t return any value and faster then print. | · Print always return 1 and slower than echo. |
· Echo can take more than one parameter when used without parentheses. | · Print only takes one parameter. |
· Syntax : void echo ( string $arg1 [, string $… ] ) | · Syntax : int print ( string $arg ) |
8.What is the difference between explode and split?when we use?
We use explode() and split() funtions to split a string into an array, but the difference is that split() uses pattern for splitting whereas explode() uses string. The difference between the two are as follows;
SPLIT | EXPLODE |
In split() you can use regular expressions to split a string. | Whereas explode() splits a string with a string. |
Split is used to split a string using a regular expression. | Explode is used to split a string using another string. |
E.g explode (” this”, “this is a string”) | E.g Split (” + “, “This+ is a string”) |
split() is slower than explode(). | explode() is faster than split(). |
9.What is the difference between include and require?when we use?
We use include and require to insert the content of one PHP file into another PHP file.
INCLUDE | REQUIRE |
· Use include() when the file is not required and application should continue when file is not found. | · Use require() when the file is required by the application. |
· include will only produce a warning (E_WARNING) and the script will continue. | · require will produce a fatal error (E_COMPILE_ERROR) and stop the script execution. |
· include() will throw a warning if it can’t include the file, but the rest of the script will run. | · require() will throw an E_COMPILE_ERROR and halt the script if it can’t include the file. |
10. What is an array? Explain its types and syntax.
ARRAY:
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
Each item in an array is called an element, and each element is accessed by its numerical index.
TYPES OF ARRAY:
There are basically three types of arrays in PHP:
I.Indexed or Numeric Arrays: An array with a numeric index where values are stored linearly.
II.Associative Arrays: An array with a string index where each value can be assigned a specific key.
III.Multidimensional Arrays: An array which contains single or multiple array within it and can be accessed via multiple indices.
SYNTAX FOR INITIALIZED ARRAYS:
array(value1,value2,value3,etc.);
SYNTAX FOR ASSOCIATIVE ARRAYS:
array(key=>value,key=>value,key=>value,etc.);
11.What is the function of print_r?
The print_r() function is a built-in function in PHP and is used to print or display information stored in a variable.
SYNTAX:
print_r( variable, isStore )
PARAMETERS:
=>variable: This parameter specifies the variable to be printed and is a mandatory parameter.
=>isStore: This is an option parameter which is used to store the output of the print_r() function in a variable.
RETURN VALUE:
If the $variable is an integer or a float or a string the function prints the value of the variable. If the variable is an array the function prints the array in a format which displays the keys as well as values, a similar notation is used for objects. If the parameter $isStore is set to TRUE then the print_r() function will return a string containing the information which it is supposed to print.
12.What is the difference between include and require_once?
The basic diffferences between include and require_once are as follows:
require once() | include() |
· when the file contains content that would produce an error on subsequent inclusion, e.g.function important() { /* important code */} is definitely needed in your application but since functions cannot be redeclared should not be included again. | · include when the file is not required and application flow should continue when not found, e.g. great for templates referencing variables from the current scope or something |
· require once() function will not include the file a second time if it has already been included. | · include() function will include the file a second time even if it has already been included |
To be continued…
References:
https://www.quora.com/What-is-the-difference-between-echo-and-print-in-PHP
https://www.quora.com/What-is-the-difference-between-explode-and-split-functions
https://stackoverflow.com/questions/2418473/difference-between-require-include-require-once-and-include-once
https://www.w3schools.com/php/func_array.asp
https://www.geeksforgeeks.org/php-print_r-function/