PHP は加算と減算の検証コードを実装します

不言
リリース: 2023-03-23 07:24:01
オリジナル
3046 人が閲覧しました

この記事では、PHP での加算と減算の検証コードを共有しますので、興味のある方はご覧ください


<?php

header(&#39;Content-Type: image/png&#39;);
$im = imagecreatetruecolor( 200 , 50 );

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);

imagefilledrectangle($im, 0, 0, 200, 49, $white);

// The text to draw
$rand = rand( 1 ,2 ) ;
$one = rand(1,9);
$two = rand(1,9);
if( $rand == 1 ){

    $result = $one + $two;
    $text = "$one+$two=?";

}else{
    if( $one < $two ){
        list( $two , $one ) = [ $one , $two ];
    }
    $result = $one - $two;
    $text = "$one-$two=?";
}

session_start();
$_SESSION[&#39;vcode&#39;] = $result;
// Replace path by your own font path
$font = &#39;./Arvo-Regular.ttf&#39;;

//imageline( $im , 0 , rand( 1,49 ) , 199 , 30 , $grey );

// Add the text
$i = 0;
$len = strlen( $text );
while( $i < $len  ){
    if( is_numeric( $text[$i] ) ){
        imagettftext($im, 20, rand(-45,45), 20 * ($i+1) , 30, $black, $font, $text[$i]);
    }else{
        imagettftext($im, 20, 0 , 20 * ($i+1) , 30, $black, $font, $text[$i]);
    }
//echo $text[$i];
    $i ++;
}
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
ログイン後にコピー


加算と減算はこの方法で実装でき、除算と乗算も同様です。

割り算するとき 割り算ができない状況に遭遇した場合は、考えを変えて掛け算を使って解くことができます

2*3=6

6/2=3

この方法では、負の数は発生しません

関連する推奨事項:

PHP は簡単な計算機を実装します



以上がPHP は加算と減算の検証コードを実装しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!