Professional Documents
Culture Documents
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:
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>
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?
1. if Statement
Syntax
if (condition) {
code to be executed if condition is true;
}
Example
<?php
$t=date(“H”);
if ($t<“20”) {
echo “Have a good day!”;
}
?>
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
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
<?php
$a=5;
$b=”5″;
if($a==$b){
echo “correct”;
else{
echo “false”;
?>
<?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
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.
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
Types of Error
Basically there are four types of errors in PHP, which are as follows:
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 “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”;
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
?>
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”;
?>
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.
1 .LAMP SERVER
2. WAMP SERVER
3. MAMP SERVER