This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
For some odd reason, I can NOT debug this for the life of me..
$this->input->post('post_page') = 10.... I echo the variable and it does print 10 to the screen.
This keeps returning false when it should be true..
So... can someone help me out with this? I tried putting individual parentheses around each check AND changing the || to OR.. still nothing.
Here is my code nonetheless:
if($this->input->post('post_page') <> 10
|| $this->input->post('post_page') <> 25
|| $this->input->post('post_page') <> 50
|| $this->input->post('post_page') <> 75) {
return false;
}
<> is equivalent to NOT EQUAL.
10 is NOT EQUAL to 25 hence it will enter the if statement and return false;
In fact, it will ALWAYS enter that if statement regardless of number
You could instead do this:
if($this->input->post('post_page') <> 10
&& $this->input->post('post_page') <> 25
&& $this->input->post('post_page') <> 50
&& $this->input->post('post_page') <> 75) {
return false;
}
In psuedo code:
IF
MY NUMBER IS NOT 10
AND IT IS NOT 25
AND IT IS NOT 50
AND IT IS NOT 75
RETURN FALSE
Or even better:
$allowedNumbers = array(10,25,50,75);
if(!in_array($this->input->post('post_page'), $allowedNumbers)) {
return false;
}
much easier to add new items to the list too. Any number you add to the array will not return false.
PSEUDO CODE for this one:
ALLOWED NUMBERS ARE 10,25,50,75
IF(MYNUMBER IS NOT IN THE LIST OF ALLOWED NUMBERS)
RETURN FALSE
I think you should use
if(!($this->input->post('post_page') == 10
|| $this->input->post('post_page') == 25
|| $this->input->post('post_page') == 50
|| $this->input->post('post_page') == 75)) {
return false;
}
Translated in boolean:
false or true or true or true.
The expression is true, therefore it returns false.
( #
(A is not B) # IS ALWAYS TRUE
OR (A is not C) # --------------------------
OR (A is not D) # Unless 2 conditions (AND):
OR (A is not E) # . A is not B
) # . B = C = D = E
So yes, in your case, this will always be FALSE, sorry for you.
Related
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 6 years ago.
Improve this question
I have a form, but I am having trouble getting the strlen function to work.
Below is an example of the code - there is validation further down.
I've commented out the code that isn't working. Basically, all I want to do with this section of code is determine that the passwords match, and are more than 7 characters long.
Can anyone help?
if (isset($_POST['formName']) && $_POST['formName'] == "addUser") {
if ( ( $_POST['frmName'] != '') &&
($_POST['frmSurname'] != '') &&
($_POST['frmEmail'] != '') &&
($_POST['frmPassword1'] != '') ) {
if ($_POST['frmPassword1'] != $_POST['frmPassword2'] ) {
echo "Passwords do not match!";
}
/* if (strlen( ($_POST['frmPassword1']) < 7 ) {
echo "Passwords much be a minimum of 7 characters";
} */
Look at your ():
strlen( ($_POST['frmPassword1']) < 7 )
a b b a
^-----strlen-------------------^
You're not testing the length of the $_POST value, you're doing strlen on the boolean result of foo < 7, which will always be 0/1:
php > var_dump(strlen(true), strlen(false));
int(1)
int(0)
YOu need:
if (strlen($_POST['frmPassword1']) < 7) {
a b b a
Note the labels on the ().
This is where its messed up:
if (strlen( ($_POST['frmPassword1']) < 7 ) {
Let's start that statement over.
First you want the string represented by form field frmPassword1:
$_POST['frmPassword1']
Then you want the string length:
strlen($_POST['frmPassword1'])
Then you want to compare it to less than 8 because you specifically asked for more than 7 characters. Therefore, your expression would be:
strlen($_POST['frmPassword1']) < 8
Now make that a complete condition like so:
if( strlen($_POST['frmPassword1']) < 8 ){
//insert relevant code here telling users password is too short
}
Now you have a working block of code.
You are missing end )
if (strlen( ($_POST['frmPassword1']) < 7 ) {
1 2 3 3 2 # 1 is missing
So it would be
if (strlen( ($_POST['frmPassword1']) < 7 ) ){
1 2 3 3 2 1
NOTE : In your question you have mentioned that passwords match, and are more than 7 characters. So use <= (less than or equal).
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
How do I validate a string against the following rules:
$string = 'int(11)';
Rule: first 4 characters MUST be 'int('
Rule: next must be a number between 1 and 11
Rule: next must be a ')'
Rule: Everything else will fail
Experienced PHP Developer here - Regular Expressions are not my strong point..
Any help or suggestions welcome.
Thanks guys..
if (preg_match('/int\((\d{1,2})\)/', $str, $matches)
&& (int) $matches[1] <= 11 && (int) $matches[1] > 0
) {
// ... do something nice
} else {
echo 'Failed!!!'
}
Or if you want to not use the pReg library (can be faster):
$str = 'int(11)';
$i = substr($str, 4, strpos($str, ')') - 4);
if (substr($str, 0, 4) === 'int('
&& $i <= 11
&& $i > 0
) {
echo 'succes';
} else {
echo 'fail';
}
use this regular expression int\((\d|1[01])\)
int\(( first rule
(\d|1[01]) second rule
\) third rule
This regular expression is even smaller:
int\((\d1?)\)
or without the capturing group (if you don't need to retrieve the numeric value).
int\(\d1?\)
I'm curious about how PHP handles conditional statements / order of operations with nesting. If I use the following if condition:
if(x == (1 || 2))
{
// do something
}
I would expect it to behave the same as
if(x == 1 || x == 2)
{
// do something
}
...but it doesn't. My first example seems like it would be a handy shorthand that makes pretty good sense, but it doesn't do what I expect. Can anyone shed some light on the issue? What exactly does PHP do with my first statement?
So for this piece of code:
if ($x == ( 1 || 2))
{
// do something
}
In PHP, any non-zero number is considered true. Disclaimer: This fact isn't necessarily true in other languages. So in PHP, 0 is the only number considered false. So you're asking if $x == true in the above piece of code.
Hence, whenever $x is any number other than 0 the statement inside the if will resolve as true. However, when $x = 0 then that is equivalent to saying false == true which of course will resolve as false.
This article might help: PHP: Booleans
Your shorthand is logically invalid. In almost every case you'll have to write out the full logical cases for all possibilities you want to test for.
I say 'almost' because in PHP you can do something ridiculous like:
if( in_array($x, array(1,2)) ) {
// code!
}
x == (1 || 2)
evaluates like this:
(1 (if its false) then testing for 2, if not, the expression returns true)
now it will become:
if(x==true)?
Another example taken from (PHP.NET):
// foo() will never get called as those operators are short-circuit
$b = (true || foo());
See here about the precedence of an operator
http://php.net/manual/en/language.operators.precedence.php
It will behave the same as math (think BEDMAS), with the brackets being executed first. So your example is behaving as:
if (x == ( 1 || 2)) {
//code
}
and because 1 and 2 are both non-zero values (thus both true), you get:
if (x == true) {
//code
}
Unfortunately to get what you want you'll need:
if (x == 1 || x == 2) {
//code
}
how would it make sense? you are asking the computer to execute the following logical expression:
if x == (1 || 2) which is same as x == (the result of 1 || 2)
so your expression would be x == true since 1 || 2 would return true
computers do whatever you tell them to do
if(x == (1 || 2))
{
// do something
}
OR and AND operations come back with TRUE or FALSE
you statement says - if x equals (true) - as 1 or 2 will always be true
- this just doesn't make sense...
In first if evaluate result of 1||2 and check that it equal to x
In second its like this or this or this
however in first you can see that var_dump(1 || 2) returns every time true so
$x = 3;
var_dump($x == 1 || 2);
if($x == 1 || 2){
echo 'inside if';
}
is also true so it will print inside if even $x is 3
so imo second way is way to go
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
var_dump((int) true); // returns 1
var_dump(true > 0); // returns true
var_dump(true > - 1); // returns false
var_dump(1 > - 1); // returns true
Can somebody explain me in detail what the results of those two above are because it is comparing boolean with integer and it is confusing to me.
> is a numerical comparison operator, so PHP does a "loose comparison" and converts true to 1 or -1 in each case.
I imagine the following is happening internally:
When you ask if (true > 0), PHP first loosely tests if true==0, returns false, then it substitutes 1 for true and the comparison returns true.
When you ask if (true > -1), PHP first loosely test if true==-1, returns true, which implies (true > -1) must be false.
In short: don't do things like this.
This answer no longer satisfies the question as OP changed the question.
You shouldn't normally compare operands of different types, if you have some code that does this there is probably a mistake. The result may not make a lot of sense.
If you want to know the answer, just try it:
var_dump(true > 0); // gives true
var_dump(true > -1); // gives false
I figured out what is happening here by considering what Blazemonger has written.
Consider this:
var_dump(true == 0); // returns false
var_dump(true == 1); // true
var_dump(true == -1); // true
var_dump(true == -2); // true
So true is anything BUT zero.
For the > operator, PHP first tests for equality (==), so
true == X which is always true except for X = 0.
If the comparison yields true, then it cannot be greater, so true > X is always false except for X = 0.
Therefore:
var_dump(true > 0); // true
var_dump(true > 1); // false
var_dump(true > -1); // false
var_dump(true > -2); // false
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
PHP: Shortest way to check if a variable contains positive integer?
How do I know, is a given variable a number without residue (and not a negative number)?
Like we have numbers (each number is a variable): 5; 6.416; -76; 254.
Its better to have some function, which would give true or false, like:
number "5" is true
6.416 is false (has residue "416")
-76 is false
254 is true.
Thanks.
if ( (int)$num == $num && (int)$num > 0 ){
return true;
}
This should do it:
function is_positive_int($n) {
return $n && abs(intval($n)) == $n;
}