PHP Questions

Q:

Explain when to use GET or POST. Provide example for both the ways.

Answer

Both GET and POST are used to collect data from a form. However, when security is desired $_POST should be used. When the $_ POST variable is used, all variables used are NOT displayed in the URL. Also, they have no restrictions to the length of the variables. GET should be used when the interaction with the form is more like a question. For e.g. firing a query etc. POST should be used more often when the interaction is more like an order or the interaction changes the state of the resource.


POST example:


<form action="sample.php" method="post">


Name: <input type="text" name="name" />


Age: <input type="text" name="age" />


<input type="submit" />


</form>


On clicking submit the URL resembles to: https://www.mysamplesite.com/sample.php


The sample.php file can be used for catching the form data using $_POST 


Hello <?php echo $_POST["name"]; ?>.<br />


You are <?php echo $_POST["age"]; ?> years old!


 


GET EXAMPLE


<form action="sample.php" method="get">


Name: <input type="text" name="name" />


Age: <input type="text" name="age" />


<input type="submit" />


</form>


On clicking submit the URL resembles to : https://www.mysamplesite.com/sample.php?name=jim&age=37


The sample.php file can be used for catching the form data using $_GET 


Hello <?php echo $_GET["name"]; ?>.<br />


You are <?php echo $_GET["age"]; ?> years old!

Report Error

View answer Workspace Report Error Discuss

Subject: PHP - Technology

0 3995
Q:

How to create a basic text file in php?

Answer

$filename = "/home/user/guest/newfile.txt";
$file = fopen( $filename, "w" );
if( $file == false )
{
echo ( "Error in opening new file" );
exit();


}
fwrite( $file, "This is a simple test\n" );
fclose( $file );

Report Error

View answer Workspace Report Error Discuss

Subject: PHP - Technology
Job Role: IT Trainer

1 3984
Q:

Explain Superglobal variables in PHP .

Answer

To access the global data which is originating externally, the super globals are used. The superglobals are available as arrays. These superglobals represents the data from URLs, HTML forms, cookies, sessions and also the web server. For this purpose, $HTTP_GET_VARS, $HTTP_POST_VARS are used. The super globals are pretty good enough to be used in functions. The following are the list of super globals.


$_GET, $_POST, $_COOKIE;$_REQUEST, $_SERVER, $_SESSION, $_ENV, $_FILE


Another PHP Superglobal, called $GLOBALS; is available for persisting every variable with global scope.

Report Error

View answer Workspace Report Error Discuss

Subject: PHP - Technology

0 3980
Q:

Explain the working with directories using opendir(), readdirs(), closedir() along with examples.

Answer

Opendir():- It opens the directory. This function returns a directory stream on success and FALSE and an error on failure.


Syntax:


Opendir(directory, context)


Context is a set of options that can modify the behavior of a stream


Example: opens sample directory.


$dir = opendir("directory");


 


Readdir(): It returns an entry from a directory handle opened by opendir().


Syntax:


Readdir(dir_stream)


Example:


$file = readdir($dir);


 


closedir(): It closes a directory handle opened by opendir().


Syntax:


closedir(dir_stream)


Example:


$file = close($dir);

Report Error

View answer Workspace Report Error Discuss

Subject: PHP - Technology

0 3922
Q:

What is PHP’s configuration file called ?

Answer

PHP’s configuration file is called php.ini.

Report Error

View answer Workspace Report Error Discuss

Subject: PHP - Technology

6 3828
Q:

How can we extract string "abc.com" from a string "https://info@abc.com" using regular _expression of php?

Answer

We can use the preg_match() function with “/.*@(.*)$/” as the regular expression pattern.
For example:
preg_match("/.*@(.*)$/","https://info@abc.com",$data);
echo $data[1];
?>

Report Error

View answer Workspace Report Error Discuss

Subject: PHP - Technology

0 3799
Q:

Would I use print "$a dollars" or "{$a} dollars" to print out the amount of dollars in this example?

Answer In this example it wouldn’t matter, since the variable is all by itself, but if you were to print something like "{$a},000,000 mln dollars", then you definitely need to use the braces.
Report Error

View answer Workspace Report Error Discuss

Subject: PHP - Technology

0 3622
Q:

What is the difference between Notify URL and Return URL?

Answer

Notify URL and Return URL is used in Paypal Payment Gateway integration. Notify URL is used by PayPal to post information about the transaction. Return URL is used by the browser; A url where the user needs to be redirected on completion of the payment process.

Report Error

View answer Workspace Report Error Discuss

Subject: PHP - Technology

2 3566