Jump to content

Recommended Posts

Posted (edited)

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
  • 2 weeks later...
Posted

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 :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...