PHP Differences Questions
PHP Differences Questions
8. How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators
differ?
The comparison operator called Equal Operator is the double equal sign “==”. This operator accepts two
inputs to compare and returns true value if both of the values are same (It compares only value of
variable, not data types) and return a false value if both of the values are not same.
The comparison operator called as the Identical operator is the triple equal sign “===”. This operator
allows for a much stricter comparison between the given variables or values.This operator returns true if
both variables contains same information and same data types otherwise return false.
9. What is PEAR in php?
PEAR (PHP Extension and Application Repository) is a framework and repository for reusable PHP
components. PEAR is a code repository containing all kinds of php code snippets and libraries.
PEAR also offers a command-line interface that can be used to automatically install packages.
11. What is the difference between include_once() and require_once() , which one would you use in
circumstances where you need to connect to a database, and why?
include_once() or include allows a file to be included, and in cases where the file is missing or has the
wrong name, we receive an error message and execution will still continue regardless.
On the other hand, require_once() or require would be suitable in cases where a file needs to be
included once and if it is missing or has a wrong name then we receive a fatal error and the execution of
the program stops.
require_once or require is a suitable method in cases where a database connection file is involved and
helps alleviate the possibility of multiple instances of the same file being included several times.
PEAR is a framework and repository for reusable PHP components. PEAR stands
for PHP Extension and Application Repository. It contains all types of PHP code
snippets and libraries.
It also provides a command line interface to install "packages" automatically.
Rasmus Lerdorf
In static websites, content can't be changed after running the script. You can't
change anything in the site. It is predefined.
In dynamic websites, content of script can be changed at the run time. Its
content is regenerated every time a user visit or reload. Google, yahoo and every
search engine is the example of dynamic website.
6) What is the name of scripting engine in PHP?
The Zend Engine is the open source scripting engine written in C language that
interprets the PHP programming language.
o WordPress
o Joomla
o Magento
o Drupal etc.
o CakePHP
o CodeIgniter
o Yii 2
o Symfony
o Zend Framework etc.
PHP echo output one or more string. It is a language construct not a function. So
use of parentheses is not required. But if you want to pass more than one
parameter to echo, use of parentheses is required. It returns nothing.
Echo can output one or more string but print can only output one string and
always returns 1.
Echo is faster than print because it does not return any value.
PHP constants are name or identifier that can't be changed during execution of the
script. PHP constants are defined in two ways:
o Using define() function
o Using const() function
PHP magic constants are predefined constants which changes on the basis of their
use. They start with a double underscore (__) and end with a double underscore
(__).
PHP data types are used to hold different types of data or values. There are 8
primitive data types which are further categorized in 3 types:
PHP Data Types
1. Scalar Types
2. Compound Types
3. Special Types
PHP Data Types: Scalar Types
The PHP count() function is used to count total elements in the array, or something
an object.
The header() function is used to send a raw HTTP header to a client. It must be
called before sending the actual output. For example, you can't print any HTML
element before using this function.
The isset() function checks if the variable is defined and not null.
PHP parameterized functions are functions with parameters. You can pass any
number of parameters inside a function. These passed parameters act as variables
inside your function. They are specified inside the parentheses, after function
name. Output depends upon dynamic values passed as parameters into function.
PHP supports variable length argument function. It means you can pass 0, 1 or n
number of arguments in function. To do this, you need to use 3 ellipses (dots)
before the argument name. The 3 dot concept is implemented for variable length
argument since PHP 5.6.
PHP supports variable length argument function. It means you can pass 0, 1 or n
number of arguments.
PHP array is an ordered map (contains value on the basis of key). It is used to hold
multiple values of similar type in a single variable.
PHP index is represented by number which starts from 0. We can store number,
string and object in the PHP array. All PHP array elements are assigned to an index
number by default.
There are two ways to define indexed array:
1st way:
$season=array("summer","winter","spring","autumn");
2nd way:
$season[0]="summer";
$season[1]="winter";
$season[2]="spring";
$season[3]="autumn";
Example
File: array1.php
<?php
$season=array("summer","winter","spring","autumn");
echo "Season are: $season[0], $season[1], $season[2] and $season[3]";
?>
Output:
Season are: summer, winter, spring and autumn
File: array2.php
<?php
$season[0]="summer";
$season[1]="winter";
$season[2]="spring";
$season[3]="autumn";
echo "Season are: $season[0], $season[1], $season[2] and $season[3]";
?>
Output:
Season are: summer, winter, spring and autumn
Click me for more details...
We can associate name with each array elements in PHP using => symbol.
There are two ways to define associative array:
1st way:
$salary=array("Sonoo"=>"350000","John"=>"450000","Kartik"=>"200000");
2nd way:
$salary["Sonoo"]="350000";
$salary["John"]="450000";
$salary["Kartik"]="200000";
Example
File: arrayassociative1.php
<?php
$salary=array("Sonoo"=>"350000","John"=>"450000","Kartik"=>"200000");
echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";
echo "John salary: ".$salary["John"]."<br/>";
echo "Kartik salary: ".$salary["Kartik"]."<br/>";
?>
Output:
Sonoo salary: 350000
John salary: 450000
Kartik salary: 200000
File: arrayassociative2.php
<?php
$salary["Sonoo"]="350000";
$salary["John"]="450000";
$salary["Kartik"]="200000";
echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";
echo "John salary: ".$salary["John"]."<br/>";
echo "Kartik salary: ".$salary["Kartik"]."<br/>";
?>
Output:
Sonoo salary: 350000
John salary: 450000
Kartik salary: 200000
You can use JavaScript submit() function to submit the form without explicitly
clicking any submit button.
PHP allows you to include file so that page content can be included in another PHP
file. There are two ways to include file in PHP.
1. Include()
2. Require()
Require and include both are used to include a file, but if file is not found include
sends warning and continues executing the script whereas require sends Fatal error
and stops the execution.
PHP setcookie() function is used to set cookie with HTTP response. Once cookie is
set, you can access it by $_COOKIE superglobal variable.
Syntax:
bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path
[, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )
PHP Engine creates a logical object to preserve data across subsequent HTTP
requests or multiple pages of the same application, which is known as session.
Sessions generally store temporary data to allow multiple PHP pages to offer a
complete functional transaction for the same user.
Simply, it maintains data of a user (browser).
<?php
Session_register($ur_session_var);
?>
PHP session_start() function is used to start the session. It starts a new or resumes
the existing session. It returns the existing session if session is created already. If
session is not available, it creates and returns new sessions.
The main difference between session and cookie is that cookies are stored on
user's computer in the text file format while sessions are stored on the server side.
Cookies can't hold multiple variables on the other hand Session can hold multiple
variables.
You can manually set an expiry for a cookie, while session only remains active as
long as browser is open.
PHP fopen() function is used to open file or URL and returns resource. It accepts
two arguments: $filename and $mode.
Syntax:
1. resource fopen ( string $filename , string $mode [, bool $use_include_path =
false [, resource $context ]] )
PHP provides various functions to read data from file. There are different functions
that allow you to read all file data, read data line by line and read data character by
character.PHP file read functions are given below:
o fread()
o fgets()
o fgetc()
47) How to write in a file in PHP?
PHP fwrite() and fputs() functions are used to write data into file. To write data into
file, you need to use w, r+, w+, x, x+, c or c+ mode.
49) What is the method to execute a PHP script from the command line?
You should just run the PHP command line interface (CLI) and specify the file
name of the script to be executed.
There are two methods to connect MySQL database with PHP. Procedural and
object oriented style.
Since PHP 4.3, mysql_reate_db() is deprecated. Now you can use following 2
alternatives.
o mysqli_query()
o PDO::_query()
By default, maximum execution time for PHP scripts is set to 30 seconds. If a script
takes more than 30 seconds, PHP stops the script and returns an error.
You can change the script run time by changing the max_execution_time directive
in php.ini file.
When a script is called, set_time_limit function restarts the timeout counter from
zero. It means, if default timer is set to 30 sec, and 20 sec is specified in function
set_time_limit(), then script will run for 45 seconds. If 0 sec is specified in this
function, script takes unlimited time.
The PHP split() function splits string into an array by regular expression.
$_SERVER["REMOTE_ADDR"];
https://www.javatpoint.com/php-interview-questions
$here_doc = <<<EOT
This is $name website
for PHP, Laravel and Angular Tutorials
EOT;
echo $here_doc;
Output:
This is Bootsity website
for PHP, Laravel and Angular Tutorials
4.nowdoc
The Nowdoc string definition method in PHP is like the heredoc method but it
works like the way single quotes work.
For nowdoc, no parsing takes place at time of printing inside the Nowdoc
blocks.
Nowdoc string definition comes handy when we are working with raw data that
do not need to be parsed.
Consider:
$name = "Bootsity";
$here_doc = <<<'EOT'
This is $name website
for PHP, Laravel and Angular Tutorials
EOT;
echo $here_doc;
Output:
This is $name website
for PHP, Laravel and Angular Tutorials
Concatenating strings
In PHP, string concatenation is done using concatenation operator, which is
denoted by a dot (.)
Lets see how we can use it practically:
$str = "Bootsity"." Tutorials"
echo $str; // will print - Bootsity Tutorials
Some important String functions
In this section, we are including most frequently used in-built PHP functions
related to strings. If you want to see the complete list of string functions,
1 strtolower
strtolower makes a string lowercase
print strtolower("PHP and Laravel Tutorials"); // php and laravel
tutorials
Similarly, to make a string uppercase, we can use strtoupper
2 strlen
strlen computes length of string
print strlen("abcd"); // 4
3 explode
explode returns array after separating strings delimited by given string
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
4 substr
substr returns part of the string
$rest = substr("abcdef", 0, -1); // returns "abcde"
$rest = substr("abcdef", 2, -1); // returns "cde"
$rest = substr("abcdef", 4, -4); // returns false
5 ucfirst
ucfirst turns a string’s first character uppercase
$foo = 'hello world!';
$foo = ucfirst($foo); // Hello world!
5. Comments
Comments are strings that we write in our programs to give information
about the current code. Comments are meta-data about code and are not
executed at run-time. PHP supports two type of comments: 1. Single line
comments and 2. Multi line comments. Let’s see them:
<?php
/* Multiline comment
it spreads across multiple lines
*/