isset

Difference between `if (isset($_SESSION))` and `if ($_SESSION)`?

与世无争的帅哥 提交于 2019-12-04 03:21:42
问题 I've noticed that frequently people simply write <?php if($_SESSION['username']) {...} ?> while I have been using: <?php if(isset($_SESSION['username'])) {...} ?> Could someone explain the difference when checking if a variable is set (that's what I'd be using it for)? 回答1: According to PHP.net, isset() does the following: Determine if a variable is set and is not NULL. When writing: <?php if($_SESSION['username']) {...} ?> You are checking to see if $_SESSION['username'] is equal to true. In

PHP魔术方法

半城伤御伤魂 提交于 2019-12-04 02:11:39
[TOC] 做下记录,温故而知新。 构造函数和析构函数 __construct 构造函数 类会在每次创建新对象时先调用此方法,所以非常适合在使用对象之前做一些初始化工作。 __deconstruct 析构函数会在到某个对象的所有引用都被删除或者当对象被显式销毁时执行。 <?php class MyDestructableClass { public $name; function __construct() { print "In constructor\n"; $this->name = "MyDestructableClass"; } function __destruct() { print "Destroying " . $this->name . "\n"; } } $obj = new MyDestructableClass(); ?> 方法重载 public __call ( string $name , array $arguments ) : mixed public static __callStatic ( string $name , array $arguments ) : mixed 在对象中调用一个不可访问方法时,__call() 会被调用。 在静态上下文中调用一个不可访问方法时,__callStatic() 会被调用。 方法不存在时也会经过此函数

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

我只是一个虾纸丫 提交于 2019-12-04 00:28:06
I need to check if php://input exists/isset. Does it work with php isset() ? What is the proper way to check it? 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 implementation, it may be possible to open another php://input stream and restart reading. This is only possible if

Check if array value isset and is null

北战南征 提交于 2019-12-03 22:43:52
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? Use array_key_exists() instead of isset() , because isset() will return false if the variable is null , whereas array_key_exists() just checks if the key exists in the array: function check($array,

Check if variable is_undefined in PHP

冷暖自知 提交于 2019-12-03 16:54:14
问题 In PHP, I want to check if a variable has not been set/defined, where setting a variable NULL is considered set/defined . I'm aware everything here: http://php.net/manual/en/types.comparisons.php including isset(), empty(), and is_null(). None of these appear to be what I'm looking for. Consider the following example: <?php $myNull = null; echo 'isset($myNull): "'.isset($myNull).'"<br />'; echo '$myNull value = "'.$myNull . '"<br />'; echo "<br />"; echo 'isset($myUndefined): "'.isset(

PHP if multiple variables exist

邮差的信 提交于 2019-12-03 12:51:24
So i'm having a bit of a problem with having my PHP run a command if multiple variables exist. I made a simple version for people to see what i'm trying to fix easier. Thanks in advance to anyone who can help :) <?php if ((isset($finalusername)) && if (isset($finalpassword)) && if (isset($finalemail))) echo "This will save."; ?> if (isset($finalusername, $finalpassword, $finalemail)) Also see The Definitive Guide to PHP's isset and empty . You don't need to place the multiple if in there. if (isset($finalusername) && isset($finalpassword) && isset($finalemail)) { // ... } In fact, I'd even do

PHP防止表单重复提交方法

泄露秘密 提交于 2019-12-03 10:04:33
下面的情况就会导致表单重复提交: 点击提交按钮两次。 点击刷新按钮。 使用浏览器后退按钮重复之前的操作,导致重复提交表单。 使用浏览器历史记录重复提交表单。 浏览器重复的HTTP请求。 网页被恶意刷新。 下面是几种解决办法: 一:利用js设置按钮点击后变成灰色 <form name=form1 method=”POST” action=”/” target=_blank> <p> <input type=”text” name=”T1″ size=”20″> <input type=”button” value=”提交” οnclick=”javascript:{this.disabled=true;document.form1.submit();}”> </p> </form> 点击完按钮之后变成灰色就不能点击了,用户需要再次提交表单的话就要刷新页面之后重新填写数据再提交了。 二:利用session 在 session 中放一个特殊标志。当表单页面被请求时,生成一个特殊的字符标志串,存在 session 中,同时放在表单的隐藏域里。接受处理表单数据时,检查标识字串是否存在,并立即从session中删除它,然后正常处理数据。 如果发现表单提交里没有有效的标志串,这说明表单已经被提交过了,忽略这次提交。 这使你的web应用有了更高级的 XSRF 保护 加载提交的页面时候

isset() function is returning true even when item is not set

匿名 (未验证) 提交于 2019-12-03 09:06:55
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Here is my code. For some reason, if I submit the form without placing and passwords in, it still creates the database entry. There are some comments scattered throughout the code, but the code is fairly straightforward. Any ideas?

What does the exclamation point mean in PHP? [duplicate]

拥有回忆 提交于 2019-12-03 08:50:44
This question already has answers here : Reference — What does this symbol mean in PHP? (18 answers) Getting confused with empty, isset, !empty, !isset (6 answers) Possible Duplicate: Reference - What does this symbol mean in PHP? Getting confused with empty, isset, !empty, !isset In PHP what is the difference between: if(!isset) if(isset) Same with if(!empty) and if(empty) ? What does the "!" character mean? ! is the logical negation or NOT operator. It reverses the sense of the logical test. That is: if(isset) makes something happen if isset is logical True . if(!isset) makes something

Check if value isset and null

匿名 (未验证) 提交于 2019-12-03 07:36:14
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I need to check if value is defined as anything, including null. isset treats null values as undefined and returns false . Take the following as an example: $foo = null ; if ( isset ( $foo )) // returns false if ( isset ( $bar )) // returns false if ( isset ( $foo ) || is_null ( $foo )) // returns true if ( isset ( $bar ) || is_null ( $bar )) // returns true, raises a notice Note that $bar is undefined. I need to find a condition that satisfies the following: if ( something ( $bar )) // returns false; if ( something ( $foo )) //