Asked  7 Months ago    Answers:  5   Viewed   34 times

I read somewhere that the isset() function treats an empty string as TRUE, therefore isset() is not an effective way to validate text inputs and text boxes from a HTML form.

So you can use empty() to check that a user typed something.

  1. Is it true that the isset() function treats an empty string as TRUE?

  2. Then in which situations should I use isset()? Should I always use !empty() to check if there is something?

For example instead of

if(isset($_GET['gender']))...

Using this

if(!empty($_GET['gender']))...

 Answers

65

isset vs. !empty

FTA:

"isset() checks if a variable has a value including (False, 0 or empty string), but not NULL. Returns TRUE if var exists; FALSE otherwise.

On the other hand the empty() function checks if the variable has an empty value empty string, 0, NULL or False. Returns FALSE if var has a non-empty and non-zero value."

Wednesday, March 31, 2021
 
Len_D
answered 7 Months ago
87

Being a superglobal, $_FILES is presumably always set, regardless whether an uploaded file exists or not.

Check for the file upload(s) you would expect and look at the size field. (Apparently according to the User Contributed Notes in the manual, if the form contains the upload element, it is possible that even isset($_FILES["my_file_name"]) will return true even though there was no file selected.

This should work reliably:

if($_POST['type'] == 'photo' && 
   ((isset($_FILES["my_file_name"]["size"]) && 
    ($_FILES["my_file_name"]["size"] > 0)) ){

(the isset() is to prevent a "undefined index" notice.)

What do you do this for, by the way?:

$_FILES = $HTTP_POST_FILES;
Wednesday, March 31, 2021
 
turson
answered 7 Months ago
52

The issue with the second method is that it will result in an error when error reporting is set to E_STRICT. For example:

Strict standards: Non-static method Foo::bar() should not be called statically in /home/yacoby/dev/php/test.php on line 10

A point with PHP6 is that the E_STRICT errors are moved to E_ALL. In other words E_ALL will cover all errors including not allowing you to call non static methods statically.

An alternative method may be to move the validation logic to a static function. That way the non static function and the static function can call the validation logic.

Saturday, May 29, 2021
 
csi
answered 5 Months ago
csi
11

String characters can be referenced by their offset using syntax like $a[0] for the first character, e.g.

$string = 'Hello';
echo $string[1];  // echoes 'e'

so PHP is recognising that $a is a string; casting your 'b' to a numeric (which casts to a 0), and trying to test isset on $a[0], which is the first character a

Though it should also throw an illegal offset 'b' warning if you have errors enabled

EDIT

$a = 'a';
echo isset($a['b']), PHP_EOL;
echo $a['b'];

PHP 5.3

1
a

PHP 5.4

Warning: Illegal string offset 'b' in /Projects/test/a10.php on line 6
a

PHP 5.5

PHP Warning:  Illegal string offset 'b' in /Projects/test/a10.php on line 6

Warning: Illegal string offset 'b' in /Projects/test/a10.php on line 6
a
Saturday, May 29, 2021
 
Trott
answered 5 Months ago
72

The Android Developer pages still state how you can download and use the ADT plugin for Eclipse:

  1. Start Eclipse, then select Help > Install New Software.
  2. Click Add, in the top-right corner.
  3. In the Add Repository dialog that appears, enter "ADT Plugin" for the Name and the following URL for the Location: https://dl-ssl.google.com/android/eclipse/
  4. Click OK.
  5. In the Available Software dialog, select the checkbox next to Developer Tools and click Next.
  6. In the next window, you'll see a list of the tools to be downloaded. Click Next.
  7. Read and accept the license agreements, then click Finish. If you get a security warning saying that the authenticity or validity of the software can't be established, click OK
  8. When the installation completes, restart Eclipse.

Links for the Eclipse ADT Bundle (found using Archive.org's WayBackMachine) I don't know how future-proof these links are. They all worked on February 27th, 2017.

  • Windows x86-64
  • Windows x86
  • Mac OS x86-64
  • Linux x86
  • Linux x86-64

Update (2015-06-29): Google will end development and official support for ADT in Eclipse at the end of this year and recommends switching to Android Studio.

Saturday, July 31, 2021
 
user1865027
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :