Page 1 of 1

Very Simple: How do I break/return/stop a function?

Posted: Thu May 08, 2008 9:18 pm
by JAB Creations
Ok this one is really simple: one of the values is invalid by default (so you don't have to do anything except just save the page and click the validate button).

The undesirable behavior is that it continues to execute the function even though on line 89 I used return. If any invalid values (highlighted in red) are found I want to stop executing the function.

That's it! Simple! Yet just complex enough that it's a shower on my otherwise on-fire productive day. :mrgreen:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>PHP Serverside Validation of CSS post data</title>
<style type="text/css">
body,html {font-family: monospace;}
b {color: #00f;}
b.bad {color: #f00;}
b.good {color: #0f0;}
div.overflow {height: 200px; overflow: auto; width: 40%;}
</style>
</head>
<body>
 
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<fieldset>
<input name="ce000" value="000" />
<input name="ce001" value="fff" />
<input name="ce002" value="ff0" />
<input name="ce003" value="00f" />
<input name="ce010" value="a1b" />
<input name="ce011" value="7f6" />
<input name="ce012" value="transparent" />
<input name="ce013" value="1f" />
<input name="ce020" value="2f6" />
<input name="ce021" value="3f6" />
<input name="ce022" value="4f6" />
<input name="ce023" value="5f6" />
<input name="ce030" value="6f6" />
<input name="ce031" value="7d6" />
<input name="ce032" value="" />
<input name="ce033" value="" />
<input name="ce040" value="" />
<input name="ce041" value="" />
<input name="ce042" value="" />
<input name="ce043" value="" />
<input name="ce050" value="" />
<input name="ce051" value="" />
<input name="ce052" value="" />
<input name="ce053" value="" />
<input name="ce060" value="" />
<input name="ce061" value="" />
<input name="ce062" value="" />
<input name="ce063" value="" />
<input name="ce070" value="" />
<input name="ce071" value="" />
<input name="ce072" value="" />
<input name="ce073" value="" />
<input name="ce080" value="" />
<input name="ce081" value="" />
<input name="ce082" value="" />
<input name="ce083" value="" />
<input name="ce090" value="" />
<input name="ce091" value="" />
<input name="ce092" value="" />
<input name="ce093" value="" />
<br style="clear: both;" />
<!--
<label for="position" style="border: #000 dotted 1px; font-size: 18px;">Choose Regex Position: 
<select id="position" name="position">
 <option value="all">Display all form names and values.</option>
 <option value="0">Name value ends with '0'.</option>
 <option value="1">Name value ends with '1'.</option>
 <option value="2">Name value ends with '2'.</option>
 <option value="3">Name value ends with '3'.</option>
</select>
</label>
-->
<input style="display: block; width: 60%;" type="submit" value="Validate Form Data" />
</fieldset>
</form>
 
<div>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$comma_separated = implode(",", $_POST);
echo '<p><b>Imploded Post Data:</b> '.$comma_separated.'</p>'."\n";
}
 
echo '<div class="overflow">';
//$regex_a = '/^(([a-f0-9]{3})|([a-f0-9]{6}))$/';
$regex_a = '/^(([0-9a-f]{1,2}){3}|transparent|none)?$/';
$regex_b = '/^[a-f0-9]{6}$/';
function validate_array($regex, $var)
{
 if ($validity != '1')
 {
  if (preg_match($regex, $var)) {echo '<b class="good">'.$var.'</b> is a <b class="good">match</b>!</b><br />';}
  else {echo '<b class="bad">'.$var.'</b> not a <b class="bad">match</b>!</b><br />'; return;}
 }
}
 
foreach($_POST as $key => $value)
 
validate_array($regex_a,$value);
 
echo '</div>';
if (isset($validity)) {echo '<br />The array <b class="bad">contains invalid data</b>!';}
else {echo '<br />The array contains <b class="good">only valid data</b>.';}
 
?>
<p>Valid data includes 'transparent', short-hand hexidecimal (fff, 0ff, 123, etc), and regular hexidecimal values.</p>
 
<p>Any other data <b><i>should</i></b> trigger an error and in which case all the form data should be rejected as a whole!</p>
 
</div>
</body>
</html>

Re: Very Simple: How do I break/return/stop a function?

Posted: Thu May 08, 2008 9:25 pm
by nowaydown1
The return in your validate_array function is doing what it's supposed to do. You're calling the validate_array function in a loop. When the return condition is hit, your function will exit it's local scope, but the loop will keep rocking. You'll need some additional logic in there:

Code: Select all

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>PHP Serverside Validation of CSS post data</title>
<style type="text/css">
body,html {font-family: monospace;}
b {color: #00f;}
b.bad {color: #f00;}
b.good {color: #0f0;}
div.overflow {height: 200px; overflow: auto; width: 40%;}
</style>
</head>
<body>
 
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<fieldset>
<input name="ce000" value="000" />
<input name="ce001" value="fff" />
<input name="ce002" value="ff0" />
<input name="ce003" value="00f" />
<input name="ce010" value="a1b" />
<input name="ce011" value="7f6" />
<input name="ce012" value="transparent" />
<input name="ce013" value="1f" />
<input name="ce020" value="2f6" />
<input name="ce021" value="3f6" />
<input name="ce022" value="4f6" />
<input name="ce023" value="5f6" />
<input name="ce030" value="6f6" />
<input name="ce031" value="7d6" />
<input name="ce032" value="" />
<input name="ce033" value="" />
<input name="ce040" value="" />
<input name="ce041" value="" />
<input name="ce042" value="" />
<input name="ce043" value="" />
<input name="ce050" value="" />
<input name="ce051" value="" />
<input name="ce052" value="" />
<input name="ce053" value="" />
<input name="ce060" value="" />
<input name="ce061" value="" />
<input name="ce062" value="" />
<input name="ce063" value="" />
<input name="ce070" value="" />
<input name="ce071" value="" />
<input name="ce072" value="" />
<input name="ce073" value="" />
<input name="ce080" value="" />
<input name="ce081" value="" />
<input name="ce082" value="" />
<input name="ce083" value="" />
<input name="ce090" value="" />
<input name="ce091" value="" />
<input name="ce092" value="" />
<input name="ce093" value="" />
<br style="clear: both;" />
<!--
<label for="position" style="border: #000 dotted 1px; font-size: 18px;">Choose Regex Position:
<select id="position" name="position">
 <option value="all">Display all form names and values.</option>
 <option value="0">Name value ends with '0'.</option>
 <option value="1">Name value ends with '1'.</option>
 <option value="2">Name value ends with '2'.</option>
 <option value="3">Name value ends with '3'.</option>
</select>
</label>
-->
<input style="display: block; width: 60%;" type="submit" value="Validate Form Data" />
</fieldset>
</form>
 
<div>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$comma_separated = implode(",", $_POST);
echo '<p><b>Imploded Post Data:</b> '.$comma_separated.'</p>'."\n";
}
 
echo '<div class="overflow">';
//$regex_a = '/^(([a-f0-9]{3})|([a-f0-9]{6}))$/';
$regex_a = '/^(([0-9a-f]{1,2}){3}|transparent|none)?$/';
$regex_b = '/^[a-f0-9]{6}$/';
function validate_array($regex, $var)
{
 if ($validity != '1')
 {
  if (preg_match($regex, $var)) {echo '<b class="good">'.$var.'</b> is a <b class="good">match</b>!</b><br />';}
  else {echo '<b class="bad">'.$var.'</b> not a <b class="bad">match</b>!</b><br />'; return false;}
 }
 
 return true;
}
 
foreach($_POST as $key => $value) {
   if(!validate_array($regex_a,$value)) {
      break;
   }
}
 
echo '</div>';
if (isset($validity)) {echo '<br />The array <b class="bad">contains invalid data</b>!';}
else {echo '<br />The array contains <b class="good">only valid data</b>.';}
 
?>
<p>Valid data includes 'transparent', short-hand hexidecimal (fff, 0ff, 123, etc), and regular hexidecimal values.</p>
 
<p>Any other data <b><i>should</i></b> trigger an error and in which case all the form data should be rejected as a whole!</p>
 
</div>
</body>
</html>
 
You'll see that you validate_array function has return codes now, true or false depending on what happens inside. Additionally, I expanded your foreach loop to check the return code of your validate_array function. If something appears invalid, it does a break; which stops the execution of your loop. Hope that helps.

Re: Very Simple: How do I break/return/stop a function?

Posted: Thu May 08, 2008 9:31 pm
by JAB Creations
That is frigin awesome! Thank you very much nowaydown1! :mrgreen: