1

I am trying to write a script that includes if/else statements and functions. some background

  • $parts first character should have the letter "N"
  • $desc is suppose to be at least one character long
  • $price needs to be positive (0 or higher)

if all three of theses requirements are met then it should say "data accepted" if something is not met (one or all) the "Invalid...." needs to show.

can someone tell me what part of my script I should look at.

<?php
$parts = $_POST["parts"];
$desc  = $_POST["desc"];
$price = $_POST["price"];

$pa = substr($parts, 0, 1);
$de = strlen($desc);

if ($pa != "N")
 {echo "Invalid Part Number";}
else
 if ($de <= 1)
  {echo "Invalid Description Length";}
 else
  if ($price <= 0)
   {echo "Invalid Price";}
    else
     {echo "Data Accepted";}
?>
4
  • In what way is your script not working as you want it to? Commented Apr 6, 2011 at 3:58
  • Parse error: parse error in on line 3 Commented Apr 6, 2011 at 4:02
  • 2
    Just looking at it I would say you need to put semi-colons where they belong after your variable declarations. I'd also say that $pa != N is not going to work N should be wrapped in quotes, and then it will only check if the variable is equal to N and not a sub string of the variable is equal to N. I would look at the use of substr to get that working right. Commented Apr 6, 2011 at 4:04
  • ok I was trying to use substr to pull out the first character of the parts number so i can compare it to the letter N. substr($parts, 0,1) if I recall will start at the first character and end after that one. Commented Apr 6, 2011 at 4:08

5 Answers 5

2

The second if should be if($de==1) $de=1 will always return true.

Also add semicolons after each statement.

Sign up to request clarification or add additional context in comments.

3 Comments

do you add semicolons on the if and else lines?
I always write these as (1 == $d) then if I accidentally leave out an = and perform assignment it produces a compilation error.
@ Wes i hadn't seen it written that way before but that is something I will keep in mind @ Rasika Thanks
2

Your requirement:-

$parts first character should have the letter "N"

$desc is suppose to be at least one character long

$price needs to be positive (0 or higher)

Solution:-

$parts = $_POST["parts"];
$desc  = $_POST["desc"];
$price = $_POST["price"];

$pa = substr($parts, 0, 1);
$de = strlen($desc);

if($pa != 'N') {
    echo "Invalid Part Number";
} elseif($de < 1) {
    echo "Invalid Description Length";
} elseif($price < 0) {
    echo "Invalid Price";
} else {
    echo "Data Accepted";
}

Comments

1

the second if should be if($de < 1) you can have if($de == 1) if its always going to be one character long but this will work if its 1 or more

1 Comment

That makes a good point but wouldnt it be ($de <=1) because if it is less then on an error is produce if its more then one then its accepted
0
$parts = $_POST["parts"];
$desc  = $_POST["desc"];
$price = $_POST["price"];

2 Comments

Looks like this was intended to answer a previous revision of the question.
@Phoenix I probably thought it was a syntax error and error reporting is disabled.
0
    <?php
    $parts = '';
    $desc = '';
    $price = 0;
    if ($_POST['parts'] != 'n')
    {
    echo 'Not equal to n<br>';
    }
    else {
    'Accepted input<br>';
    }
    $desc = strlen($_POST['desc']);
    if ($desc < 1)
    {
    echo 'Input less than 1<br>';
     }
    else {
    echo 'Accepted input<br>';
    }
    if ($_POST['price'] < 0)
    {
    echo 'Input below 0<br>';
    }
    else {
    echo 'Input accepted<br>';
    }
    ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.