Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Common PHP function with code

Answer»

Common PHP function with code
Below are the diferent PHP function with example and output
(1)is_number
it is used to check value is numeric or not
Code:-
< ?php
if(is_numeric("crackyourinterview"))
{
echo "true";
}
else
{
echo "false";
}
?>
Output:-False

(2)number_format
used to separate digit and decimal parts
Code
< ?php
echo number_format(1124563);
? >
Output:-1,124,563

(3)rand
Used to generate a random NUMBER
Code:-
< ?php
echo rand();
?>
Output:- Random Number

(4)round
Round off a number with decimal points to NEAREST WHOLE number
Code:-
< ?php
echo round(56.23);
?>
Output:-56

(5)SQRT
Return the square root of a number
Code:-
< ?php
echo sqrt(81);
?>
Output:-9

(5)cos
Return the cosine
Code:-
echo cos(45);
?>
Output:-0.52532198881773

(6)sin
Return the sine
Code:-
< ?php
echo sin(45);
?>
Output:-0.85090352453412

(7)tab
Return the tangent
Code:-
< ?php
echo TAN(45);
?>
Output:-1.6197751905439

(8)pi
Returns the value of PI
Code:-
< ?php
echo pi();
?>
Output:-3.1415926535898

2.

PHP Loop For, ForEach, While, Do While Example

Answer»

PHP Loop For, ForEach, While, Do While EXAMPLE
Loop is an ITERATIVE Control Structure that execute same number of code a number of TIMES until a certain condition is satisfied.
(1)Below is the example of For loop
< ?php
for ($a = 0; $a < 10; $a++){
$msgval = 10 * $a;
echo "The output of 10 * $i is $msgval ";
}
?>


(2)Below is the example of Foreach
< ?php
$techdetails = array("dotnet","php","sqlserver","java","web");
foreach($techdetails as $array_values){
echo $array_values;
}
?>


(3)Below is the example of While loop
< ?php
$a = 0;
while ($a < 5){
echo $a + 1;
$a++;
}
?>


(4)Below is the example of Do While
< ?php
$a = 5;
do{
echo "$a is";
}
while($a < 5);
?>

3.

How to connect MongoDB from php code?

Answer»

How to connect MongoDB from PHP code?
Below is the code to connect MongoDB database from PHP. And if database is not there with that name it will automatically create that database in MongoDB. Below is the syntax

< ?php
// connect to mongodb with below code
$m = new MongoClient();
echo "First step CONNECTED to database";
// select a database with given below COMMAND
$db = $m->mytestdb;
echo "Database mytestdb selected";
?>


after executing above code in php we will GET below two lines as output:-

First step Connected to database
Database mytestdb selected

4.

Method to create a public static method with example in PHP?

Answer»

Method to create a public STATIC method with example in PHP?
Static method is one of a member of a class which is called DIRECTLY by using the class NAME WITHOUT creating any instance of class. And in PHP we create a static method by using static keyword. Below is the syntax or CODE to create a static method:-

class X
{
public static function testHello()
{
echo 'hello Static method';
}
}

X::testHello();

5.

Write down code to add 301 redirects in PHP?

Answer»

Write down code to add 301 REDIRECTS in PHP?
We can add 301 REDIRECT in PHP by using below code in PHP:-

HEADER("HTTP/1.1 301 Moved Permanently");
header("Location: /option-a");
exit();

6.

Number of time a value occur in array in PHP

Answer»

Below CODE helps you to get number of time a value occur in given array it will product how many time a values repeat in given array as PER below example


&LT; ?php
$arr_values = array(3,2,3,5,3,2,4,2,2,1,1,5,3,2,2,2,4,5);
$value_count = array_count_values($arr_values);
print_r($value_count);
?&GT;

7.

Code to connect to mysql in php

Answer»

Below is the code to connect MYSQL in php. We have to PUT USERNAME and PASSWORD. And database name in place of DB_Name


&LT; ? php
$dbconnect = mysql_connect("HOST", "user_name", "pwd");
mysql_select_db("DB_NAME",$dbconnec);
?>

8.

Code to break username and hostname from emailaddress

Answer»

Below CODE will helps you to split USERNAME and hostname in two parts. Here in below example we use explode keyword to split emailaddress


< ?php
$TestEmailAddress = "questionsgetproductprice.com";
$TestArray = explode("",$TestEmailAddress);
print_r($TestArray);
?>


And we GET below output from above code
[0]=questions
[1]=getproductprice.com

9.

How to gernate random value in PHP

Answer»

Below CODE will helps you to get a random NUMBER from 1 to 500. This can alos helps in doing lottery system as WELL as in same game programming.


< ?PHP
$RandNumb = range(1,500);
$NumberVal = array_rand($RandNumb);
ECHO "The number is: $NumberVal n n";
?>

10.

Different way to include variable in double quotes in php

Answer»

Here in below example i have used variable in double QUOTES and CREATE three FORMATS to use variable
(1)IN First row i have simple used variable in echo
(2)In second and third i have used variable inside string
And code is as below

< ?php
$Question = 'GetProductPrice';
echo "$Question .com is good website. ";
echo "Latest Interview Questions on ${Question}.com ";
echo "Good Interview Questions on {$Question}.com.";
? >


output of above is
Getproductprice .com is good website
Latest Interview Questions on Getproductprice.com
Good Interview Questions on Getproductprice.com

11.

Remove whitespace from end of string in PHP

Answer»

Here in below code we have used keyword CHOP it is used to remove SPACE from end of string. One more thing is that it will remove trailing not leading space to remove leading space we need trim.

< ?php
$CONTENTS = " getproductprice.com ";
echo " n";
echo "&AMP;lt" . chop($contents) . ">";
&lt getproductprice.com >
&lt getproductprice.com>
? >

12.

something about array creation sorting searching in php

Answer»

here is the syntax for array creation,search ,loop from the array in PHP

******syntax to create and array in Php******
< ?php
$myarray = array(1,2,3,4,5,6,7,8,9,10);
or
$myarray = RANGE(1,10);
or
$myarray = range(a,z);
? >


******syntax to VIEW item of array******
< ?php
$values = array("a","b","c","d");
print_r($values);
? >


******syntax of loop foreach to print a to z******
< ?php
$ALPHABET = range("A","Z");
foreach($alphabet as $letter) { echo "The letter $letter n"; }
? >


*******syntax to search item in array*******
< ?php
$values = array("a","b","c","d");
$newvalue = "cr";
if (in_array($newvalue,$values)) { echo "$value is already in the array!"; }
? >


******syntax for shuffle or random item from a to z******
< ?php
$values = range("A","Z");
shuffle($values);
print_r($values);
? >


******syntax for sorting in array******
< ?php
$myworld= range("A","Z","O","P","L","k");
arsort($myworld);
print_r($myworld);
? >


******syntax for multi-dimensional array******
< ?php
$multiarray = array(array(5,4,3,2,1),array(7,8,2,9,10));
array_multisort($multiarray[0],SORT_ASC);
print_r($multiarray);
? >


******syntax for get count of element in array ******
< ?php
$values = array(1,2,3,4,5,6,7,8);
$ELEMENTS = array_unshift($values,1);
echo "No of elements: $elements n n";
print_r($values);
? >


******syntax for number of times a vallue appears in array ******
< ?php
$values = array(a,b,c,d,s,a,a,c,d,f,a,s,a,a,b,d,d,d);
$most_popular = array_count_values($values);
print_r($most_popular);
? >


******syntax to find size of an array *******
< ?php
$values = range("A","Z");
echo count($values);
? >


******syntax to find randome number from array ******
< ?php
$number = range(1,100);
$rannumber = array_rand($number);
echo "Number is: $rannumber n n";
? >


******syntax to remove first value from array ******
< ?php
$values = array(a,b,c,d,e,f,g);
$first = array_shift($values);
echo "First value is: $first";
print_r($values);
? >

******syntax to invert array ******
< ?php
$values = array("b","a","c");
print_r($values);
$values = array_flip($values);
print_r($values);
? >

13.

difference between htmlentities() and htmlspecialchars()

Answer»

In PHP htmlspecialchars picks , single QUOTE , DOUBLE quote and ampersand. On the other hand htmlentities do to translates all occurrences of character sequences that have some DIFFERENT meaning in HTML.