Sedikit Mengenal Operator di PHP

Written By Abdul Wakhid on 16 Maret 2011 | 09.39.00

tkpOK, langsung saja kita kenalan dengan operatornya Open-mouthed smile. Untuk yang pertamaX mantra buat Aritmatik’nya dulu.. he.. Open-mouthed smile

<?php
$a=1;
$b=3;
$c=$a+$b;
$d=$a-$b;

print("A = ".$a.'<br>');
print("B = ".$b.'<br>');
print("A + B = ".$c.'<br>');
print("A - B = ".$d.'<br>');
print("A / B = ".$a/$b.'<br>');
print("A mod B = ".$a%$b.'<br>');
?>

Kalau yang ini, contoh mantra buat perbandingannya ; Open-mouthed smile

<?php
//untuk string
  $a = ("AKU"=="AKU")? "true":"false";
  $b = ("AKU"=="KAMU")?"true":"false";

//untuk integer & double
  $c = (1.785==1.785)? "true":"false";
  $d = (5 != 5)?"true":"false";

//untuk boolean 
  $e = (true == true)?"true":"false";
  $f = (false != false)?"true":"false";

//untuk perbandingan nilai integer 
  $g = (100<200)?"true":"false";
  $h = (100<100)?"true":"false";
  $i = (100<=100)?"true":"false";
  $j = ( -1 > 1 )?"true":"false";
 
  print('<b>'."Contoh Operasi perbandingan di PHP :".'</b><br>');
  //untuk string
  print("AKU == AKU = ".$a.'<br>');
  print("AKU == KAMU = ".$b.'<br>');
  //untuk integer & double
  print("1.785 == 1.785 = ".$c.'<br>');
  print("5 != 5 = ".$d.'<br>');
  //untuk boolean
  print("true == true = ".$e.'<br>');
  print("false != false = ".$f.'<br>');
  //untuk perbandingan nilai integer
  print("100 < 200 = ".$g.'<br>');
  print("100 < 100 = ".$h.'<br>'); 
  print("100 <= 100 = ".$i.'<br>');
  print("-1 > 1 = ".$j.'<br>'); 
?>

OK, gitu aja gan.., semoga membantu.. Open-mouthed smile

0 komentar:

Posting Komentar

Don't Spam Please !