Jump to content
dalawh

Function to check for undefined?

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.

Share this post


Link to post
Share on other sites
Guest So Called
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.

Edited by So Called

Share this post


Link to post
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.

Share this post


Link to post
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! :)

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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...