PHP

Interview Questions on PHP

41. How to get a character in particular position of string in php?

Syntax :- $string_name(index_number);
Ex :-
$string = 'abcdef';
echo $string[0];                 // a
echo $string[3];                 // d

42. How to get index of character/substring of string in php?

Ex :- strops(“main string”,”sub string”);
<?php
echo strpos("I love php, I love php too!","php");
?>

43. How to connect to a database?

// Create connection
$conn = new mysqli($servername, $username, $password);

44. How to execute mysql commands in php?

$sql= “write sql command here ”;
$conn->query($sql);

45. How to assign default values for argument?

Using get :
GET requests can be cached
GET requests remain in the browser history

46. What is the difference b/w get and post?

Using get :
GET requests can be cached
GET requests remain in the browser history

Using post :
POST requests are never cached
POST requests do not remain in the browser history

47. How to access form input values in php?

Use $_POST or $_GET
Ex :-  $_POST[‘form_input_name’];

48. How to know total number of elements in array?

Use count();
Ex :- 
<?php
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
$result = count($a);
// $result == 3
?>

49. How to encrypt to md5 in php?

Use md5 function
Ex :-
<?php
$str = 'apple';
if (md5($str) === '1f3870be274f6c49b3e31a0c6728957f') {
    echo "Would you like a green or red apple?";
}
?>

50. How to upload file in php?

Syntax :-  move_uploaded_file ( string $filename , string $destination );
Ex :- 
<?php
        $uploads_dir = '/uploads';
        $tmp_name = $_FILES["fileToUpload"]["tmp_name"];
        move_uploaded_file($tmp_name, "$uploads_dir/filename ");
?>

www.000webhost.com