isset

isset and !empty not passing through a check for uploaded files

╄→гoц情女王★ 提交于 2019-12-22 05:15:15
问题 I have an upload form with a file to be uploaded. The issue I have is that even when no file is uploaded the if(isset($_FILES)) OR if(!empty($_FILES)) still passes as successful: $_FILES = $HTTP_POST_FILES; if($_POST['type'] == 'photo' && isset($_FILES)){ // returns true even if no file is uploaded. What am I missing! } 回答1: 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

isset and !empty not passing through a check for uploaded files

最后都变了- 提交于 2019-12-22 05:15:08
问题 I have an upload form with a file to be uploaded. The issue I have is that even when no file is uploaded the if(isset($_FILES)) OR if(!empty($_FILES)) still passes as successful: $_FILES = $HTTP_POST_FILES; if($_POST['type'] == 'photo' && isset($_FILES)){ // returns true even if no file is uploaded. What am I missing! } 回答1: 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

Check if variable is set and then echo it without repeating?

吃可爱长大的小学妹 提交于 2019-12-21 08:07:48
问题 Is there a concise way to check if a variable is set, and then echo it without repeating the same variable name? Instead of this: <?php if(!empty($this->variable)) { echo '<a href="', $this->variable, '">Link</a>'; } ?> I'm thinking about something in the lines of this C-style pseudocode: <?php echo if(!empty($this->variable, '<a href="', %s, '">Link</a>')); ?> PHP has sprintf, but it doesn't quite do what I was hoping for. If course I could make a method/function out of it, but surely there

Check if array value isset and is null

烂漫一生 提交于 2019-12-21 06:58:46
问题 How to check if array variable $a = array('a'=>1, 'c'=>null); is set and is null. function check($array, $key) { if (isset($array[$key])) { if (is_null($array[$key])) { echo $key . ' is null'; } echo $key . ' is set'; } } check($a, 'a'); check($a, 'b'); check($a, 'c'); Is it possible in PHP to have function which will check if $a['c'] is null and if $a['b'] exist without "PHP Notice: ..." errors? 回答1: Use array_key_exists() instead of isset() , because isset() will return false if the

Will isset() return false if I assign NULL to a variable?

好久不见. 提交于 2019-12-21 03:51:16
问题 I mean... I "set" it to NULL. So isset($somethingNULL) == true? 回答1: bool isset ( mixed $var [, mixed $var [, $... ]] ) Determine if a variable is set and is not NULL. If a variable has been unset with unset(), it will no longer be set. isset() will return FALSE if testing a variable that has been set to NULL . Also note that a NULL byte ("\0") is not equivalent to the PHP NULL constant. Return values Returns TRUE if var exists and has value other than NULL, FALSE otherwise. From the manual.

If url variable is empty redirect to home page

我是研究僧i 提交于 2019-12-20 06:31:03
问题 I am not able to get this to work correctly!! I have a view.php page that uses a variable of 'ID' to retrieve db info. So my url is like this: /view.php?id=23 I am trying to get it to "redirect" the user to the home page if they erase the "23" part of the url.. ? so when you visit /view.php?id= It should : if(isset($_GET['id']) == "") { header('Location: /'); exit; } Works if I visit /view.php 回答1: Try using empty(): if(empty($_GET['id'])) { header('Location: /'); exit; } 回答2: Another way: if

How to check if a multidimensional array item is set in JS?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-19 18:24:27
问题 How to check if a multidimensional array item is set in JS? w[1][2] = new Array; w[1][2][1] = new Array; w[1][2][1][1] = 10; w[1][2][1][2] = 20; w[1][2][1][4] = 30; How to check if w[1][2][1][3] is set? Solution with if (typeof w[1][2][1][3] != 'undefined') doesn't work. I don't want to use an Object instead of Array. 回答1: You are not checking the previous array elements existence before checking its children as the children elements cant exist if the parent doesnt if( typeof(w) != 'undefined

What's better, isset or not?

喜你入骨 提交于 2019-12-19 07:03:33
问题 Is there any speed difference between if (isset($_POST['var'])) or if ($_POST['var']) And which is better or are they the same? 回答1: It is a good practice to use isset for the following reasons: If $_POST['var'] is an empty string or "0" , isset will still detect that the variable exists. Not using isset will generate a notice. 回答2: They aren't the same. Consider a notional array: $arr = array( 'a' => false, 'b' => 0, 'c' => '', 'd' => array(), 'e' => null, 'f' => 0.0, ); Assuming $x is one

What's better, isset or not?

感情迁移 提交于 2019-12-19 07:02:10
问题 Is there any speed difference between if (isset($_POST['var'])) or if ($_POST['var']) And which is better or are they the same? 回答1: It is a good practice to use isset for the following reasons: If $_POST['var'] is an empty string or "0" , isset will still detect that the variable exists. Not using isset will generate a notice. 回答2: They aren't the same. Consider a notional array: $arr = array( 'a' => false, 'b' => 0, 'c' => '', 'd' => array(), 'e' => null, 'f' => 0.0, ); Assuming $x is one

How to implement __isset() magic method in PHP?

你。 提交于 2019-12-19 05:22:20
问题 I'm trying to make functions like empty() and isset() work with data returned by methods. What I have so far: abstract class FooBase{ public function __isset($name){ $getter = 'get'.ucfirst($name); if(method_exists($this, $getter)) return isset($this->$getter()); // not working :( // Fatal error: Can't use method return value in write context } public function __get($name){ $getter = 'get'.ucfirst($name); if(method_exists($this, $getter)) return $this->$getter(); } public function __set($name