isset

PHP isset for an array element while variable is not an array

不羁的心 提交于 2019-12-10 19:01:42
问题 $a = 'a'; echo isset($a['b']); This code returns 1. Why? 回答1: 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']

Should I declare and check if variables exist in PHP?

大憨熊 提交于 2019-12-10 16:45:28
问题 I've noticed on XAMPP that strict error reporting is on and I get undefined index errors now. I just have two small questions (I'm still learning here): I know you don't have to declare variables in PHP but is there any advantage to declaring them anyway? If not, why do I get errors when strict error reporting is on when I don't define them? When I use get variables for example, I check for their value before I run a function like if($_GET['todo'] == 'adduser') runFunctionAddUser(); This

Test if session is started

折月煮酒 提交于 2019-12-10 13:33:35
问题 How do you test to see if sessions are on. This is not the way... session_start(); if(isset($_SESSION)) { echo "sessions ON<br>"; } else{ echo "sessions OFF<br>"; } session_destroy(); if(isset($_SESSION)) { echo "sessions ON<br>"; } else{ echo "sessions OFF<br>"; } 回答1: Try this: if(session_id()) http://php.net/manual/en/function.session-id.php 回答2: session_start() itself will return boolean TRUE if the session was started successfully, boolean FALSE if not. 来源: https://stackoverflow.com

Php if($_POST) vs if(isset($_POST))

霸气de小男生 提交于 2019-12-10 09:57:08
问题 I have a simple form as demonstrated below: <form action="" method="post"> <input type="text" /> <input type="submit" value="SEND" /> </form> When I try to receive data sent from this form via if($_POST) , I fail, but when try with isset , I success. if($_POST){ echo 'a'; //Doesn't print anything. } if(isset($_POST)){ echo 'b'; //Prints 'b' } I guess the reason behind it is missing name attribute in my form input, but I can't understand why if($_POST) and isset($_POST) react different ways in

How to tell whether a variable in ASP has been declared

余生颓废 提交于 2019-12-10 04:22:28
问题 Let me start off by saying I'm a PHP developer, not an ASP one. (And I really wish ASP had isset() .) And I'm working in a live environment so I don't really have an opportunity to do any testing. All the resources I've found suggest different ways to test for the existence of a variable. Here's what I'm trying to do: On SOME pages, I set a variable which holds a value for a robots <meta> tag: dim dsep_robots dsep_robots = "nofollow,noindex" All pages include header.asp . In my header file, I

How to check if php://input is set?

半世苍凉 提交于 2019-12-09 14:40:49
问题 I need to check if php://input exists/isset. Does it work with php isset() ? What is the proper way to check it? 回答1: Try to test it with file_get_contents() (for reading) + empty() or boolean conversion (for testing): <?php $input = file_get_contents('php://input'); if ($input) { // exists } else { // not exists } From php.net: Note: Prior to PHP 5.6 , a stream opened with php://input could only be read once; the stream did not support seek operations. However, depending on the SAPI

in php, why empty(“0”) returns true?

百般思念 提交于 2019-12-09 02:49:09
问题 According to php documentation, the following expressions return true when calling empty($var) "" (an empty string) 0 (0 as an integer) 0.0 (0 as a float) "0" (0 as a string) NULL FALSE array() (an empty array) $var; (a variable declared, but without a value) i've found how to "solve" the problem by using empty($var) && $var != 0 but why php developers did it? i think it is ridiculous, suppouse you have this code: if (empty($_POST["X"])) { doSomething(); } i think "0" is not empty, empty is

How to delete multiple rows from mysql database with checkbox using PHP?

不羁岁月 提交于 2019-12-08 15:05:23
问题 I try to delete my data in "admin" database, but the delete button does not function. This is my top part <?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="admin"; // Database name $tbl_name="admin"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql

基于codeigniter3框架使用PHPspreadsheet包实现excel导入导出功能

让人想犯罪 __ 提交于 2019-12-08 08:17:54
文章目录 codeigniter3+PHPspreadsheet实现excel导入导出功能 引入composer依赖包PHPspreadsheet 添加类库并use相关类 添加导入导出方法 实战应用 codeigniter3+PHPspreadsheet实现excel导入导出功能 引入composer依赖包PHPspreadsheet 根目录composer.json文件的require节点加入如下内容,注意json格式规则: "phpoffice/phpspreadsheet": "*" 之后需执行 composer update 下载PHPspreadsheet依赖包 添加类库并use相关类 在 application\libraries 目录下添加名为 Php_spread_sheet_lib.php 的文件(名称随意,注意不要使用PHPspreadsheet,避免不必要的冲突,ci3无命名空间) use必要类 use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice

Is there any reason to use isset()?

人盡茶涼 提交于 2019-12-07 22:56:38
问题 Why should I use if (isset($var)) {} rather than just if ($var) {} ? It seems to do the same thing and just take extra processing. Thanks! 回答1: Reason The reason is, isset() will return boolean and doesn't throw a warning when you check for the existence of variable and proceed. Also, there is a possibility that, a variable may have zero values: false 0 "" But they will be already set. Example $varb = false; $vari = 0; $vars = ""; isset($varb) // true isset($vari) // true isset($vars) // true