Page 1 of 1
Help! PHP class assignment
Posted: Sat Jun 21, 2014 3:51 pm
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.
Re: Help! PHP class assignment
Posted: Sat Jun 21, 2014 4:17 pm
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?
Re: Help! PHP class assignment
Posted: Sat Jun 21, 2014 4:59 pm
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.";
}
Re: Help! PHP class assignment
Posted: Sat Jun 21, 2014 5:59 pm
by Celauran
What's with the leading not before the regex?
Re: Help! PHP class assignment
Posted: Sat Jun 21, 2014 6:10 pm
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.";
}
}
Re: Help! PHP class assignment
Posted: Mon Jun 23, 2014 6:16 am
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.)
Re: Help! PHP class assignment
Posted: Mon Jun 23, 2014 6:23 am
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).