Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何编写代码来控制两个相互依赖的变量_Php_Html_Forms - Fatal编程技术网

Php 如何编写代码来控制两个相互依赖的变量

Php 如何编写代码来控制两个相互依赖的变量,php,html,forms,Php,Html,Forms,我想把使用php在网站上注册参与者的年龄限制在16岁及以上,我有一个if声明说 if ($DOBYear === '1999' && $DOBMonth === 'November'|| 'December') { echo('You have to be at least 16 years to register'); } 当选择11月或12月作为$DOBMonth值时,这似乎不起作用,无论$DOBYear是否为1999年,它都会回显“您必须至少注册16年”。如何使这

我想把使用php在网站上注册参与者的年龄限制在16岁及以上,我有一个if声明说

if ($DOBYear === '1999' && $DOBMonth === 'November'|| 'December') {
    echo('You have to be at least 16 years to register');
}

当选择11月或12月作为
$DOBMonth
值时,这似乎不起作用,无论
$DOBYear
是否为1999年,它都会回显“您必须至少注册16年”。如何使这两个变量相互依赖。

将您的支票更改为以下内容:

if ($DOBYear === '1999' && ($DOBMonth === 'November'|| $DOBMonth === 'December'))
所以你可以确定,它必须是那一年和其中的一个月

更好的方法是计算年龄,并检查:

$ageDt = new DateTime($age);
$nowDt = new DateTime();
$diff = $nowDt->diff($ageDt);
$age = $diff->y;

您需要分别检查这两种组合。检查:

  • 一九九九年及十一月

  • 一九九九年及十二月

例如:

if (($DOBYear === '1999' && $DOBMonth === 'November') || ($DOBYear === '1999' && $DOBMonth === 'December'))
试试这个:

// assumes you have a DOBday, otherwise drop this and the j
$dob = DateTime::createFromFormat(
    'Y F j',
    $DOBYear. ' ' . $DOBMonth . ' ' . $DOBday
);
$cutoff = new DateTime();

$cutoff->sub('P16Y'); // take off the 16 years.

if ($cutoff < $dob) {
    echo 'You have to be at least 16 years to register';
}
//假设您有一天的时间,否则请删除这个和j
$dob=DateTime::createFromFormat(
‘Y F j’,
$DOBYear.''.$DOBMonth.'.$DOBday
);
$cutoff=新的日期时间();
$cutoff->sub('P16Y');//去掉16年。
如果($cutoff<$dob){
echo“你必须至少16岁才能注册”;
}

您需要更改您的if,请尝试:

if ($DOBYear === '1999' && $DOBMonth === 'November' || $DOBYear === '1999' && $DOBMonth === 'December'){
echo('You have to be at least 16 years to register');
}


这可能就是您想要的:您是否意识到,使用这种结构,您必须每月更新代码?我们是自动化的程序员。
dob===Nov | | | Dec
将作为
dob===(Nov | | | Dec)
->
dob==true
false
,导致整个
if
失败。Daan我知道这一点。该网站用于举办年度活动。我对编程非常陌生,所以我一定会考虑到这一点。非常感谢。
if ($DOBYear === '1999' && ($DOBMonth === 'November' || $DOBMonth === 'December')){
echo('You have to be at least 16 years to register');
}