strlen() not working as intended - no errors [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to check if the $field['message'] is less than 5 letters and then check again if it's more than 100 letters, but I'm not passing the first rule - it's output the result for the $field['message'] < 5 - why?
} elseif (strlen($field['message'] < 5)) {
// some output for < 5 case
} elseif (strlen($field['message'] > 100)) {
// some output for > 100 case
}
And a result is that if I use less or more than 5 letters, I'm getting the same output for < 5 case.
What am I doing wrong here? Is it because of the array?

elseif (strlen($field['message'] < 5))
You put the parentheses in wrong places. Should be:
elseif (strlen($field['message']) < 5)

Related

How many multiple 5 is in 25 using php [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Please am looking for a code or php function that can help me know how many 5 is in 25
the code below only returns 5,10,15,20
i want something that will tell 4 instead of breaking the division point
thanks in advance
for($i = 1; $i < 25; $i++)
{
if ($i % 5 == 0)
{
echo $i;
}
};
The reason is you limit your loop to stop when it hits 25. It will only loop when value is less than 25, so as soon as it reaches 25 it wont loop and so wont get your final set of 5s.
Try this:
for ($i = 1; $i <= 25; $i++)
{
if ($i % 5 == 0) {
echo $i;
}
};
isn't this just a simple division (25%5=5)?
the reason why your code gives your 4 is because you are actually not getting to 25 (the loop stops when i=25), if you will usefor($i=0;$i<=25;$i++) the loop will still run when I=25, and so it will show you 5.

PHP Regex for password: minim 4 charaters, of which at least one CAN'T be a letter or a number [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have to make a regex for a password with the following conditions:
- minimum 4 characters
- at least 1 non-word character.
I tried this way:
$regex_password = '#(\W)+#';
$regex_password1 = '#(?i)([a-z\d])+#';
if ((!preg_match($regex_password, trim($_POST['pass']))) && (!preg_match($regex_password1, trim($_POST['pass']))) && strlen(trim($_POST['pass'])) <5){
//error
}
And then tried to create a new account with the password: "pas" and it's working, so there's something wrong with my regex. Can someone help?
You just have to change your && to || i.e. if any of condition false then it will echo an error
if ((!preg_match($regex_password, trim($_POST['pass']))) || (!preg_match($regex_password1, trim($_POST['pass']))) || strlen(trim($_POST['pass'])) <5){
echo "error";
}
else
{
echo "ok";
}

If a variable is something and another variable is something else do, PHP [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm doing some PHP traning, and wanted to make my own copy of "Rock, Paper and Scissors" and then I'd like to compare the results from the user and the computer, and I tried with this code:"
if($choice = 'Rock' and $pcSelect == 'Scissors'){
echo "You win!";
}
Plz help me,
Magn0053
You are assigning 'Rock' to $choice with =. You need to test equality with == like in the second conditional.
Use && instead of and in your if
if($choice == 'Rock' && $pcSelect == 'Scissors'){
echo "You win!";
}

PHP - Trouble with Brakeing from a loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Question First: Am i going about this the wrong way?
I had trouble braking from my for each with a conditional if inside. I wrote this just to save CPU Time yet i could not brake from my foreach.
I am trying to comply with with Active Directory strict password enforcement and i don't believe in having maximum length passwords. As i have found some websites have Maximum length requirements.
foreach(count_chars($new_password, 1) as $key => $value){//Strength Test Results can be derived from $value
if(!ctype_upper(chr($key))){$check_upper=1;}//if Upper-case
if(!ctype_lower(chr($key))){$check_lower=1;}//if Lower-case
if(!ctype_digit(chr($key))){$check_digit=1;}//if Numeric
if(!ctype_punct(chr($key))){$check_punct=1;}//if Symbol
if($check_upper + $check_lower + $check_digit + $check_punct>= 3){
break;
}//Save us from checking the entire string
}
You use the wrong keyword! It should mean break
foreach(count_chars($new_password, 1) as $key => $value){//Strength Test Results can be derived from $value
if(!ctype_upper(chr($key))){$check_upper=1;}//if Upper-case
if(!ctype_lower(chr($key))){$check_lower=1;}//if Lower-case
if(!ctype_digit(chr($key))){$check_digit=1;}//if Numeric
if(!ctype_punct(chr($key))){$check_punct=1;}//if Symbol
if($check_upper + $check_lower + $check_digit + $check_punct>= 3){
break;
}//Save us from checking the entire string
}
Well that's simple: break
Brake is what a car does.
http://php.net/manual/en/control-structures.break.php

Why the output total of ord got different value? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
**Update**
Fixed : http://codepad.org/6pB0WUm5
by http://stackoverflow.com/users/476/deceze
I have two codes, basically is same, but why, I got different output?
function domain_value($domain)
{
$split_domain = str_split($domain);
$ord_count = NULL;
foreach($split_domain as $key=>$value)
{
$ord_count += ord($value);
}
return $ord_count;
}
echo domain_value('abc');
and
echo ord('a')+ord('b')+ord('c');
Output
first output : 294
second output: 98
And what happens when you use:
echo ord('a')+ord('b')+ord('c');
which is probably what you intended in that second one :-)
In other words, you appear to have left the ord of the final part of the expression.
In fact, the only way you would normally get 98 would be with:
echo ('a')+ord('b')+('c')
(with ord only on the second term) so you may want to check again. If, as you seem to now indicate, you're using ord on each term, that works fine for me.
Both of them return 294 from 97 + 98 + 99, the ASCII values for a, b and c.

Categories