All posts by shiji

isset() vs empty() vs is_null()

PHP has different functions which can be used to test the value of a variable. Three useful functions for this are isset()empty() and is_null(). All these function return a boolean value. If these functions are not used in correct way they can cause unexpected results.

isset() and empty() are often viewed as functions that are opposite, however this is not always true. In this post I will explain the differences between these functions.

 

isset()

From PHP manual – isset():

isset — Determine if a variable is set and is not NULL

In other words, it returns true only when the variable is not null.

empty()
From PHP Manual – empty():

empty — Determine whether a variable is empty

In other words, it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.

is_null()
From PHP Manual – is_null():

is_null — Finds whether a variable is NULL

In other words, it returns true only when the variable is null. is_null() is opposite of isset(), except for one difference that isset() can be applied to unknown variables, but is_null() only to declared variables.

The table below is an easy reference for what these functions will return for different values. The blank spaces means the function returns bool(false).
Value of variable ($var) isset($var) empty($var) is_null($var)
“” (an empty string) bool(true) bool(true)
” ” (space) bool(true)
FALSE bool(true) bool(true)
TRUE bool(true)
array() (an empty array) bool(true) bool(true)
NULL bool(true) bool(true)
“0″ (0 as a string) bool(true) bool(true)
0 (0 as an integer) bool(true) bool(true)
0.0 (0 as a float) bool(true) bool(true)
var $var; (a variable declared, but without a value) bool(true) bool(true)
NULL byte (“\ 0″) bool(true)
=======================================
Copy From http://techtalk.virendrachandak.com/php-isset-vs-empty-vs-is_null/  By Virendra on January 21, 2012

Practice it CSE142 MT 06au 的一道题

Write a method named hasMidpoint that accepts three integers as parameters and returns true if one of the integers is the midpoint between the other two integers; that is, if one integer is exactly halfway between them. Your method should return false if no such midpoint relationship exists.
The integers could be passed in any order; the midpoint could be the 1st, 2nd, or 3rd. You must check all cases.
Calls such as the following should return true:
hasMidpoint(4, 6, 8)
hasMidpoint(2, 10, 6)
hasMidpoint(8, 8, 8)
hasMidpoint(25, 10, -5)
Calls such as the following should return false:
hasMidpoint(3, 1, 3)
hasMidpoint(1, 3, 1)
hasMidpoint(21, 9, 58)
hasMidpoint(2, 8, 16)
=========================================================
以下是我和晓天想出来的答案。我们最开始打算先进行排序,再求差,来判断,很是程序写起来会很长,而且我们现在没有讲各种排序法。
=========================================================

public static boolean hasMidpoint(int a,int b,int c){
    int e=a+b+c;
    if (e/3==a||e/3==b||e/3==c)
        return true;
    return false;
}

=============================================================

# name expected return your return result
1 hasMidpoint(1, 2, 3) true true pass
2 hasMidpoint(2, 10, 6) true true pass
3 hasMidpoint(0, -50, -25) true true pass
4 hasMidpoint(21, 9, 58) false false pass
5 hasMidpoint(-2, 9, 27) false false pass
=================================================
提交答案之后,我突然觉得这个程序存在问题,虽然在系统给的数值里没有测出问题,但是由于int类数据的特点,10/3是等于3的。所以输入以下测试代码。
==================================================

public class test{
public static void main(String[] args){
System.out.println(hasMidpoint(0,-51,-25));
}
public static boolean hasMidpoint(int a,int b,int c){
int e=a+b+c;
if (e/3==a||e/3==b||e/3==c)
return true;
return false;
}
}

//由于是复制过来的,就不能保持很好的代码格式了。

=============================================================
输出结果:true
=============================================================
很显然,-25不是-51和0的中点,刚刚的源码是有问题的。
=================================================
把if的条件语句作了如下调整:

if ((e/3==a||e/3==b||e/3==c)&&e%3==0)

再次调用

System.out.println(hasMidpoint(0,-501,-250));

输出是false
=================================================
现在基本已经不会存在问题了,通过了我的所有测试。
如果有人认为我的代码还存在问题,欢迎回复。