如何在HTML文件中指示数字是奇数还是在PHP中 [英] How to indicate if a number is odd or even in PHP in an HTML file

查看:118
本文介绍了如何在HTML文件中指示数字是奇数还是在PHP中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP和mysql的新手,我们已经在HTML文件中的不同矩形中设置了练习。在这个练习中
2个矩形是

矩形5:一个输入栏,允许您输入一个四位数的整数或四个字母的字符串并存储它在一个变量$ x中。用户将把数字/字母放在一个盒子中,然后点击标有提交的按钮,以便将数字/字母输入到变量中。



矩形6:一个输出,指示用户输入的数字$ x是偶数还是奇数。

我有矩形5工作并显示变量$ xa顶部屏幕,但我似乎无法得到矩形6工作。



这是我的代码到目前为止:



<$ ($ _POST ['name']&& $ _POST ['name']!=){p $ p> function rect5if(){// rect5 if statement
if {
$ x = urldecode($ _POST ['name']);
}其他{
$ x =未设置;
}
echo $ x;
} //结束rect5

函数rect6oddeven(){
if(is_int($ x / 2)){
echo(Even);
} else {
echo(Odd);



echo< table border ='1'>
< td> Rectangle 5.rect5if()。
< form method = \post \>
输入四位数字/字母串
< input type = \text \name = \name \ maxlength = \4 \>
< input type = \submit \/>
< / form>< / td>
< ; td> Rectangle 6.rect6oddeven()。< / td>
< / tr>
< / table>;

有什么建议吗?谢谢GL

解决方案

你需要检查除以2的余数是否会给你。如果它将是0,数字是偶数,否则它是奇数。那叫做模数的运算符。我建议你使用它来学习它。



假设 $ x 是工作代码,代码:

  function rect6oddeven(){
if($ x%2 === 0) {
echo(Even);
}
else {
echo(Odd);
}
}


Im new to PHP and mysql, we have been set exercises in different rectangles in an HTML file. 2 rectangles in this exercise are

Rectangle 5: An "input field" that allows you to input a four digit integer number or a four letter string and store it in a variable "$x". A user will put the numbers/letters in a box and click on a button labelled "submit" in order to enter the number/letter into the variable.

Rectangle 6: An output that indicates if a number "$x" input by the user is even or odd.

I have got rectangle 5 to work and display variable $x a the top of the screen, however I cant seem to get rectangle 6 to work.

Here is my code so far:

function rect5if() { //rect5 if statement
    if( $_POST['name'] && $_POST['name'] != ""){
        $x = urldecode( $_POST['name'] );                
    } else {
        $x = "not set";
    }
    echo $x;
}//end of rect5

function rect6oddeven() {
    if(is_int($x/2)) {
        echo("Even");
    } else {
        echo("Odd");
    }
}

echo " <table border='1'>
<td>Rectangle 5 ".rect5if()."
<form method=\"post\" >
Enter four digit number/letter string
<input type=\"text\" name=\"name\" maxlength=\"4\">
<input type=\"submit\" />
</form></td> 
<td>Rectangle 6 ".rect6oddeven()."</td>
</tr>
</table> ";

Any suggestions? Thanks GL

解决方案

You need to check what remainder of division by 2 will give you. If it will be 0, number is even, otherwise it's odd. Operator that does that is named modulus. I suggest you googling it to learn it.

Working code, assuming $x is is given value earlier in the code:

function rect6oddeven() {
  if ($x % 2 === 0) {
    echo("Even");
  }
  else {
    echo("Odd");
  }
}

这篇关于如何在HTML文件中指示数字是奇数还是在PHP中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆