Jump to content
Killersites Community

Recommended Posts

What does mean that variable is set or not set? (in PHP or general)

Does that means:

  • // no var declared
  • $var;
  • $var = 'Some fancy string';
  • $var = ""; //(empty string)

I dont know all fancy terms, but I do know when variable is defined, declared and all that, but I dont know what variable is set means.

Thanks in advance, nerdz!

Edited by Biske94

Share this post


Link to post
Share on other sites

Answer:

 

isset() is a function in PHP that will return true if the variable, in this case, $var has been assigned a value. If the variable has been created but nothing assigned, has the value of null or undefined, it will return false. basically, isset($var) says is this variable safe to use or not. 

Update

To explain the differences between a NULL value and an undefined

<?php
//test 1 is defined, but has a value of null. isset will return false, use causes no error.
$test1 = null;
var_dump($test1);
var_dump(isset($test1));

echo "\n----------\n\n";

//test2 is defined with a string value. isset will return true
$test2 = "test";
var_dump($test2);
var_dump(isset($test2));

echo "\n----------\n\n";

//test3 is not defined, isset returns false and use causes error.
var_dump($test3);
var_dump(isset($test3));

Which will output:

NULL
bool(false)

----------

string(4) "test"
bool(true)

----------


Notice: Undefined variable: test3 in /in/Nmk0t on line 17
NULL
bool(false)

 

I hope this will help those who asked the same question as I :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×