if( ( ) and ( ) ) not working
The following script snippet does not seem to work and I don't know why:
// go to correct.php only if length between 8 and 11
$get_length = strlen( $_POST["phone"] );
if( ( $get_length >= 8 ) and ( $get_length <= 11 ) ) {
header("Location: correct.php");
} else {
header("Location: incorrect.php");
}
Even if the length is 6, it goes to correct.php开发者_StackOverflow中文版.
No, *AND*
, *and*
and *&&*
is the same*. You have to debug your variable:
var_dump($get_length);
or maybe even better:
var_dump($_POST);
* = in this case. Check http://www.php.net/manual/en/language.operators.precedence.php
This is kind of question which can be answered by the programmer only.
In fact, most of questions a programmer have to deal with, are of this kind.
A technique that lets to get an answer is called debugging
The main goal of debugging is to get as much info as possible - all posible error messages, and - most important part - actual values of variables..
By watching your code you have no idea of the actual values. But by running it and making it print values, you can see what's going on and guess the reasons or ask proper question on stackoverflow.
Make your code this way and post here result:
ini_set('display_errors',1);
error_reporting(E_ALL);
echo "<pre>";
$get_length = strlen( $_POST["phone"] );
var_dump( $_POST["phone"],$get_length);
if( ( $get_length >= 8 ) and ( $get_length <= 11 ) ) {
echo ("Location: correct.php");
} else {
echo ("Location: incorrect.php");
}
exit;
You code is working correctly.
Please check the data in $_POST.
Is it same as you are saying?
use echo $_POST['phone'] to check the real value of stirng and count the length.
It seems like your $_POST["phone"]
is not set.
Debug your script as follows:
- Check if the
<form>
contains element withname="phone"
<input type="text" name="phone">
Check the form action and method too it should point to the file where your code is placed to check length. form method should be
post
.Check it the
$_POST['phone']
is set byvar_dump($_POST)
After following above debugging step you will get where is the issue.
Your If condition is okeay and does not have any issue.
Thanks
Maybe you should try using "&&" instead of "and".
http://www.w3schools.com/PHP/php_operators.asp
try ?:
if( ( $get_length >= 8 ) && ( $get_length <= 11 ) ) {
精彩评论