PDA

View Full Version : PHP Code Help


HexOnxOnx
Wed 17th Dec '03, 3:50pm
I have the following code:

if(strlen($subject)<5){
$pm_subject_error = 1;
$subject_html = "Your message subject must be at least 5 characters in length.";
}

I want this to give the message indicated if someone does not include a subject at all. I thought that this code should work yet it allows a message to be sent without a subject which makes the message unreadable. The above only works if someone enters a subject between 1 and 4 letters. Can anyone help me fix this please?

Thanks.

merk
Wed 17th Dec '03, 8:05pm
if(strlen($subject)<5 OR $subject="" OR !isset($subject)){
$pm_subject_error = 1;
$subject_html = "Your message subject must be at least 5 characters in length.";
}


I think :)

Symen_4ab
Mon 22nd Dec '03, 2:28pm
if(strlen($subject)<5 OR $subject="" OR !isset($subject)){
$pm_subject_error = 1;
$subject_html = "Your message subject must be at least 5 characters in length.";
}



Don't forget the "double =" sign :) (subject==)


if((strlen($subject)<5 || $subject=="" || !isset($subject))){
$pm_subject_error = 1;
$subject_html = "Your message subject must be at least 5 characters in length.";
}

HexOnxOnx
Mon 22nd Dec '03, 2:36pm
OK I will add that in, thanks!

TiLaser
Tue 23rd Dec '03, 8:14pm
Instead of using isset(), it's often a good idea to use empty() as variables can be set but still be empty.



$subject = ""

if (! isset($subject))
echo "!isset";

if (empty($subject)
echo "empty";



The result would be "empty".

merk
Wed 24th Dec '03, 1:44am
Interesting !

I normally used isset($var) AND/OR $var=="", empty() would replace both of those tests?

Symen_4ab
Wed 24th Dec '03, 8:29am
According to this page (http://ch2.php.net/manual/en/function.empty.php), it checks whenever the variable is = 0, empty, or unset.
For an example :
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}

It's perfect for HexOnxOnx's script, but there is this "= 0" thing which makes it very different compared to isset.

TiLaser
Wed 24th Dec '03, 7:30pm
Even so,



if (empty() && !=0)



is still 2 comparisons as opposed to 3.

Symen_4ab
Wed 24th Dec '03, 10:04pm
Hey, I was not saying that "my" function was better than yours (well, it's neither mine nor yours), I was just mentionning, as a warning, that the empty() function returns false if the variable = 0.

As I said, it's perfect for HexOnxOnx's script.