
February 4th, 2006
09:39 PM
Neverside Newbie
Status: Offline!
Condensing Coding
Take a look at this piece of coding:
<?php
if ($new_string == $random){
echo ""; // if the code is correct, echo nothing and continue on
} else {
echo "The verifcation code you entered was not correct.";
exit();
}
?>
I know there is some way to condense this coding, and it's rather simple too. I'm just not quite sure what it would look like. Instead of putting in a worthless blank echo in there, can I remove that? Basically if the verification code that the user entered (thats what this piece of coding does) is correct, the script would continue on (and we could get rid of that worthless echo line). I am not trying to change the function of this script, but simply how the coding is displayed. I imagine there is a way to get rid of it.
<?php
if ($new_string == $random){
} else {
echo "The verifcation code you entered was not correct.";
exit();
}
?>
Would it look like that?
Thanks 
Last edited by Aki, February 4th, 2006 09:39 PM (Edited 1 times)

February 4th, 2006
09:54 PM
Yeah, kiswa.
Status: Offline!
How about:
<?php
if ($new_string != $random) {
echo "The verification code you entered was not correct.";
exit();
}
?>
___________________
www.kiswa.com
My attempt at articles for Neverside CodeBase: Part 1 Part 2 Part 3
(. )v( ·) <- The Webstandards Owl

February 4th, 2006
10:03 PM
Neverside Newbie
Status: Offline!
Ahh very clever. I completely forgot about that function.
<?php
if($agreetoterms){
echo ""; // if they agree to the terms, echo nothing & continue
} else {
echo "You did not agree to our terms.";
exit();
}
?>
The 'agreetoterms' is a checkbox. How can I condense this coding?
<?php
if (ereg("@",$email)) {
echo "";
} else {
echo "You did not enter an email address.";
exit();
}
?>
And this snippet. 
I don't believe you can use the != thing in this particular case, since these fields are not set equal to anything.
Last edited by Aki, February 4th, 2006 10:04 PM (Edited 1 times)

February 4th, 2006
11:09 PM
Neverside Newbie
Status: Offline!
The agreetoterms thing can be:
<?php
if(!($agreetoterms)) {
echo "You did not agree to the terms.";
exit();
}
?>
And the email address check can be:
<?php
if (!(ereg("@",$email))) {
echo "You did not enter an email address.";
exit();
}
?>
Problem solved. Thanks.

February 5th, 2006
06:21 AM
Neversidian
Status: Offline!
what i usually do is something like this
<?php
$errors = array();
if(!preg_match('/^a-z0-9]{4,12}$/i', $_POST['username']))
{
$errors['username'] = 'Username is not valid.';
}
if($_POST['password'] !== $_POST['repeatpass'])
{
$errors['password'] = 'Passwords do not match.';
}
if(!preg_match('/@/', $_POST['email']))
{
$errors['mail'] = 'Please supply a valid email address.';
}
if(count($errors) > 0)
{
foreach($errors as $error)
{
echo $error . '<br />';
}
exit;
}
?>
you can also redirect to to the form and have the specific errors by the form fields so it is easier for the user to fix the problems.
___________________
Neverside Development Director
PHP Snippets
BigToach.com - IT WORKS, TOACHY!
Last edited by BigToach, February 9th, 2006 07:43 AM (Edited 1 times)

February 7th, 2006
03:24 AM
Originally posted by BigToach:
<?php
if(!preg_match('/^a-z0-9]{4,12}$/i', $_POST['username']))
?>
fixed syntax error
Last edited by fiestated, February 7th, 2006 03:25 AM (Edited 1 times)