PHP Interview Questions
PHP (Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. It is mainly used to create dynamic web page content or dynamic images used on websites or elsewhere. PHP has many frameworks and cms for creating websites.Even a non technical person can cretae sites using its CMS
How to include a file to a php page?
How to select a database?mysql_select_db($db_name);
How to execute an sql query? How to fetch its result ?
Write a program using while loop
How we can retrieve the data in the result set of MySQL using PHP?
ARRAY FUNCTONS()
How to include a file to a php page?
We can include a file using "include() " or "require()" function with file path as its parameter.
What's the difference between include and require?
-If the file is not found by require(), it will cause a fatal error and halt the execution of the script.
-If the file is not found by include(), a warning will be issued, but execution will continue.
What is the difference between Session and Cookie?
The main difference between sessions and cookies is that sessions are stored on the server, and cookies are stored on the user's computers in the text file format. Cookies can't hold multiple variable while session can hold multiple variables..We can set expiry for a cookie,The session only remains active as long as the browser is open.Users do not have access to the data you stored in Session,Since it is stored in the server.Session is mainly used for login/logout purpose while cookies using for user activity tracking
How to create a session?
Create session : session_start();
Set value into session : $_SESSION['USER']=user;
Set value into session : $_SESSION['USER']=user;
Set value into session : $_SESSION['email']=user@gmail.com;
How to create a mysql connection?
mysql_connect(servername,username,password);How to select a database?mysql_select_db($db_name);
How to execute an sql query? How to fetch its result ?
$my_qry = mysql_query("SELECT * FROM `users` WHERE `u_id`='1'; ");
$result = mysql_fetch_array($my_qry);
echo $result['First_name'];
$my_qry = mysql_query("SELECT * FROM `users` WHERE `u_id`='1'; ");
while($result = mysql_fetch_array($my_qry))
{
echo $result['First_name'.]."<br/>";
}
while($result = mysql_fetch_array($my_qry))
{
echo $result['First_name'.]."<br/>";
}
- 1. mysql_fetch_row
- 2. mysql_fetch_array
- 3. mysql_fetch_object
- 4. mysql_fetch_assoc
What is the difference between explode() and split() functions?
-Looping in php
<?php
for($i=0;$i<5;$i++)
{
for($j=0;$j<(5-$i);$j++)
{
echo("*");
}
for($k=0;$k<=(2*$i);$k++)
{
echo("  ");
}
for($m=0;$m<(5-$i);$m++)
{
echo("*");
}
echo("<br>");
}
// ***** *****
// **** ****
// *** ***
// ** **
// * *
?>
<?php
for($i=1;$i<6;$i++)
{
for($j=1;$j<=$i;$j++."<br>")
{
echo("*");
}
echo("<br>");
}
// *
// **
// ***
// ****
// *****
?>
<?php
for($i=1;$i<6;$i++)
{
for($j=5;$j>=$i;$j--)
{
echo("*");
}
echo("<br>");
}
// *****
// ****
// ***
// **
// *
?>
<?php
$a=1;
while ($a<=15)
for($i=1;$i<6;$i++)
{
for($j=1;$j<=$i;$j++."<br>")
{
$c=$a*2;
echo("$c");
$a++;
}
echo("<br>");
}
// 2
// 46
// 81012
// 14161820
// 2224262830
?>
Split function splits string into array by regular expression. Explode splits a string into array by string.
Write down the code for save an uploaded file in php.
if ($_FILES["file"]["error"] == 0)
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
Difference between var_dump() and print_r()
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
Difference between var_dump() and print_r()
The
var_dump function displays structured information about variables/expressions including its type and value. Arrays are explored recursively with values indented to show structure. It also shows which array values and object properties are references.
The
print_r() displays information about a variable in a way that's readable by humans. array values will be presented in a format that shows keys and elements. Similar notation is used for objects.
Example:
$obj = (object) array('qualitypoint', 'technologies', 'India');
var_dump($obj) will display below output in the screen.object(stdClass)#1 (3) {
[0]=> string(12) "qualitypoint"
[1]=> string(12) "technologies"
[2]=> string(5) "India"
}
And,
print_r($obj) will display below output in the screen.stdClass Object (
[0] => qualitypoint
[1] => technologies
[2] => India
)
How to strip whitespace (or other characters) from the beginning and end of a string ?
The trim() function removes whitespaces or other predefined characters from both sides of a string.
How to redirect a page in php?
The following code can be used for it, header("Location:index.php");
How to find the length of a string?
strlen() function used to find the length of a string.
What is the difference between mysql_fetch_array() and mysql_fetch_assoc() ?
mysql_fetch_assoc function Fetch a result row as an associative array, While mysql_fetch_array() fetches an associative array, a numeric array, or both
What is mean by an associative array?
Associative arrays are arrays that use named keys that you assign to them.
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
What is the use of "enctype" attribute in a html form?
The enctype attribute determines how the form-data should be encoded when submitting it to the server. We need to set enctype as "multipart/form-data" when we are using a form for uploading files.
How send email using php?
To send email using PHP, you use the mail() function.This mail() function accepts 5 parameters as follows (the last 2 are optional). You need webserver, you can't send email from localhost. eg : mail($to,$subject,$message,$headers);
-MD5 PHP implements the MD5 hash algorithm using the md5 function,
eg : $encrypted_text = md5 ($msg);
eg : $encrypted_text = md5 ($msg);
-session_start() and session_destroy()
session_start();
$_SESSION["name"]=$user;
$_SESSION["pwd"]=$pass;
session_start();
$user=$_SESSION["name"];
$pass=$_SESSION["pwd"];
echo$user;
echo$pass;
session_unset();
session_destroy();
-Multiple string control methods..
- string_reverse
<?php
$name="abc";
$final=strrev($name);
echo($final);
//cba
?>
- string-replace
<?php
$name="welcone in our website";
$final=str_replace("welcone","welcome",$name);
echo($final);
//welcome in our website
?>
- string_position
<?php
$name="welcome in our website";
$final=strpos($name,"i");
echo($final);
//8
?>
- string_length
<?php
$fname="abc";
$flen=strlen($fname);
echo $flen;
//3
?>
- string_implode
<?php
$language=array("java","php","dotnet","android");
$connector=implode("@",$language);
echo("$connector");
//java@php@dotnet@android
?>
- string_explode
<?php
$language="java@php@dotnet@android";
$disconnect=explode("@",$language);
$element=count($disconnect);
echo($element); //4
for($i=0;$i<$element;$i++)
{
echo($disconnect[$i]."<br>");
}
//java
//php
//dotnet
//android
?>
- str_to_upper
<?php
$fname="abc";
$lname="def";
$name=$fname.$lname;
$final=strtoupper($name);
echo($final);
//ABCDEF
?>
- str_to_lower
<?php
$fname="abc";
$lname="def";
$name=$fname.$lname;
$final=strtolower($name);
echo($final);
//abcdef
?>
- str_to_ucwords
<?php
$name="welcome In OuR weBSite";
$final=ucwords($name);
echo($final);
//Welcome In OuR WeBSite
?>
for($i=0;$i<5;$i++)
{
for($j=0;$j<(5-$i);$j++)
{
echo("*");
}
for($k=0;$k<=(2*$i);$k++)
{
echo("  ");
}
for($m=0;$m<(5-$i);$m++)
{
echo("*");
}
echo("<br>");
}
// ***** *****
// **** ****
// *** ***
// ** **
// * *
?>
<?php
for($i=1;$i<6;$i++)
{
for($j=1;$j<=$i;$j++."<br>")
{
echo("*");
}
echo("<br>");
}
// *
// **
// ***
// ****
// *****
?>
<?php
for($i=1;$i<6;$i++)
{
for($j=5;$j>=$i;$j--)
{
echo("*");
}
echo("<br>");
}
// *****
// ****
// ***
// **
// *
?>
<?php
$a=1;
while ($a<=15)
for($i=1;$i<6;$i++)
{
for($j=1;$j<=$i;$j++."<br>")
{
$c=$a*2;
echo("$c");
$a++;
}
echo("<br>");
}
// 2
// 46
// 81012
// 14161820
// 2224262830
?>
ARRAY FUNCTONS()
| array_pop() | Deletes the last element of an array |
| array_push() | Inserts one or more elements to the end of an array |
| array_replace() | Replaces the values of the first array with the values from following arrays |
| array_reverse() | Returns an array in the reverse order |
| array_search() | Searches an array for a given value and returns the key |
| array_sum() | Returns the sum of the values in an array |
| array_unique() | Removes duplicate values from an array |
| sort() | Sorts an indexed array in ascending order |
| rsort() | Sorts an indexed array in descending order |
| count() | Returns the number of elements in an array |
Comments
Post a Comment