PHP Questions

Q:

What is the functionality of the function strstr and stristr?

Answer

strstr() returns part of a given string from the first occurrence of a given substring to the end of the string.
For example:
strstr("user@example.com","@") will return "@example.com".

stristr() is idential to strstr() except that it is case insensitive.

Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2269
Q:

Explain how to create random passwords.

Answer

Generating random passwords in PHP can be done in multiple ways depending on how much security the application demands:-


Md5 function can be passed one parameter as uniqid() function which in turn generates a random number. Passing the parameter as TRUE in uniqid() adds additional uniqueness.


<?php
   $passwd = md5(uniqid(rand(), true));
   Echo $passwd;
?>

Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2229
Q:

How do you call a constructor for a parent class?

Answer parent::constructor($value)
Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2223
Q:

What is a Session?

Answer

A session is a logical object created by the PHP engine to allow you to preserve data across subsequent HTTP requests. Sessions are commonly used to store temporary data to allow multiple PHP pages to offer a complete functional transaction for the same visitor.

Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2211
Q:

What is the difference between using copy() and move() function in php file uploading?

Answer

Copy() makes a copy of the file. It returns TRUE on success. It can copy from any source to destination. Move simply Moves the file to destination if the file is valid. While move can move the uploaded file from temp server location to any destination on the server. If filename is a valid upload file, but cannot be moved for some reason, no action will occur.

Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2206
Q:

What’s the difference between include and require?

Answer It’s how they handle failures. 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.
Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2135
Q:

Explain how to store the uploaded file to the final location.

Answer

A HTML form should be build before uploading the file. The following HTML code is used for selecting the file that is to be uploaded. The type attribute of input must be “file”.


<form enctype="multipart/form-data" action="uploader.php" method="POST">


<input type="hidden" name="MAX_FILE_SIZE" value="100000" />


Choose a file to upload: <input name="uploadedfile" type="file" /><br />


<input type="submit" value="Upload File" />


</form>


At the time of executing uploader.php, the uploaded file will be stored in a temporary storage are on the webserver. An associative array $_FILES['uploadedfile']['name'] is used for uploading. The ‘name’ is the original file that is to be uploaded. Another associative array $_FILES['uploadedfile']['tmp_name'] is used for placing the uploaded file in a temporary location on the server and the file should be empty and should exist with the tmp_name.


The following code snippet is used for uploading the file.


$target_path = "uploads/"; // the target location of the file


$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);


if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) 


{


       echo "The file ". basename( $_FILES['uploadedfile']['name']). 


        " has been uploaded";


}


else


{


      echo “There was an error while uploading the file”;


}


The function move_uploaded_file()is used to place the source file into the destination folder, which resides on the server.


If the uploading is successful, the message “The file filename has been uploaded. Otherwise the error message “There was an error while uploading the file” would be displayed.

Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2134
Q:

What is the maximum size of a file that can be uploaded using PHP and how can we change this?

Answer

By default the maximum size is 2MB. and we can change the following
setup at php.iniupload_max_filesize = 2M

Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2133