Identifikasi Angka

Berikut contoh script PHP lain yang bisa dijalankan di CLI. Sekaligus menjawab tantangan lanjutan dari teman saya itu. Selamat mencoba.

<?php
	$stdin = fopen('php://stdin', 'r');
	echo "Masukkan angka : ";
	$number = trim(fgets($stdin));
	$identifier = "";

	if ( !is_numeric($number) || ($number != (int)$number) || $number < 0) {
		$identifier = " tidak terdefinisikan.n"; 
	} else if ($number == 0) {
		$identifier = " adalah bilangan nol.n";
	} else if ($number % 2 == 1) {
		$identifier = " adalah bilangan ganjil.n";
	} else if ($number % 2 == 0) {
		$identifier = " adalah bilangan genap.n";
	}
	
	echo $number . $identifier;
	fclose($stdin);
?>

Berikut hasilnya ketika script dijalankan:

hasil eksekusi

Published by Eric Gunawan

Happiness Engineer. WordPress Ambassador. Remote Worker. Soccer News Follower. Movie Lover. Proud Father. Lucky Husband.

2 thoughts on “Identifikasi Angka

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: