Jump to content

Function to check for undefined?


dalawh
 Share

Recommended Posts

	 <form name="form" action="enter.php" method="post">	  First Name: <input type="text" name="first" /><br />	  Last Name: <input type="text" name="last" /><br />	  <input type="checkbox" name="c1" value="1" onclick="showSubmit()" />	  <input type="checkbox" name="c1" value="1" onclick="showSubmit()" />	  <input type="checkbox" name="c3" value="1" onclick="showSubmit()" />      <!-- SUBMIT BUTTON HERE -->	 </form>

If I only check the 1st and the 3rd checkbox and press the submit button, but in my enter.php, I made it check print out each of the inputs like so:

<?php echo $_POST["first"];echo $_POST["last"];echo $_POST["c1"];  echo $_POST["c2"]; echo $_POST["c3"];?>

I will get a warning/error claiming that the c2 is undefined. Is there a built in function that checks if something is undefined ? That way I can use a if and else statement: if is it undefined, it will print something, and if it is checked (defined), make it print something else.

Link to comment
Share on other sites

http://php.net/manua...ction.isset.php
if (isset($_POST["c2"])) $c2 = $_POST["c2"];else $c2 = "";echo $c2;

or

echo isset($_POST["c2"]) ? $_POST["c2"] : "";

or

$c2 = isset($_POST["c2"]) ? $_POST["c2"] : "";echo $c2;

etc.

Thanks a lot, that worked. I read somewhere that isset only works for things set to null, did not know undefined and null where the same.
Link to comment
Share on other sites

Guest So Called

I think eventually everybody who uses $_POST discovers that you need isset() to prevent undefined index errors. Today it's your day! :)

Link to comment
Share on other sites

They aren't the same. Null is a special value in PHP that indicates that a variable has no value. Undefined means the variable does not even exist at all. Undefined is not a value in PHP, null is a value. Undefined is a value in other languages, like Javascript, but not PHP. The error message you were getting was telling you that you are trying to access an index of an array ($_POST) that is not defined, not that the index exists and has a null value.

Link to comment
Share on other sites

I read somewhere that isset only works for things set to null, did not know undefined and null where the same.
They aren't really the same. For example:$varOne = null;Technically, $varOne is set, but it is set to null. $varTwo is not set at all. The isset function just considers null to be the same as not set. EDIT: Too slow, JSG beat me... Edited by ShadowMage
Link to comment
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
 Share

×
×
  • Create New...