SaCH Posted April 11, 2012 Share Posted April 11, 2012 Dear friends, I have a class to validate a form. This is not my own class & i got it from some where. This is my class <?php class validation { function email_validation($email, $email_label) { //E-mail validation: We use regexp to validate email. $email = trim($email); if (strlen($email) >= 1 ) { if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $email) === 0) $error = 'You have to enter a valid '.$email_label; else $error = null; }else $error = 'You have to enter your '.$email_label; return $error; } } ?> My question is in this function consider this line of code if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $email) === 0) $error = 'You have to enter a valid '.$email_label; else $error = null; here we do not using this "{" and "}" inside the if statement but is working without it. Iam confused about it and anyone can clear it out ?? Why we do not using that on the function & why did it does not producing the error ?? Thanks In Advance!! Quote Link to comment https://forums.phpfreaks.com/topic/260727-help-with-this-simple-php-class-code/ Share on other sites More sharing options...
MMDE Posted April 11, 2012 Share Posted April 11, 2012 Without the brackets it will act as if there was a { before and a } after the next line / statement / block of code. In my opinion, it's not very good practice, but with very short code, it can sometimes be easier to read. Quote Link to comment https://forums.phpfreaks.com/topic/260727-help-with-this-simple-php-class-code/#findComment-1336299 Share on other sites More sharing options...
SaCH Posted April 11, 2012 Author Share Posted April 11, 2012 Without the brackets it will act as if there was a { before and a } after the next line / statement / block of code. In my opinion, it's not very good practice, but with very short code, it can sometimes be easier to read. Thanks for your knowledge. that means the working condition of it without bracket and with the bracket is same. Quote Link to comment https://forums.phpfreaks.com/topic/260727-help-with-this-simple-php-class-code/#findComment-1336300 Share on other sites More sharing options...
MMDE Posted April 11, 2012 Share Posted April 11, 2012 Without the brackets it will act as if there was a { before and a } after the next line / statement / block of code. In my opinion, it's not very good practice, but with very short code, it can sometimes be easier to read. Thanks for your knowledge. that means the working condition of it without bracket and with the bracket is same. Haven't looked too much on the code, but I would guess so, especially if you got it from somewhere else, probably by someone who knows how to use it properly. I don't think it look very readable, so I see little reason to not add them. To be on the safe side, you could always add them, especially since you don't feel secure not using them! Quote Link to comment https://forums.phpfreaks.com/topic/260727-help-with-this-simple-php-class-code/#findComment-1336302 Share on other sites More sharing options...
SaCH Posted April 11, 2012 Author Share Posted April 11, 2012 Thanks Agian.... I hope someone's new replies against this. Quote Link to comment https://forums.phpfreaks.com/topic/260727-help-with-this-simple-php-class-code/#findComment-1336309 Share on other sites More sharing options...
Muddy_Funster Posted April 11, 2012 Share Posted April 11, 2012 { and } open and close a "code block" respectivly. It informs the PHP processor that everything between then is to be run in the even for the if, while, else, foreach etc. By not using them PHP defaults to run only the next single line of code as the event code and then dropt out of the condition back to the main page code. rule of thumb : if your not sure about it, use them. Quote Link to comment https://forums.phpfreaks.com/topic/260727-help-with-this-simple-php-class-code/#findComment-1336359 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.