简体   繁体   English

if(()和())不起作用

[英]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. 即使长度为6,它也会进入correct.php。

No, *AND* , *and* and *&&* is the same*. 不, *AND**and**&&*相同*。 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 检查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. 但是通过运行它并使其显示值,您可以查看发生的情况并猜测原因或对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. 请检查$ _POST中的数据。

Is it same as you are saying? 和你说的一样吗?

use echo $_POST['phone'] to check the real value of stirng and count the length. 使用echo $ _POST ['phone']来检查搅拌的真实值并计算长度。

It seems like your $_POST["phone"] is not set. 似乎您的$_POST["phone"]未设置。

Debug your script as follows: 如下调试脚本:

  1. Check if the <form> contains element with name="phone" 检查<form>包含name="phone"元素

<input type="text" name="phone">

  1. 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 . 表格方法应该是post

  2. Check it the $_POST['phone'] is set by var_dump($_POST) 检查$_POST['phone']是否由var_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. 您的If条件很好,没有任何问题。

Thanks 谢谢

Maybe you should try using "&&" instead of "and". 也许你应该尝试使用“&&”而不是“和”。

http://www.w3schools.com/PHP/php_operators.asp http://www.w3schools.com/PHP/php_operators.asp

尝试?:

if( ( $get_length >= 8 ) && ( $get_length <= 11 ) ) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM