Checking the Value of Numbers Using Classes in PHP
class Check { var $num; var $text; var $abc; function Check($num) { if($angka % 2 == 0) { $this->text = $num.
' is an even number'; $this->abc = 'add text is: ABC'; } else { $this->text = $num.
' is an odd number'; $this->abc = 'add text is: DEF'; } } function gtext() { return $this->text; } function gabc() { return $this->abc; } } $cek = new Check(); $cek->cek(10000); $teks = $cek->gtext(); $abcs = $cek->gabc(); echo $teks.
" - ".
$abcs; Explanation of the code above is: 1.
class name is Check 2.
properties of the class are: * $ num * $ Text * $ Abc 3.
constructor from the class is function check ($num) Constructor is being called the first time in a class.
It poses a program code to check whether the number is entered including the number is even or odd.
Results of checking the change the value taken by 2 the next two methods, namely gtext and gabc.
4.
gtext -> retrieve the return value of $this->text 5.
gabc -> retrieve the return value of $this->abc then to call the class using the command: $check = new Check(); then enter the number you want to check whether even or odd $check->check (10); Then taken the change value, for the even / odd use $check->gtext(); while for the additional text using $check->gabc(); and the result of taking these values are written to the browser with command echo.
Similarly, little information on how to use the class to check whether a number is odd or even.
Hopefully helpful.