Jump to content

Recommended Posts

After reading about variables and operators I tired this. It's for email verification (I think). It works when I try it but I'm not sure yet how to use it a contact form. Do you like?  :D

 

<?php
  // email test
  $email1 = $_POST['email1'];
  $email2 = $_POST['email2'];
  $result = $email2 !== $email1 ? 'No Match' : 'Success';
  echo $result;
?>

Link to comment
https://forums.phpfreaks.com/topic/200933-my-first-script/
Share on other sites

I'm still a noob lol but l learned some more complex codes since that time. Your validation looks much more professional. I assume that's a default php function for email input?

 

Lol I like how this was posted on May 6th. So you are still not a noob ? Learning takes time.

Here is how I validate emails:

function validEmail($email){
return filter_var($email, FILTER_VALIDATE_EMAIL);
}

 

Good ol' one liners.

Link to comment
https://forums.phpfreaks.com/topic/200933-my-first-script/#findComment-1057328
Share on other sites

Lol I like how this was posted on May 6th. So you are still not a noob ? Learning takes time.

Here is how I validate emails:

function validEmail($email){
return filter_var($email, FILTER_VALIDATE_EMAIL);
}

 

Good ol' one liners.

 

Why make a function that would be called like this:

 


if(validEmail($email)){

}//end if

 

when you can save overhead and just do this:

 


if(filter_var($email, FILTER_VALIDATE_EMAIL)!==false){

}//end if

Link to comment
https://forums.phpfreaks.com/topic/200933-my-first-script/#findComment-1059003
Share on other sites

It isn't a variation, he just wrapped a standard function in a call with a different name...

 

Check this out, though if you are looking to be overwhelmed... It is a regex for true email validation:

 

http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html

Link to comment
https://forums.phpfreaks.com/topic/200933-my-first-script/#findComment-1059125
Share on other sites

Lol I like how this was posted on May 6th. So you are still not a noob ? Learning takes time.

Here is how I validate emails:

function validEmail($email){
return filter_var($email, FILTER_VALIDATE_EMAIL);
}

 

Good ol' one liners.

 

Why make a function that would be called like this:

 


if(validEmail($email)){

}//end if

 

when you can save overhead and just do this:

 


if(filter_var($email, FILTER_VALIDATE_EMAIL)!==false){

}//end if

 

As you may want to vary the implementation, at some point you may want to ditch filter_* and use something different (like an on-line service for example) which would require you to hunt down all files that use filter_* instead of just modifying the validEmail function (as it may be used at multiple locations, newsletter, registration, login, ..).

 

It's always better to write a function once you start to need something at multiple places, chances are you will be needing it in more places later on.

Link to comment
https://forums.phpfreaks.com/topic/200933-my-first-script/#findComment-1059173
Share on other sites

Regardless. I understand that the implementation may vary, but all you have to do is pass a different flag to the expression. I personally use a single email validation function that returns errors, such as 'invalid character in domain part', etc, so I can understand your desire to call it from a single location. But the example in this case is as useless as the following:

 


function myIf($check,$ifTrue,$ifFalse){

if($check){

$ifTrue;

}else{

$ifFalse;

}//end if

}//end function

 

It is a complete waste. It is not like it is so hard to go here: http://us.php.net/manual/en/filter.filters.php and get a different flag. Plus it would no longer be an email validation function if you change the flag to "FILTER_VALIDATE_IP" or something else.

Link to comment
https://forums.phpfreaks.com/topic/200933-my-first-script/#findComment-1059196
Share on other sites

The reason I used a function is just in case I find a better way of checking for a valid email, I call this function in several different locations. I need to know if it is valid or not. If I need something more from the input, then I would have written the function to do so. You cannot judge abstract code without knowing the application.

 

 

Also, just a suggestion:

if(filter_var($email, FILTER_VALIDATE_EMAIL)!==false){
}

 

Is syntactically correct, but doing this: !==, is the operator for not identical, != is for not equal, in php.

 

I would have written it like this:

if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
}

 

For the expression is evaluated to true or not. So in order for it to be a non-valid email we would put the not operator (!) in the expression so it can be evaluated to being true to execute the if body.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/200933-my-first-script/#findComment-1059656
Share on other sites

I fail to follow your train of thought. I'm well aware of the purpose of the !== and the !, as well as the differences between !== and !=.

 

I myself usually just use !, but as I was illustrating to the OP that we wanted to make sure it was not invalid (false), I used !==.

 

The reason I used a function is just in case I find a better way of checking for a valid email

 

This is a really bad example for this case. Email validation specs will doubtfully change. I'm sure the ones packaged with php will suffice for most people. Although I understand your desire to follow the DRY method. My point here is that it is a little off topic, trying to teach him about OOP ideologies when his question was about email validation. Although I suppose you never explicitly stated your function was called from an object.

 

For the expression is evaluated to true or not. So in order for it to be a non-valid email we would put the not operator (!) in the expression so it can be evaluated to being true to execute the if body.

 

You do know that this function, filter_var does not evaluate to true or false right? It returns either false, or the string (email) itself.

 

I maintain that for this example, this particular one, wrapping filter_var in another function is idiotic.

Link to comment
https://forums.phpfreaks.com/topic/200933-my-first-script/#findComment-1059668
Share on other sites

While I respond to you, I must keep in mind that I must stay classy.

 

While talking about checking for a false evaluation why would you use a !== instead of a !=, false or FALSE is always 0.

 

Furthermore, I am not trying to impress anyone. I am not trying to showoff, if I could. I was just showing him an example of simple email validation.

 

I fail to understand YOUR explanation of a DRY method. First off, what is a DRY method? Just a procedural approach, maybe?

 

I also like the fact that you truncate my quotes.

 

However, from what I understand is, I should not wrap a function around another one, even though I call it several different locations, I should just use it as is, then go back and change it in several different locations? By doing so I am an idiot. Thanks.

 

I, for one, did not know that filter_var return the email otherwise.

 

So now I can just change my one function to say this:

function validEmail($email){
  if(!filter_var($email, FILTER_VALIDATE_EMAIL))
    return false;
  else
    return true;
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/200933-my-first-script/#findComment-1059682
Share on other sites

I truncated your quotes to include the relevant parts.

 

DRY = Don't Repeat Yourself... essentially don't code twice.. which is what you were arguing for btw.

 

And once again, for this SPECIFIC example there is no point in wrapping a perfectly fine function in any of the wrappers you've provided. Now that you have your new fancy function wrapper what are you going to say?

 


if(validEmail($email)){

}

 

That is a complete waste, when you can merely verify it is not false.

 

I really fail to understand how that is easier than the following, in this case:

 


if(!filter_var($email,FILTER_VALIDATE_EMAIL)){

}//end if

 

which, as I've said:

 

 

I myself usually just use !, but as I was illustrating to the OP that we wanted to make sure it was not invalid (false), I used !==.

 

 

I merely did the identity check to illustrate to the OP that he needed to make sure it did not fail. I figure someone who is new to programming could very easily skip over a little ! hidden at the beginning of an if() and become confused, thus defying the purpose of an example.

 

So to clarify, I agree with you about a single instance (not as in instantiated instance) of a function. As do all people who adhere to DRY. And people who like OOP. And, I agree that ! will suffice instead of !==. However, I refuse to agree with your arguments for wrapping filter_var().

Link to comment
https://forums.phpfreaks.com/topic/200933-my-first-script/#findComment-1059689
Share on other sites

I want to apologize to you. I've been going through a lot of crap here lately. Taking it out, through the internet, is not acceptable.

 

I can see where you are coming from, without the application involved this function looks ridiculous, however I am all about writing clean, efficient, DRY code. ;)

 

I am done arguing with why I am using the function.

 

Once again, sorry.

Link to comment
https://forums.phpfreaks.com/topic/200933-my-first-script/#findComment-1059702
Share on other sites

Although I suppose you never explicitly stated your function was called from an object..

 

Because it isn't.

 

trying to teach him about OOP ideologies when his question was about email validation.

 

DRY nor KISS is an ideology specific to OOP but to general programming.

 

I refuse to agree with your arguments for wrapping filter_var().

 

Imagine yourself using filter_var in different modules (not in a function of course, that's stupid) and you get a new client who has his own server running some CMS system and is interested in buying your product (a CRM system for example) however your client happens to run a PHP version smaller then 5.2 (which does not have filter_var()) and due to the CMS he runs refuses to upgrade, now you have to go through all your code to change all occurrences of filter_var with validEmail() as I bet you don't want to lose ~$2000 over filter_var() do you?

 

All I want to say is that you can have multiple reasons for wrapping something in a function and it's ALWAYS a good idea to wrap it inside a function if you are to use it in MULTIPLE places to make problems like the above easier to CHANGE. Your application is subject to change and you should make it as easy on yourself to these changes.

 

NOW it may seem like a bad idea to wrap filter_var() in all these locations but you will be happy to have done so when the above client passes.

 

Another scenario may be where a client requires custom rules to apply to an e-mail, like where it would only be considered valid if it does not contain hotmail, gmail, ..

 

No one can predict the future but you can write your software to more easily incorporate possible changes.

Link to comment
https://forums.phpfreaks.com/topic/200933-my-first-script/#findComment-1059928
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.