Jump to content

Help with this simple php class code


SaCH

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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! :)

Link to comment
Share on other sites

{ 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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.