isset() vs check if exists for a variable in PHP [duplicate] - php

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Best way to test for a variable's existence in PHP; isset() is clearly broken
(17 answers)
Closed 7 years ago.
How do the following two function calls compare to check if a variable exists for a GET call or a POST call ?
if(isset($_GET['var']))
if($_GET['var'])
Both do their job but sometimes without isset() i get a warning :
PHP Notice: Undefined index: var on at Line XX
Update
It was a $_GET not $GET. Also i know it has nothing to do with GET/POST. All i am discussing is variable check instead of HTTP method. Thanks.

$_GET[''] is usually retrieved from the browser, so if you had something like the following:
www.hello.com/index.php?var=hello
You could use:
if(isset($_GET['var'])) {
echo $_GET['var']; // would print out hello
}
Using isset() will stop the undefined index error, so if you just had www.hello.com/index.php for example then you would not see the error even though the var is not set.
Posts are usually when one page posts information to another, the method is the same but using $_POST[''] instead of $_GET['']. Example you have a form posting information:
<form method="post" action="anotherpage.php">
<label></label>
<input type="text" name="text">
</form>
In anotherpage.php to get the information would be something like:
$text = isset($_POST['text']);
echo $text; // would echo what ever you input into the text field on the other page
In a nut shell, just putting $_GET['name'] if name is not set you will get an error.
Using isset($_GET['name']) will check is the var name has any value before continuing.
Not sure if this is what you were after, but from the question this is my best guess at an answer

Related

Do I have to check every variable is set before echoing it? [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
I am currently in a process of writing my PHP script, and I am doing it with
E_ALL set in php and I can not allow ANY errors, even
PHP Notice: Undefined variable
And I have many places where i ECHO $myvariable without checking if it's empty, which is causing error mentioned above.
Is it just me or it's extremely stupid to do this for every variable that can be undefined:
if(!empty($myvariable)) {
echo $myvariable;
}
Is this only way to avoid these errors?
EDIT:
Question is not a duplicate, what you refereed to as duplicate has nothing to do with what i asked here.
Three possible solutions:
1st: initialize your var at the very beginning of your code (or method, or class, whatever you're doing)...
$var = "";
// Do stuff with $var
echo $var;
2nd: use a ternary operator
echo (isset($var)) ? $var : "";
3rd: use #
#echo($var);
It is a notice which gives you an insight that your code could produce unexpected results since you did not assign a value (constant or calculated).
You can avoid this error by checking if it's set using the isset function or set a value for that variable at the top of your script.

php script command line $_POST arguments [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
I have a php script which adds rows to a MySQL database, when I set variables in the script it works fine. As is the norm I want values to be entered via an html page. To test that I understood I could run my php script with arguments to simulate the $_POST values, similar to:
http://www.website.com/php/insert_client3.php?nickname='Bob'&emailaddress='bob#yahoo'
The script beginning is:
<?php
print_r($_POST);
var_dump($_POST);
// check for required fields
$nickname = $_POST['nickname'];
$emailaddress = $_POST['emailaddress'];
print_r and var_dump are returning empty arrays, is my approach wrong or syntax? I'm trying to build up html entry of data by starting with the database and working back
Any help appreciated...
Thanks
You're looking for $_GET and not $_POST. Variables passed in the query string are sent with an HTTP GET request, whereas HTTP POST request data is not visible in the URL.
<?php
print_r($_GET);
var_dump($_GET);
// check for required fields
$nickname = $_GET['nickname'];
$emailaddress = $_GET['emailaddress'];

What is the simplest method to POST a single on or off value (PHP) [duplicate]

This question already has answers here:
How can I detect unchecked checkbox with php?
(3 answers)
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
I have to pass either an ON or OFF value in php. Is a single checkbox to check whether a value is on or off the simplest method? I have:
<input type="checkbox" name="highlight">
and when it is posted, if it is checked I get the value 'on' but if it is not checked I get an error of 'Undefined index'
Now I have tried testing whether it is empty or isset but I would like to know if this is the correct method for passing a boolean value.
Unchecked boxes are simply not sent in POST|GET
You can just use the isset() function, its value is irrelevant:
if (isset($_POST['highlight'])) {
echo 'it was checked';
} else {
echo 'it was not checked';
}
You can do like this:
$cheeked = isset($_POST['highlight'])?1:0;
$cheeked will be 1 if checked and 0 if not.

how to fix error when url is directly entered with no search keywords? [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I am creating small search engine for job portal. I used following code for searching.
<form method="get" action="search.php">
<input type="text" name="keyword">
<input type="text" name="location">
</form>
when I search it sends input by url and displays the results.
e.g : url: localhost/jobportal/search.php?keyword=php&location=India
But if I directly open the page by entering url : "localhost/jobportal/search.php" in browser, it shows following error.
Notice: Undefined index: keyword in C:\xampp\htdocs\jobportal\search\index.php on line 384
Notice: Undefined index: location in C:\xampp\htdocs\jobportal\search\index.php on line 385
I know why this error is occurring I only need to know how to avoid this error.
Just add a check if they exists. Use empty. -
if(!empty($_GET['keyword'])) {
// use value
}
if(!empty($_GET['location'])) {
// use value
}
empty() will check if it is set and the value is not null or false.

Undefined Variable : kd_user [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
I just started coding so i dont know whats wrong with my code
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: kd_user
Filename: views/dokter.php
Line Number: 1
Controller
function read_user()
{
$this->load->model('dokter_model');
$data['datauser']=$this->dokter_model->read_user();
$this->load->view('dokter', $data);
}
Model
function read_user()
{
$q="SELECT a.*, b.*
FROM users a
LEFT JOIN dokter b ON a.kd_user=b.kd_user
WHERE a.kd_user='".$this->session->userdata('kd_user')."'";
$query=$this->db->query($q);
return $query->result();
}
Views
<p align="center">Selamat Datang <?php echo $kd_user;?></p>
Sorry for my poor english
Change :
$data['datauser']=$this->dokter_model->read_user();
to:
$data['kd_user']=$this->dokter_model->read_user();
In general, the error
Message: Undefined variable: kd_user
Means that you are referring to a variable (in this case, $kd_user), and it was never defined (you never initialized it and/or assigned a default value to it.)
Looking at your code, you are referring to kd_user in two places -- once in the associative array user_data in your model, and once in your view as kd_user.
To begin, you may wish to debug in each of those files (most probably the view) if that variable actually exists.
Without seeing all of your app's code, its hard to say for sure, but I'd verify that your view has a variable available called $kd_user for you to access. If it isn't there, you either need to find a way to get it into there, or reference another variable to get to the value you need.

Categories