tags:

views:

77

answers:

4

i want to disply an error when some varaiable have blank value or empty or null value (whatever we say), variable is shown below:

 $mo=strtotime($_POST['MondayOpen']);
 where var_dump($_POST['MondayOpen']) return string(0) "".

now i want to solve below

  1. first of all i want to know $mo is which type of variable (string or integer or other type of variable)

  2. which function is better to find that $mo have no value

i conduct a test with $mo and i got these results

is_int($mo);//--Return nothing
is_string($mo); //--Return bool(false) 
var_dump($mo);  //--Return bool(true)                   
var_dump(empty($mo));//--Return bool(true) 
var_dump($mo==NULL);//--Return bool(true) 
var_dump($mo=='');//--Return nothing
A: 

You can check its type using:

gettype($mo);

but null and empty are different things, you can check with these functions:

if (empty($mo))
{
  // it is empty
}

if (is_null($mo))
{
  // it is null
}

Another way to check if variable has been set is to use the isset construct.

if (isset($mo))
{
  // variable has been set
}
Sarfraz
+1  A: 

var_dump outputs variables for debugging purposes, it is not used to check the value in a normal code. PHP is loosely typed, most of the time it does not matter if your variable is a string or an int although you can cast it if you need to make sure it is one, or use the is_ functions to check.

To test if something is empty:

if ( empty( $mo ) ) {
  // error
}

empty() returns true if a variable is 0, null, false or an empty string.

Chad
+2  A: 

PHP offers a function isset to check if a variable is not NULL and empty to check if a variable is empty.

To return the type, you can use the PHP function gettype

if (!isset($mo) || is_empty($mo)) {
 // $mo is either NULL or empty.
 // display error message
 }
Anthony Forloney
if (!isset($mo) || empty($mo)) is redundant.if (empty($mo)) has the same behavior.
chris
+1  A: 

doing strtotime will return false if it cannot convert to a time stamp.

$mo = strtotime($_POST['MondayOpen']);
if ($mo !== false)
{
//valid date was passed in  and $mo is type int
}
else
{
//invalid date let the user know
}
MANCHUCK