[go: up one dir, main page]

0% found this document useful (0 votes)
453 views6 pages

PHP Functions and Concepts Explained

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
453 views6 pages

PHP Functions and Concepts Explained

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1. State the difference between session and cookies.

ANS:
COOKIE SESSION

Cookie are stored in browser as text file Session are stored in server side.
format

The cookie is a clinet side resources. The session is a server side resources.

It is stored limit amount of data. It is stored unlimited amount of data.

It os not holding the variable in cookie. It is holding the multiple variable in session.

Specific identifier links to server. Specific identifier links to user.

2. State the difference between get and post.


GET Method POST Method

GET has limits on the amount of information POST has no limits on the amount of
to send. The limitation is about 2048 information to send.
characters
Can be bookmarked Cannot be bookmarked.

Can be cached Not cached

Parameter remains in browser history. Parameter are not saved in browser history.
Only ASCII characters are allowed. No restrictions Binary data is also allowed.

3. State the difference between for and for each loop.


ANS:
For loop For each loop

The iteration is clearly visible. The block of The iteration is hidden. The block of code is
code is repeated as long as the condition is repeated until iterating over the array is
met or the counter meets a specific value. completed.

Good performance. Better performance.

The stop condition is specified easily. The stop condition has to be explicitly
specified.

Upon working with collections, it needs the It can simply work without the usage of the
usage of the count() function. count() method.

4. Differentiate between implode and explode function.


Ans:
GET Method POST Method

It joins an array of elements into strings. It splits the array based on the separator.

The output type is a string. The output type is an array.


The syntax of implode() function is The syntax of explode() function is
implode($delimiter, $array) explode($delimiter, $string, $limit)

The output example is ‘“ Car”,” Truck”,” Bus”’. The output example is ‘Array ( [0] => Car [1]
=> Truck [2] => Bus )

5. State user defined function and explain with example.


ANS:
 Definition: -
i. A function is a block of code written in a program to perform some specific task.
ii. They take information as parameters, execute a block of statements or perform
operations on these parameters and return the result.
iii. A function will be executed by a call to the function.

 Define User Defined Function in PHP:


A user-defined function declaration starts with the keyword function.
 Syntax
function functionName() {
code to be executed;
}
 Example
<?php
function writeMsg() {
echo "Welcome to PHP world!";
}
writeMsg(); // call the function
?>

6. Explain cloning in php with example.


ANS:
Object Cloning os the process to create a copy of an object.An Object copy is created by
using the magic method.
<!DOCTYPE html>
<html>
<body>
<?php
class car {
public $color;
public $price;
function __construct()
{
$this->color = 'red';
$this->price = 200000;
}
}
$mycar = new car();
$mycar->color = 'blue';
$mycar->price = 500000;
$newcar = clone $mycar;
print_r($newcar);
?>
</body>
</html>

The above code creates a car with a constructor which initializes its member variable
named color and price.
An object of the variable is created, and it is cloned to demonstrate deep cloning

7. Explain serialization in php with example.


Ans:
i. Serialization is a technique used by programmers to preserve their working
data in format that can later be restored to its previous form.
ii. Serializaling an object means converting it to a byte stream representation
that can be stored in a file.
iii. Serialization in php is mostly automatic, it requires little extra work from
you,beyond calling the serialize() & unserialize functions.

Syntax: Serialize(value1);

Example:
<?php
$s_data= serialize(array('Welcome', 'to', 'PHP'));
print_r($s_data . "<br>");
$us_data=unserialize($s_data);
print_r($us_data);
?>
8. How do you validate user inputs in PHP?
ANS: There are several ways to validate user inputs in PHP:

1. Using built-in PHP functions:


PHP provides several built-in functions for validating user inputs, such as
`filter_var()`, `is_numeric()`, `is_email()`, etc.

2. Using regular expressions:


Regular expressions can be used to validate complex patterns in user inputs, such as
phone numbers, zip codes, or custom formats.

3. Implementing custom validation logic:


You can write your own validation logic to ensure that user inputs meet specific
criteria, such as length, format, or range.

4. Using input sanitization:


Sanitizing user inputs to remove or escape potentially malicious content is an
important part of input validation.

You might also like