Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

66 users online



Condensing Coding

Condensing Coding

Currently viewing this thread: 1 (0 members and 1 guests)


Aki

Aki

Neverside Newbie
Status: Offline!

Condensing Coding

Take a look at this piece of coding:

PHP:

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

<?php

if ($new_string == $random){
} else {
    echo 
"The verifcation code you entered was not correct.";
    exit();
}
?>

Would it look like that?

Thanks Smile

Last edited by Aki, February 4th, 2006 09:39 PM (Edited 1 times)

kiswa

kiswa

Yeah, kiswa.
Status: Offline!

How about:

PHP:

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

Aki

Aki

Neverside Newbie
Status: Offline!

Ahh very clever. I completely forgot about that function.

PHP:

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

<?php
    
if (ereg("@",$email)) {
        echo 
"";
    } else {
        echo 
"You did not enter an email address.";
        exit();
    }
?>

And this snippet. Wink

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)

Aki

Aki

Neverside Newbie
Status: Offline!

The agreetoterms thing can be:

PHP:

<?php
    
if(!($agreetoterms)) {
        echo 
"You did not agree to the terms.";
        exit();
    }
?>

And the email address check can be:

PHP:

<?php
if (!(ereg("@",$email))) {
        echo 
"You did not enter an email address.";
        exit();
    }
?>

Problem solved. Thanks.

BigToach

BigToach

Neversidian
Status: Offline!

what i usually do is something like this

PHP:

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

fiestated

fiestated

god.
Status: Offline!
Originally posted by BigToach:
PHP:

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

Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.0098 seconds.