PHP Forum Stuff
PHP Forum Stuff
The example here means if the field "username" in the form is set to some value then only
run the script in brackets.
If the field is not set with some value and is empty then skip running the script.
Isset is usually used to check if user has entered some value to the variable/field in the form.
see less
Q. Can isset() function be used to redirect php page to another php page?
The isset() function is used when there are functions to be performed if any input type is set. We
always use isset with loops. E.g.
if(isset($_POST["submit"]){ -- code --}.
It cannot redirect php pages to another but can be used as a condition to redirect. E.g.
if(isset($_POST["submit"]){ header("location:home.php"); exit; }
This code will redirect the web page if the submit button is set to home.php page.
see less
Q. How can a session variable be defined in one PHP file and transferred to another PHP file? Can
multiple PHP files share the same set of session variables and be in the same session?
A session creates a file in a temporary directory on the server where registered session
variables and their values are stored.
This data will be available to all pages on the site during that visit. The location of the
temporary file
is determined by a setting in the PHP.ini file called session.save_path.
When a PHP script wants to retrieve the value from a session variable, PHP automatically
gets the unique session identifier string from the PHPSESSID cookie and then looks in its
temporary directory for the file bearing that name.
Session variables are stored in associative array called $_SESSION[].
These variables can be accessed during lifetime of a session.
Yes, multiple files can access the same set of session variables by initializing session_start() function
at start of file.
see less
Q. What is the use of $_REQUEST variable?
The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.The
PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and
POST methods.
Q. In HTML forms, what is the difference between using the GET method versus POST? Practically
when should we use POST and GET?
The basic difference in using GET and POST method is the security.
GET method will send the variables through a URL which is visible to users and can be
manipulated. POST method will send the values from one page to another in a hidden way
and this is not visible to users.
You should use POST when you are using forms in your web page, i.e inserting the values in
the database. POST method is the best method in that case.
GET should be used for searching purposes. E.g. whenever you search something on Google,
the values you have entered in the search box is visible in the URL of the web page. i.e these
queries don't change any values in the database, they just fetch the values. So GET method
should be used.
Moreover you cannot bookmark the URL if you are using POST request but it is possible
using GET method. Suppose you find awesome search result at Google for some word and
want to save that link. You will only be able to save if they are using GET method otherwise
you have to type the word again and then get search result, you cannot directly jump to the
link if it is not passing value as GET method.
see less
Q. How to check whether the data is inserted in the database?
Store the result of mysqli_query in a variable and if that returns true, the data is inserted otherwise
not. E.g.
$result= mysqli_query($con, $sql);
if ($result) { echo "data inserted successfully "; } else { echo "Error in inserting the records
".mysql_error($con)."; }
see less
Q. Please explain the line: $result= mysqli_query($con, $sql) or die("Error in
connection:".mysqli_error($con));
mysqli_query($con, $sql) runs the query you saved in $sql variable on port number of sql server
saved in $con variable.
If the script runs successfully then it saves the result in $result variable.
If the script does not run successfully the mysqli_error() displays the error, the die() function
prints a message and exits the current script.
see less
Q. How to stop the execution of rest of PHP script in the code?
We use the exit() or die() function to stop the execution of the remaining PHP script.
Q. Where do we need to write the mysqli_connect function?
Make a separate file for the connection and include that file in all the web pages where you need
database connection.
Q. What's the difference between "$var1 == $var2" and "$var1 === $var2"?
"==" returns true if the value of $var1 is equal to value of $var2. For example: $var = "1"; $var2 = 2;
$var == $var2 returns true even if $var is string and $var2 is integer. However, "===" returns true
only if the value as well as the datatype of $var1 and $var2 are same. So $var1 === $var2 returns
false.
Q. What is the purpose of break statement?
Break terminates the loop and transfers execution to the immediately following statement.
Q. What is the purpose of continue statement?
Continue causes the loop to skip the remainder of its body for the current iteration and immediately
move on to the next iteration of the loop.
Q. Can we assign default values to a function's parameters?
Yes, we can set a parameter to have a default value which will be used in case no value is passed for
the variable during function call.
function demo($number = 1){
-- code --
}
demo();
In this example, $number would have value 1 as no value is passed to the $number variable during
function call.
see less
Q. How to get the number of total elements in an array?
The PHP count() function is used to count total elements in the array.
Q. What is NULL?
The special NULL value represents a variable with no value. NULL is the only possible value of type
null. A variable is considered to be null if:
<?php
$var = NULL;
?>
see less
Q. How to pass an array using function?
In PHP, you can pass an array the same way you'd pass a variable to a function. Example:
function passArray($arr){
foreach($arr as $value) echo $value;
}
$arr = array(1, 2, 3);
passArray($arr);
see less
Q. What is the difference between constant and string data type?
String data type takes the number of characters as its value and the value can be changed during
execution or some other characters could be assigned to this variable having string data type. In case
of constants, its value can't be changed once assigned. It will be the same.
Q. What is the difference between single-quoted and double-quoted strings in PHP?
There is no difference while declaring a string in single quotes and double quotes. But for displaying
string there is a difference in single quotes and double quotes.
Single quoted strings (' Single quotes ') display everything. PHP won't interpret variables and
escape sequence (\n \r) for strings in single quotes and are fast as PHP won't use additional
processing for interpretation.
Double quoted strings (" Double quotes ") can interpret variables and escape sequence(\n
\r). Ex: $s = "Internshala"; echo 'Welcome to $s.'; // Welcome to $s. echo "Welcome to $s.";
// Welcome to Internshala.
see less
Q. How can we define constant in PHP?
In PHP define() function is used for defining constant. The arguments to define function are
name: Specifies the name of the constant
value: Specifies the value of the constant
case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false
For example:
define("A", "Internshala");
echo A; //Internshala
see more
Q. What are Magic constants?
PHP provides a large number of predefined constants to any script which it runs. There are five
magical constants that change depending on where they are used.
__LINE__: -The current line number of the file.
__FILE__: -The full path and filename of the file.
__FUNCTION__:-The function name.
__CLASS__: -The class name.
__METHOD__:-The class method name.
see less
Q. What is the difference between require and require_once?
require_once()
It includes a specified file. a file that has already been included, will not be included again. It will
produce a fatal error if it fails to find the file and stops the execution.
require()
It includes a specified file. It will produce a fatal error if it fails to find the file and stops the execution
Q. What is the use of include_once?
It includes a specified file. a file has already been included, it will not be included again. It will
produce a warning if it fails to find the file and execute the remaining scripts.
Q. What are rules for naming a PHP variable?
Variable names must begin with a letter or underscore "_" character. In PHP, numbers, letters,
underscores are allowed for a variable name but you cannot use characters like + , - , % , ( , ) . & , etc.
Q. What is the difference between print and echo?
Echo and print are more or less the same. They are both used to output data to the screen. The
difference is echo has no return value while print has a return value of 1 so it can be used in
expressions. print can just print one variable, while echo can print multiple variables separated by a
comma (,) in the syntax.
Q. What are different ways of adding comments in PHP?
We can add comment in 3 ways
1) We can use double slashes(//) for adding single line comment.
2) We can use hash (#) for adding single line comment.
3) We can add multiline comment by adding slash start and then comment and then start and
slash(/* */).
Q. How can I run my PHP files in WAMPServer?
First of all, keep all your PHP code files in a separate folder and name it something relevant to your
project. Place the folder in the www folder inside the wamp64(or wamp) folder in your installation
drive(mostly C drive). Then open your favourite web browser and type localhost in the address bar.
You will be directed to a page showing various information regarding PHP and WAMP Server. You
can run your code from this page by typing in the name of your project folder after localhost. If the
name of your project folder is Internshala, you can run it by typing the following address in the
address bar - localhost/Internshala
see less
Q. How to debug PHP code?
The browser notifies the problem through a warning or a notice or an error. This notification consists
of the warning/notice/error that has occurred and also specifies the line number in the PHP file
where this has occurred. You can just go to that line and check your code and then rectify it.
Q. Explain the difference b/w static and dynamic websites?
In static websites, the content is fixed. Every visitor to that page will be greeted by the exact same
text, multimedia design or video every time he visits the page until you alter that page's source code.
In dynamic websites however, content can be changed at run time. Dynamic website has a backend
written in a language like PHP, Python, Ruby, etc. These websites can take user input and change
their content based on databases, external APIs, etc.
Q. Is PHP a case sensitive language?
No, PHP is partially case sensitive. The keywords are not case sensitive but variable names and
constant names are case sensitive.
Q. If I am using any other text editor instead of Netbeans, then how can I run my PHP code?
Install wamp/xampp. Then, put your project folder inside "path/wamp/www" in case of wamp,
"path/xampp/htdocs" in case of xampp.Then run wamp/xampp and type
"localhost/your_project_name/filename.php" in the address bar of any browser.