PHP Question Answer
PHP Question Answer
PHP Question Answer
(c) What is different between single quote String literal & double quote String literal?
Ans:
Single Quoted Literal:The simplest way to specify the a string into enclose it in single quote
e.g:- the character
In the echo statement the single quote used to display on the browser windows but variable not expanded
occur in the single quoted
e.g :
$a=10;
echo the number of a = $a;
o/p the number of a = $a
Double Quoted Literal:It the String is enclosed in double quoted()
e.g The String
It is expanded the occur the variable in double quoted literal
e.g
$a=10;
echo the number of a = $a;
o/p the number of a = 10
(d)Explain the terms Die &Return?
Ans :
Die :
The return statement its argument as the value of the function call
Return statement will also end the execution of an eval() statement of an or Script file
$_session[views]=1;
?>
(g)What is different between function unlink and unset?
Ans :
Unset () Function:- it is mainly used to free the specified session variable
e.g
<?php
unset($_session[views)
?>
Unlink() Function :- Its mainly used delete the file
Syntax :
unlink(filename,context)
Integer number
2.
3.
Strings
4.
Booleans
5.
Arrays
6.
Objects
7.
Resources
8.
Null
Integer
The integer data types is use to specify a numeric value without a factorial comment.
Real number
It is also known as floating number or floating point number. It is whole number and has
fractions such as 1.22, 2.45, 100.765 etc.
3.14
0.001
-1.234
0.314E2 // 31.4
1.234E-5//0.00001234
-3.45E-3//-0.00345
Boolean
Boolean values are true or false, also 0 and empty string evaluates to false, and any numeric value
rather than zero, or a string that is not empty evaluates to true.
You can declare as given bellow.
Boolean $variables;
Strings
String values are sequence of characters, include in a single quotes, for example,
1.
Single quoted
2.
Double quoted
3.
Heredoc quoted
4.
An array in PHP is actually ordered map. A map is a type that associates values to keys.
This type is optimized for several different uses; it can be treated as an array, list (vector), hash
table (an implementation of a map), Dictionary, collection, stack, queue, and probably more.
As array values can be other arrays, trees and multidimensional arrays are also possible.
Resource
Resources are not actual data type, but the storing of a reference to functions and resources
external to PHP.
The most common example of using the resources data type is a data base call.
Null
Null is a special data type which can only have one value, which is itself.
Which is to say, null is not only a data type, but also a key word literal. A variable of data type
null is a variable that has no value assigned to it
OR
Mysql supports various types of tables or storage engines to allow you To optimize your
database.
1. ISAM :
ISAM had been deprecated and removed from version 5.x. all of
It functionality entire replace by MYISAM. ISAM table has a hard Size 4GB and is not portable.
2. MYISAM:
operating system and the data File is portable from system to system.
With MYISAM table type you can have 64 keys per table and maximum key length of 1024
bytes.
3. InnoDB:
Different from MYISAM table type, innoDB table are transaction safe And supports rowlevel locking.
The data file of Innodb table can be stored in more than one File so the size of table
depends on the disk space.
like the MYISAM Table type, data file of InnoDB is portable from system to system.
The disadvantage of InnoDB in comparison with MyISAM is it take More disk space.
4. BDB:
5. MERGE:
Merge table type is added to treat multiple MYISAM tables as a single Table so it remove
the size limitation from MYISAM tables.
6. HEAP:
Heap table is stored in memory so it is the fastest one. Because of storage Mechanism, the
data will be lost when the power failure and sometime it cause the server run out of memory.
Heap tables do not supports columns with AUTO_INCREMENT, BLOB and TEXT
characteristics.
(b)Discuss different loops available in php.
Loops in PHP are used to execute the same block of code a specified number of times, or while a
specified condition is true.
PHP supports four loop types.
1. for
2. while
3. do...While
4. for each
1. for loop:
The for statements is used when you know how many times you want To execute a statement or
block of statement.
Syntax:
for(initialization; condition; increment)
{
Code to be executed;
}
Example:
<?php
$test =5;
For ($i=1 i<=5 ; i++)
{
echo $i;<br/>;
}
?>
Output:
1
2
3
4
5
2. While loop:
Output:
I = 1;
Num = 40
3. Dowhile loop:
The dowhile statement will execute a block of code at least Once it then will repeat the loop as
long as a condition is true.
Syntax:
do
{
Code to be executed;
}while(condition);
Example:
<?php
$i=0;
$num=50;
While($i < 10)
{
$sum;
$i++;
Example:
<?php
$array = array(1,2,3,4,5);
foreach($array as $value)
{
Echovalue
is
$value <br/>;
}
?>
Output:
Value is 1
Value is 2
Value is 3
Value is 4
Value is 5
$file=fopen("E:/Sonu/test.txt","r");
?>
2) Fread() function :
The fread() reads from an open file. The function will stop at the end of the file or when it reaches the
specified length,whichever comes first.
Syntax:
fread(file,length)
Example1:<?php
$file=fopen("test.txt","r")
fread($file,"10");
fclose($file);
?>
Example2:<?php
$file=fopen("test.txt","r");
fread($file,filesize("test.txt"));
// for read entire file...
fclose($file);
?>
3) Fwrite() function :
The fwrite() writes to an open file. The function will stop at the end of the file or when it reaches the
specified length,whichever comes first.
Syntax:
fwrite(file,string,length)
length is optional..
Example:<?php
$file=fopen("test.txt","r+");
fwrite($file,"Hello");
fclose($file);
?>
4) Fclose() function :
The fclose() function closes an open file. The function returns TRUE on success or FALSE on failure.
Syntax:
fclose(file)
Example:
<?php
$file=fopen("test.txt","r");
// Some code to be executed
fclose($file);
echo the file is closed
?>
Output:
The file is closed
5) File_exists() function :
The file_exists() function checks whether or not a file or directory exists.
Syntax:
file_exists(path)
Example:<?php
echo file_exists("test.txt");
?>
Output:
The output of the code will be: 1
OR
A PHP session solves this problem by allowing you to store user information on the server for later use
(i.e. username, shopping items, etc). However, session information is temporary and will be deleted after
the user has left the website. If you need a permanent storage you may want to store the data in a
database.
Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID. The
UID is either stored in a cookie or is propagated in the URL.
Why Use Session?
As a website becomes more sophisticated, so must the code that backs it. When you get to a stage where
your website needs to pass along user data from one page to another, it might be time to start thinking
about using PHP sessions.
A normal HTML website will not pass data from one page to another. In other words, all information is
forgotten when a new page is loaded. This makes it quite a problem for tasks like a shopping cart, which
requires data (the user's selected product) to be remembered from one page to the next.
(b) Write a php script to list data in table from database.
<?php
$cn=mysql_connect("localhost","root","");
mysql_select_db("test_db",$cn);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Database Sample Application</title>
</head>
<body>
<h1 align="center">My First Database Application</h1>
<hr size="3" color="#FF0000"/>
<table align="center">
<tr bgcolor="#CCCCCC">
<td>Name</td>
<td>Age</td>
</tr>
<?php
$query=mysql_query("select * from student");
while($rs=mysql_fetch_object($query))
{
?>
<tr>
<td><?php echo $rs->s_name;?></td>
<td><?php echo $rs->s_age;?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
OUTPUT:
While there is only one type of comment in HTML, PHP has two types.
The first type we will discuss is the single line comment. The single line comment tells the
interpreter to ignore everything that occurs on that line to the right of the comment.
for single line comment type "//" or "#" and all text to the right will be ignored by PHP
interpreter.
PHP Code:
<?php
echo "Hello World!"; // This will print out Hello World!
// echo "Hello India";
# echo "Hello India";
?>
Similar to the HTML comment, the multi-line PHP comment can be used to comment out large blocks of
code or writing multiple line comments. The multiple line PHP comment begins with " /* " and ends with
" */ ".
<?php
echo "Hello World!";
/* echo "Hello India";
echo "Hello World";
*/
?>
Arrays provide a way to store much larger amounts of data without having many separate named
variables.
If a normal variable where data can be stored, a simple array is like a row where lots of data can
be stored.
Each element in the array has its own index so that it can be easily accessed.
the array.
You can assign any type for keys and values. Such as string, float ,integer etc.
1.Numeric array
1. In the following example the index are automatically assigned (the index starts at 0):
<?php
$names=array("Juned Sir"," Nishant"," Karishma"," Mayur");
?>
2. In the following example we assign the index manually:
<?php
$names[0]="JunedSir";
$names[1]="Nishant";
$names[2]="Karishma";
$names[3]="Mayur";
?>
2.Associative array :
An associative array, each ID key is associated with a value.
When storing data about specific named values, a numerical array is not always the best way to
do it.
With associative arrays we can use the values as keys and assign values to them.
<?php
$ages = array("Nishant"=>20, "Mayur"=>19, "Karishma"=>21);
?>
3.Multidimensional array
In a multidimensional array, each element in the main array can also be an array.
And each element in the sub-array can be an array, and so on.
<?php
$faculties=array
(
"BCA"=>array
(
"JunedSir",
"MaulikSir",
"KirtiMadam"
),
"BBA"=>array
(
"ShamaMadam"
),
"BCOM"=>array
(
"NehaMadam",
"BhumikaMedam",
"RadhikaMadam"
)
);
?>
The array above would look like this if written to the output:
<?php
Array
(
[BCA] => Array
(
[0] => Juned Sir
[1] => Maulik Sir
[2] => Kirti Madam
)
[BBA] => Array
(
[0] => Shama Madam
)
[BCOM] => Array
(
[0] => Neha Madam
[1] => Bhumika Medam
[2] => Radhika Madam
)
)
?>
Syntax :
sort(array)
Example:
<?php
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
print_r($fruits);
?>
The output of the code above will be:
Array ( [0] => apple [1] => banana [2] => lemon [3] => orange )
2) rsort()
The asort() function sorts an array by the values. The values keep their original keys.
Syntax:
asort(array)
Example:
<?php
$fruits = array("lemon", "orange", "banana", "apple");
asort($fruits);
print_r($fruits);
?>
sorts an array by the values in reverse order. The values keep their original keys.
Syntax:
arsort(array,sorttype)
Example:
<?php
$fruits = array("lemon", "orange", "banana", "apple");
arsort($fruits);
print_r($fruits);
?>
The output of the code above will be:
Array ( [1] => orange [0] => lemon [2] => banana [3] => apple )
Q.4(C) Write a script to fill the combo box from mysql database.
<?php
$cn=mysql_connect("localhost","root","");
mysql_select_db("test_db",$cn);
?>
<html>
<body>
<select name="mycombo">
<?php
$query=mysql_query("select * from student");
while($rs=mysql_fetch_object($query))
{
?>
<option><?php echo $rs->s_name;?></option>
<?php
}
?>
</select>
</body>
</html>
?
1
Output:
OR
Q.4(B). Write a script for login page with validations.
<?php
$cn=mysql_connect("localhost","root","");
mysql_select_db("test_db",$cn);
?>
<html>
<body>
<h1 align="center">My Login Page</h1>
<hr size="3" color="#FF0000"/>
<script language="javascript">
function checkfrm()
{
if(document.frmlogin.txtlogin.value == "")
{
alert("Please Enter User Name..");
document.frmlogin.txtlogin.focus();
return false;
}
if(document.frmlogin.txtpassword.value == "")
{
alert("Please Enter Password..");
document.frmlogin.txtpassword.focus();
return false;
}
return true;
}
</script>
<?php
if(isset($_POST['submit']))
{
$sql = "select * from login where username='".$_POST['txtlogin']."' and pas
$_POST['txtpassword']."'";
$rs = mysql_query($sql);
if(mysql_num_rows($rs) > 0)
{
header("location:welcome.php");
}
else
{
echo "Invalid Login";
}
}
?>
<form name="frmlogin" method="post" onsubmit="return checkfrm()">
<table align="center">
<tr>
<td>User Name:</td>
<td><input type="text" name="txtlogin" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="txtpassword" /></td>
</tr>
<tr>
<td></td>
<td align="center">
<input type="submit" name="submit" value="Login" />
<input type="reset" /></td>
</tr>
</table>
</form>
</body>
</html>
<strong>Output:</strong>