Вы находитесь на странице: 1из 14

What is Session?

How Session is created and destroyed

On the internet there is one problem: the web server does not know who you are or
what you do, because the HTTP address doesn’t maintain state.

Session variables solve this problem by storing user information to be used across
multiple pages (e.g. username, favourite colour, etc). By default, session variables last
until the user closes the browser.

Creation of a session:

Start a PHP Session

A session is started with the session_start() function.

Session variables are set with the PHP global variable: $_SESSION.

Example :

<?php
session_start();
?>
<html>
<body>
<?php
// Echo session variables that were set on previous page
echo “Favorite color is ” . $_SESSION[“favcolor”] . “.<br>”;
echo “Favorite animal is ” . $_SESSION[“favanimal”] . “.”;
?>
</body>
</html>

Destroy a PHP Session

To remove all global session variables and destroy the session, use session_unset()
and session_destroy():

<?php
// remove all session variables
session_unset();
// destroy the session
session_destroy();
?>
Name three conditional statements in PHP.
When would you use the === operator?

Ans: Conditional statements in PHP are as follows –

1. if Statement

If statement is used to execute some code only if a specified condition is true.

Syntax

if (condition) {
code to be executed if condition is true;
}

Example

<?php
$t=date(“H”);
if ($t<“20”) {
echo “Have a good day!”;
}
?>

1. If… else Statement

Use If….else statement to execute some code if a condition is true and another
code if the condition is false.

 Syntax

if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}

Example-

<?php
$t=date(“H”);
if ($t<“20”){
echo “Have a good day!”;
} else {
echo “Have a good night!”;
}
?>

1. Switch Statement

The switch statement is used to perform different actions based on different


conditions.

 Syntax

switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;

default:
code to be executed if n is different from all labels;
}

Example-

<?php
$favcolor=”red”;
switch ($favcolor) {
case “red”:
echo “Your favorite color is red!”;
break;
case “blue”:
echo “Your favorite color is blue!”;
break;
case “green”:
echo “Your favorite color is green!”;
break;
default:
echo “Your favorite color is neither red, blue, or green!”;
}
?>
‘===’ is a comparison operator, as their name implies, allow you to compare two
values.

$a===$b Identical True if $a is equal to $b, and they are of same type

Example– Case1 (if we use “==”)

<?php

$a=5;

$b=”5″;

if($a==$b){

echo “correct”;

else{

echo “false”;

?>

Output:- Here Output will be “Correct”

Case2 (if we use “===”)

<?php

$a=5;

$b= “5″;

if($a===$b)

echo “correct”;
}

else

echo “false”;

?>

Output:- Here Output will be false because there is difference in data type of $a
& $b.
Explain data types and variables in PHP. What is the difference between $msg
and $$msg?
Ans :
Data Types

Different types of data take up different amounts of memory and may be treated
differently when they are manipulated in a script. Some programming languages
therefore demand that the programmer declare in advance which type of data a
variable will contain. PHP4 is loosely typed, which means that it will calculate
data types as data is assigned to each variable.
Type Example Description
Integer 5 A whole number
Double 3.2 A floating-point number
A collection of
String “hello”
characters
One of the special values
Boolean True/false
true or false
Object
Array

Variables

PHP is a very loosely typed language. This means that variables do not have to be
declared before they are used, and that PHP always converts variables to the type
required by their context when they are accessed.

A variable is a special container that you can define to “hold” a value. A variable
consists of a name that you can choose, preceded by a dollar ($) sign. The variable
name can include letters, numbers, and the underscore character (_). Variable names
cannot include spaces or characters that are not alphanumeric.

Dynamic Variables

As you know, you create a variable with a dollar sign followed by a variable name.
Unusually, the variable name can itself be stored in a variable. So, when assigning a
value to a variable

$user = “mike”;

is equivalent to

$holder=”user”;

$$holder = “mike”;
The $holder variable contains the string “user”, so you can think of $$holder as a
dollar sign followed by the value of $holder. You can use a string constant to define a
dynamic variable instead of a variable. To do so, you must wrap the string you want to
use for the variable name in braces:
${“user”} = “mike”;
Differentiate between Static and Dynamic web page

S.No. Static web page Dynamic web page


New content brings people back to the site
1. Content can get stagnant
& helps in the search engines
Requires web development expertise
2. Much easier to update
to update site
3. Cheap to host Hosting costs a little more
4. Cheap to develop Slower / more expensive to develop
Can work as a system to allow staff or
5. Site not as useful for the user
users to collaborate
What is the difference between POST & GET Method?

The POST Method

The POST method transfers information via HTTP headers. The information is
encoded as described in case of GET method and put into a header called
QUERY_STRING.

 The POST method does not have any restriction on data size to be sent.
 The POST method can be used to send ASCII as well as binary data.
 The data sent by POST method goes through HTTP header so security
depends on HTTP protocol. By using Secure HTTP you can make sure that
your information is secure.
 The data sent by POST method is not visible in URL.
 The PHP provides $_POST associative array to access all the sent information
using POST method.

The GET Method

GET is used to request data from a specified resource.

GET is one of the most common HTTP methods.

Note that the query string (name/value pairs) is sent in the URL of a GET request:

/test/demo_form.php?name1=value1&name2=value2

Some other notes on GET requests:

 GET requests can be cached


 GET requests remain in the browser history
 GET requests can be bookmarked
 GET requests should never be used when dealing with sensitive data
 GET requests have length restrictions
 GET requests is only used to request data (not modify)
What are the different types of errors in PHP?

Types of Error

Basically there are four types of errors in PHP, which are as follows:

 Parse error (Syntax error)


 Fatal error
 Warning error
 Notice error

1. Parse Errors (Syntax errors)

The Parse error occurs if there is a syntax mistake in the script; the output is parse
errors. A parse error stops the execution of the script. There are many reasons for the
occurrence of parse errors in PHP. The common reasons are as follows:

 Unclosed quotes
 Missing or Extra parentheses
 Unclosed braces
 Missing semicolon

Example :

<?php

echo “cat”;

echo “Dog” // missing ;

echo “Lion”;

?>
2.Fatal errors

Fatal errors are caused when PHP understands that you’ve written, however what
you’re asking it to do can’t be done. Fatal errors stop the execution of the script. If
you are trying to access the undefined functions, then the output is fatal error.

Example :

<?php

Function fun1()

echo “ABC”;

Fun2() ; // there is no Fun2 defined

3.Warning Errors

Watching errors will not stop execution of the script. The main reason for warning
errors is to include a missing file or using the incorrect number of parameters in a
function.

Example:

<?php

Include(‘welcome.php’); // there is no file named welcome.php

?>

4.Notice errors

Notice error is the same error as a warning error i.e., in the notice error execution of
the script does not stop. Notice that the error occurs when you try to access the
undefined variable, and then produce a notice error.

Example :
<?php

$a= “Hello”;

echo “Notice error”;


echo $b; // there is no variable $b

?>
What is the difference between WAMP, MAMP, and LAMP?

All these are used for php website and act as a local server.

So with the help of this we can test our website first locally then upload to the server
without any error and bugs.

The major difference between WAMP, LAMP, and MAMP is operating system.

LAMP is for Linux operating system


WAMP is for windows operating system
MAMP is for Mac OS X operating system.

Xampp is for X-OS, Apache, Mysql, Php, Perl.

1 .LAMP SERVER

1. Full form of LAMP is Linux, Apache, MySQL and PHP.


2. This is an open source platform.
3. LAMP Server is work on Linux Operating System only.
4. LAMP is a combine package of Linux, Apache, MySQL and PHP.
5. Apache is the web server
6. Mysql is the relational database management system.
7. PHP is the object-oriented scripting language.

2. WAMP SERVER

1. Full form of WAMP is Windows, Apache, MySQL and PHP.


2. This is an open source platform.
3. WAMP Server is work on Windows Operating System only.
4. LAMP is a combine package of Windows, Apache, MySQL and PHP.
5. Apache is the web server
6. Mysql is the relational database management system.
7. PHP is the object-oriented scripting language.

3. MAMP SERVER

1. Full form of MAMP is MAC, Apache, MySQL and PHP.


2. This is an open source platform.
3. MAMP Server is work on MAC Operating System only.
4. LAMP is a combine package of MAC, Apache, MySQL and PHP.
5. Apache is the web server
6. Mysql is the relational database management system.
7. PHP is the object-oriented scripting language.