Jump to content

Need help with PHP problem

CripDawg
Go to solution Solved by fizzlesticks,

thanks at least it works now but  it just prints "numbers are equal" because they are both the same value  

That's why it needs to be the first check done.

if(strlen($num1) == 0 && strlen($num2) == 0) {}else if(num1 == num2){}else if(num1 > num2){}else //num1 < num2{}

i have this PHP activity to do for my software design class we have a program that our teacher gave us that has two text boxes you put numbers in and it says witch one is bigger our job is to make it say "no data is entered" when both boxes are left blank the work sheet says the function "strlen" would be useful i figured that it shows the amount of characters in a string and tried ading my own IF statement but it doesn't work......  any help would be appreciated 

 

Code:

 

<?php
 $num1 = $_POST['number1'];
 $num2 = $_POST['number2'];
 
 if ($num1 == $num2)
 
 {
  echo "Numbers are equal" ;
   }
 
 else
 
   {
 
  if ($num1 > $num2)
 
   { 
echo "$num1 is bigger" ;  
   }
 
 else
 
   {
echo "$num2 is bigger";
   }
  
 if ($num1 && $num2 == sterlen == NULL
{
echo "no data was entered";
}
 
 } 
?>
 
Link to comment
Share on other sites

Link to post
Share on other sites

Its been a while since I've done some basic PHP coding, but I did notice something with your code. In your description you mentioned "strlen" which would be string length, but in your code you have it down as "sterlen" which doesn't look right to me. So possibly a spelling mistake?

Link to comment
Share on other sites

Link to post
Share on other sites

strlen is a function that must be passed a string. Doing strlen($num1) will give the length of the string $num1.

So you will need to doing something like:

if(strlen($num1) == 0 && strlen($num2) == 0) {    //do whatever}

It should also be the first check you do.

 

Also $num1 and $num2 are strings, to figure out which one has a bigger number you must convert them to numbers using intval($num1)

 

And lastly, please use code tags.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

strlen is a function that must be passed a string. Doing strlen($num1) will give the length of the string $num1.

So you will need to doing something like:

if(strlen($num1) == 0 && strlen($num2) == 0) {    //do whatever}

It should also be the first check you do.

 

Also $num1 and $num2 are strings, to figure out which one has a bigger number you must convert them to numbers using intval($num1)

 

And lastly, please use code tags.

thanks at least it works now but  it just prints "numbers are equal" because they are both the same value  

Link to comment
Share on other sites

Link to post
Share on other sites

thanks at least it works now but  it just prints "numbers are equal" because they are both the same value  

That's why it needs to be the first check done.

if(strlen($num1) == 0 && strlen($num2) == 0) {}else if(num1 == num2){}else if(num1 > num2){}else //num1 < num2{}

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

You can also use 

if(isset($num1) && isset($num2)){}

Which checks if variables are set and aren't null.

Curing shitposts by shitposts

Link to comment
Share on other sites

Link to post
Share on other sites

 

That's why it needs to be the first check done.

if(strlen($num1) == 0 && strlen($num2) == 0) {}else if(num1 == num2){}else if(num1 > num2){}else //num1 < num2{}

Thanks dude works perfectly you've been a big help :)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×