Help! PHP class assignment

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Becky
Forum Newbie
Posts: 5
Joined: Sat Jun 21, 2014 3:46 pm

Help! PHP class assignment

Post by Becky »

Yes, this is a homework assignment. I have spent 20 hours trying to figure this one (and one other similar one) out and I am not asking that you do it but if you could give me a hint! I can't get it to move onto the ELSE part of the code. I am sure it it something silly but after all these hours I am burning out. Your help would be greatly appreciated.

Code: Select all

code removed
Last edited by Becky on Mon Jun 23, 2014 6:07 am, edited 1 time in total.
Becky
Forum Newbie
Posts: 5
Joined: Sat Jun 21, 2014 3:46 pm

Re: Help! PHP class assignment

Post by Becky »

I see one error as I review this, again. As I moved the is_numeric together, I missed the preg_match pattern for the three variables. Would it be better to use a different function to make sure they are numeric?
Becky
Forum Newbie
Posts: 5
Joined: Sat Jun 21, 2014 3:46 pm

Re: Help! PHP class assignment

Post by Becky »

Ok... I am down to fixing the preg_match to be sure the 0 is not entered. I broke them down into three if statements but they still are kicking back error messages "Warning: preg_match(): Empty regular expression" . I have everything else working. Any help?

Code: Select all

	if(!is_numeric($SpeedA) || !preg_match(!'/^\-/', $SpeedA) || $SpeedA = 0){
		$DisplayForm = FALSE;
	}
	else{
		echo "Speed A must be a positive number.";
	}
	if(!is_numeric($SpeedB) || !preg_match(!'/^\-/', $SpeedB) || $SpeedB = 0){
		$DisplayForm = FALSE;
	}
	else{
		echo "Speed B must be a positive number.";
	}
	if(!is_numeric($Distance) || !preg_match(!'/^\-/', $Distance) || $Distance = 0){
		$DisplayForm = FALSE;
	}
	else{
		echo "Distance must be a positive number.";
	}	
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Help! PHP class assignment

Post by Celauran »

What's with the leading not before the regex?
Becky
Forum Newbie
Posts: 5
Joined: Sat Jun 21, 2014 3:46 pm

Re: Help! PHP class assignment

Post by Becky »

You lost me. I know the validation still isn't right but I got the rest working. I submitted the assignment as it was but any help in understanding this for the future would be helpful. I fixed my "double negative by removing the ! from the preg_match. This removed the error messages but it still doesn't stop the script if there is an error.

Code: Select all

if(!is_numeric($SpeedA) || !preg_match('/^\-/', $SpeedA) || $SpeedA = 0){
		$DisplayForm = FALSE;
	}
	else{
		echo "Speed A must be a positive number.";
		}
	if(!is_numeric($SpeedB) || !preg_match('/^\-/', $SpeedB) || $SpeedB = 0){
		$DisplayForm = FALSE;
	}
	else{
		echo "Speed B must be a positive number.";
		}
	if(!is_numeric($Distance) || !preg_match('/^\-/', $Distance) || $Distance = 0){
		$DisplayForm = FALSE;
	}
	else{
		echo "Distance must be a positive number.";
		}	
	}
Becky
Forum Newbie
Posts: 5
Joined: Sat Jun 21, 2014 3:46 pm

Re: Help! PHP class assignment

Post by Becky »

Could you please clarify the "leading not before the regex"? These are not terms used in the book.

(I have removed the original code so it doesn't end up in someone else's assignment. No need to propagate my mistakes.)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Help! PHP class assignment

Post by Celauran »

Why are you using regex to determine if the number is positive? Is that a requirement of the assignment? Makes more sense and is more efficient to check if the number is greater than 0. That would also allow you to remove the third comparison (which is currently an assignment and not a comparison).
Post Reply