Elroi PHP Fresher Interview Questions with Answers–PART 5

Hi everyone with this post we’re going to wind-up Elroi PHP Fresher Interview Questions with Answers.[Also see Elroi PHP Fresher Interview Questions with Answers – PART 1,Elroi PHP Fresher Interview Questions with Answers – PART 2,Elroi PHP Fresher Interview Questions with Answers – PART 3, Elroi PHP Fresher Interview Questions with Answers–PART 4Let us discuss the remaining questions in this post;

Elroi PHP Fresher Interview Questions with Answers–PART 5

26.What is count? When do you use it?

The count() function is used to count the elements of an array or the properties of an object.

SYNTAX:
count(array_name, mode)
PARAMETERS:

            Name        Description       Required /
Optional
            Type
array_name Specifies the array or object to count. Required Array
mode Sets the mode of the function.
Possible values :
COUNT_RECURSIVE (or 1) : here the count() function counts the array recursively. This is useful for counting all the elements of a multidimensional array. 
The default value is 0.
Optional Integer

RETURN VALUE:
The number of elements in array_name.

27.What is the function to find length of the string?

The strlen() function returns the length of a string.
SYNTAX:
strlen(string)
PARAMETERS:
string-Specifies the string to check
RETURN VALUE:
Returns the length of a string on success, and 0 if the string is empty.

28.Write program for while loop?

SAMPLE CODE:
<?php
$i = 0;
while ($i < 5){
echo $i + 1 . “<br>”;
$i++;
}
?>
SAMPLE OUTPUT:
1
2
3
4
5

29.What is inheritance?write program for inheritance?

=>When a class is defined by inheriting existing function of a parent class then it is called inheritance.
=>The child class will inherit all or few member functions and variables of a parent class.
=>PHP class definitions can optionally inherit from a parent class definition by using the extends clause.
SYNTAX:
class Child extends Parent {
<definition body>
}
SAMPLE CODE:
//The parent class
class Car {
// Private property inside the class
private $model;

//Public setter method
public function setModel($model)
{
$this -> model = $model;
}

public function hello()
{
return “Hello! I am a <i>” . $this -> model . “</i><br />”;
}
}

//The child class inherits the code from the parent class
class SportsCar extends Car {
//No code in the child class
}

//Create an instance from the child class
$sportsCar1 = new SportsCar();

// Set the value of the class’ property.
// For this aim, we use a method that we created in the parent
$sportsCar1 -> setModel(‘Mercedes Benz’);

//Use another method that the child class inherited from the parent class
echo $sportsCar1 -> hello();

SAMPLE OUTPUT:
Hello!I am a Mercedes Benz

30.What is the new version of phpuse now?when it was launched?

=>The latest version of php is PHP 7.2.
=>It was launched on 30th November 2017.
=>It is supported until 30th November 2020.
=>Based on performance benchmarks run by Phoronix, PHP 7.2 runs up to 13% faster than PHP 7.1, or 20% faster than PHP 7.0. And compared to PHP 5.6, PHP 7.2 is over 250% faster.

31.What is encryption and decryption?

Encryption-

Encrypts given data with given method and key, returns a raw or base64 encoded string
SYNTAX:
string openssl_encrypt ( string $data , string $method , string $key [, int $options = 0 [, string $iv = “” [, string &$tag = NULL [, string $aad = “” [, int $tag_length = 16 ]]]]] )
PARAMETERS:
=>data-The plaintext message data to be encrypted.
=>method-The cipher method. For a list of available cipher methods, use openssl_get_cipher_methods().
=>key-The key.
=>options-options is a bitwise disjunction of the flags OPENSSL_RAW_DATA and OPENSSL_ZERO_PADDING.
=>iv-A non-NULL Initialization Vector.
=>tag-The authentication tag passed by reference when using AEAD cipher mode (GCM or CCM).
=>aad-Additional authentication data.
=>tag_length-The length of the authentication tag. Its value can be between 4 and 16 for GCM mode.
RETURN VALUE:
Returns the encrypted string on success or FALSE on failure.

Decryption-

Decrypts the data and returns the unencrypted data.
SYNTAX:
string mcrypt_decrypt ( string $cipher , string $key , string $data , string $mode [, string $iv ] )
PARAMETERS:
=>cipher-One of the MCRYPT_ciphername constants, or the name of the algorithm as string.
=>key-The key with which the data was encrypted. If the provided key size is not supported by the cipher, the function will emit a warning and return FALSE
=>data-The data that will be decrypted with the given cipher and mode. If the size of the data is not n * blocksize, the data will be padded with ‘\0’.
=>mode-One of the MCRYPT_MODE_modename constants, or one of the following strings: “ecb”, “cbc”, “cfb”, “ofb”, “nofb” or “stream”.
=>iv-Used for the initialization in CBC, CFB, OFB modes, and in some algorithms in STREAM mode. If the provided IV size is not supported by the chaining mode or no IV was provided, but the chaining mode requires one, the function will emit a warning and return FALSE.
RETURN VALUE:
Returns the decrypted data as a string or FALSE on failure.

32.What function to upload image in php?

=>In your “php.ini” file, search for the file_uploads directive, and set it to On:
file_uploads = On
=>If the value assigned to the input’s name attribute in uploading form was file, then PHP would create following five variables −
$_FILES[‘file’][‘tmp_name’] − the uploaded file in the temporary directory on the web server.
$_FILES[‘file’][‘name’] − the actual name of the uploaded file.
$_FILES[‘file’][‘size’] − the size in bytes of the uploaded file.
$_FILES[‘file’][‘type’] − the MIME type of the uploaded file.
$_FILES[‘file’][‘error’] − the error code associated with this file upload.
=>SAMPLE CODE:
<?php
$target_dir = “uploads/”;
$target_file = $target_dir . basename($_FILES[“fileToUpload”][“name”]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST[“submit”])) {
$check = getimagesize($_FILES[“fileToUpload”][“tmp_name”]);
if($check !== false) {
echo “File is an image – ” . $check[“mime”] . “.”;
$uploadOk = 1;
} else {
echo “File is not an image.”;
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo “Sorry, file already exists.”;
$uploadOk = 0;
}
// Check file size
if ($_FILES[“fileToUpload”][“size”] > 500000) {
echo “Sorry, your file is too large.”;
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != “jpg” && $imageFileType != “png” && $imageFileType != “jpeg”
&& $imageFileType != “gif” ) {
echo “Sorry, only JPG, JPEG, PNG & GIF files are allowed.”;
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo “Sorry, your file was not uploaded.”;
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES[“fileToUpload”][“tmp_name”], $target_file)) {
echo “The file “. basename( $_FILES[“fileToUpload”][“name”]). ” has been uploaded.”;
} else {
echo “Sorry, there was an error uploading your file.”;
}
}
?>

References:
https://www.w3resource.com/php/function-reference/count.php
https://www.guru99.com/php-loop.html
https://www.tutorialspoint.com/php/php_object_oriented.htm
https://phpenthusiast.com/object-oriented-php-tutorials/inheritance-in-object-oriented-php
https://www.quora.com/Whats-the-latest-version-of-php