WAKTU
$waktu = date ("H:i:s",time());
print "Waktu sekarang adalah $waktu";
?>
WHILE
print "Contoh menggunakan pola while
";
$bila=5;
while ($bila < 20)
{
$bila +=3;
print "$bila
";
}
print "
Contoh menggunakan pola while..endwhile
";
$bila=5;
while ($bila < 20):
print "$bila ";
$bila += 3;
endwhile;
?>
Minggu, 10 Januari 2010
PHP : URUT ARRAY3
$buah = array ("pisang","apel","mangga","durian");
print "Data array sebelum di urutkan adalah :
";
foreach ($buah as $indeks => $data)
{
print "$indeks : $data
";
}
print "Data setelah di urutkan secara ascending
";
asort($buah);
foreach ($buah as $indeks => $data)
{
print "$indeks : $data
";
}
print "Data setelah di urutkan secara descending
";
arsort($buah);
foreach ($buah as $indeks => $data)
{
print "$indeks : $data
";
}
?>
asort(namaarray) untuk ascending dengan index berubah sebaliknya arsort
print "Data array sebelum di urutkan adalah :
";
foreach ($buah as $indeks => $data)
{
print "$indeks : $data
";
}
print "Data setelah di urutkan secara ascending
";
asort($buah);
foreach ($buah as $indeks => $data)
{
print "$indeks : $data
";
}
print "Data setelah di urutkan secara descending
";
arsort($buah);
foreach ($buah as $indeks => $data)
{
print "$indeks : $data
";
}
?>
asort(namaarray) untuk ascending dengan index berubah sebaliknya arsort
PHP : URUT ARRAY2
$buah = array("pepaya","apel","mangga","durian");
print "Data array sebelum di urutkan adalah :
";
foreach ($buah as $indeks => $data)
{
print "$indeks : $data
";
}
print "Data setelah di urutkan secara ascending
";
sort($buah);
foreach ($buah as $indeks => $data)
{
print "$indeks : $data
";
}
print "Data setelah di urutkan secara descending
";
rsort($buah);
foreach ($buah as $indeks => $data)
{
print "$indeks : $data
";
}
?>
krsort(namaarray) untuk ascending, snaliknya ksort(nmarray)
sort(namaarray) ascending index tetap senaliknya rsort(namaarray)
print "Data array sebelum di urutkan adalah :
";
foreach ($buah as $indeks => $data)
{
print "$indeks : $data
";
}
print "Data setelah di urutkan secara ascending
";
sort($buah);
foreach ($buah as $indeks => $data)
{
print "$indeks : $data
";
}
print "Data setelah di urutkan secara descending
";
rsort($buah);
foreach ($buah as $indeks => $data)
{
print "$indeks : $data
";
}
?>
krsort(namaarray) untuk ascending, snaliknya ksort(nmarray)
sort(namaarray) ascending index tetap senaliknya rsort(namaarray)
PHP : URUT ARAY 1
$teman = array ("2"=>"Meilana", "4"=>"Ali", "3"=>"Zezen", "1" => "Udin");
print "Data aslinya adalah :
";
foreach ($teman as $tampil => $hasil)
{
print "$tampil = $hasil
";
}
print "Pengurutan data secara ascending
";
ksort($teman);
foreach ($teman as $tampil => $hasil)
{
print "$tampil = $hasil
";
}
print "Pengurutan data secara descending :
";
krsort($teman);
foreach ($teman as $tampil => $hasil)
{
print "$tampil = $hasil
";
}
?>
print "Data aslinya adalah :
";
foreach ($teman as $tampil => $hasil)
{
print "$tampil = $hasil
";
}
print "Pengurutan data secara ascending
";
ksort($teman);
foreach ($teman as $tampil => $hasil)
{
print "$tampil = $hasil
";
}
print "Pengurutan data secara descending :
";
krsort($teman);
foreach ($teman as $tampil => $hasil)
{
print "$tampil = $hasil
";
}
?>
PHP : UPLOAD2
/* Simple Upload manager
Script Version 0.4, copyright RRWH.com 2004
This script is distributed under the licence conditions on the website http://rrwh.com/scripts.php
This script is a simple interface to allow you to upload files to a configured directory on your server. It will additionally automatically create sub-directories if you want it to. Additionally, It will let you do a directory listing of the base directory or any specified sub-directory.
You only need to modify the $password and $dir variable and ensure that the directory exists on the server and the permission is set to 777
You may want to modify the $types if you wish to allow other file types to be uploaded - Be careful not to allow dangerous files to be uploaded to your server.
*/
$pw = 'fred';
$dir = "D:/xampplite/htdocs/"; //Change this to the correct dir RELATIVE TO WHERE THIS SCRIPT IS, or /full/path/
//MIME types to allow, Gif, jpeg, zip ::Edit this to your liking
$types = array("text/plain","application/pdf","image/png","image/x-png","audio/wav","image/gif","image/jpeg","image/pjpeg","application/x-zip-compressed");
// Nothing to edit below here.
//Function to do a directory listing
/*function scandir($dirstr) {
echo "
\n";\n";
passthru("ls -l -F $dirstr 2>&1 ");
echo "
}
*/
//Check to determine if the submit button has been pressed
if((isset($_POST['submit'])) and ($_POST['PW'] == $pw)){
//Shorten Variables
$tmp_name = $_FILES['upload']['tmp_name'];
$new_name = $_FILES['upload']['name'];
$path = $_POST['subdir'];
// $fullpath = $_SERVER['DOCUMENT_ROOT'].'/nc/';
$fullpath = $_SERVER['DOCUMENT_ROOT'].'/up_download/';
// $fullpath = "D:/xampplite/htdocs.'/up_download/' ";
$fullpath = str_replace("..", "", str_replace("\.", "", str_replace("//", "/", $fullpath)));
$clean_name = ereg_replace("[^a-z0-9._]", "", str_replace(" ", "_", str_replace("%20", "_", strtolower($new_name) ) ) );
// lets see if we are uploading a file or doing a dir listing
if(isset($_POST['Dir'])){
echo "Directory listing for $fullpath\n";
scandir("$fullpath");
}else{
//Check MIME Type
if ((in_array($_FILES['upload']['type'], $types)) and (!file_exists($fullpath.$clean_name))){
// create a sub-directory if required
if (!is_dir($fullpath)){
mkdir("$fullpath", 0755);
}
//Move file from tmp dir to new location
move_uploaded_file($tmp_name,$fullpath . $clean_name);
echo "$clean_name of {$_FILES['upload']['size']} bytes was uploaded sucessfully to $fullpath
";
echo "mau lihat isinya";
}else{
//Print Error Message
echo "File {$_FILES['upload']['name']} Was Not Uploaded - bad file type or file already exists
";
//Debug
$name = $_FILES['upload']['name'];
$type = $_FILES['upload']['type'];
$size = $_FILES['upload']['size'];
$tmp = $_FILES['upload']['name'];
echo "Name: $name
Type: $type
Size: $size
Tmp: $tmp";
}
}
} else {
echo 'Ready to upload your file';
} ?>
PHP : UPLOAD1
echo mime_content_type('freeundelete.exe') . "\n";
echo mime_content_type('model.zip') . "\n";
//echo mime_content_type('terakhir.php') . "\n";
//echo mime_content_type('alias.eko') . "\n";
//echo $_SERVER['DOCUMENT_ROOT'];
/*if ($submit)
{
$doc_directory="D:/xampplite/htdocs";
$my_file="./up_download/".$userfile_name;
$copy_path=$doc_directory.$my_file;
if (!move_uploaded_file($userfile, $copy_path))
{
print "upload gagal";
}
else
{
print "upload sukses";
}
}
print "";
*/
?>
echo mime_content_type('model.zip') . "\n";
//echo mime_content_type('terakhir.php') . "\n";
//echo mime_content_type('alias.eko') . "\n";
//echo $_SERVER['DOCUMENT_ROOT'];
/*if ($submit)
{
$doc_directory="D:/xampplite/htdocs";
$my_file="./up_download/".$userfile_name;
$copy_path=$doc_directory.$my_file;
if (!move_uploaded_file($userfile, $copy_path))
{
print "upload gagal";
}
else
{
print "upload sukses";
}
}
print "";
*/
?>
PHP : UPLOAD
$pw = 'eko';
//direktori dimana skrip di jalankan
$dir = "D:/xampplite/htdocs/";
//tipe data yang di perbolehkan untuk upload
$types = array("application/octet-stream","text/plain","application/pdf","image/png","image/x-png","audio/wav","audio/mpeg","image/gif","image/jpeg","image/pjpeg","application/x-zip-compressed");
if((isset($_POST['submit'])) and ($_POST['pass'] == $pw))
{
//Shorten Variables
$tempor = $_FILES['upload']['tmp_name'];
$new_nama =$_FILES['upload']['name'];
$path = $_POST['subdir'];
$fullpath = $_SERVER['DOCUMENT_ROOT'].'/up_download/';
$fullpath = str_replace("..", "", str_replace("\.", "", str_replace("//", "/", $fullpath)));
$nama_baru = ereg_replace("[^a-z0-9._]", "", str_replace(" ", "_", str_replace("%20", "_", strtolower($new_nama) ) ) );
if ((in_array($_FILES['upload']['type'], $types)) and (!file_exists($fullpath.$nama_baru)))
{
move_uploaded_file($tempor,$fullpath . $nama_baru);
print "File $nama_baru {$FILES['upload']['size'] } bytes telah sukses di upload ke $fullpath
";
print "Mau lihat isinya
";
}
else
{
print "File {$_FILES[upload][name]} tidak bisa di upload, mungkin karena salah tipe data atau data telah ada.
";
$name = $_FILES['upload']['name'];
$type = $_FILES['upload']['type'];
$size = $_FILES['upload']['size'];
$tmp = $_FILES['upload']['name'];
print "Nama: $name
Type: $type
Size: $size
Tmp: $tmp
";
print "lihat isi folder";
}
}
print "
//direktori dimana skrip di jalankan
$dir = "D:/xampplite/htdocs/";
//tipe data yang di perbolehkan untuk upload
$types = array("application/octet-stream","text/plain","application/pdf","image/png","image/x-png","audio/wav","audio/mpeg","image/gif","image/jpeg","image/pjpeg","application/x-zip-compressed");
if((isset($_POST['submit'])) and ($_POST['pass'] == $pw))
{
//Shorten Variables
$tempor = $_FILES['upload']['tmp_name'];
$new_nama =$_FILES['upload']['name'];
$path = $_POST['subdir'];
$fullpath = $_SERVER['DOCUMENT_ROOT'].'/up_download/';
$fullpath = str_replace("..", "", str_replace("\.", "", str_replace("//", "/", $fullpath)));
$nama_baru = ereg_replace("[^a-z0-9._]", "", str_replace(" ", "_", str_replace("%20", "_", strtolower($new_nama) ) ) );
if ((in_array($_FILES['upload']['type'], $types)) and (!file_exists($fullpath.$nama_baru)))
{
move_uploaded_file($tempor,$fullpath . $nama_baru);
print "File $nama_baru {$FILES['upload']['size'] } bytes telah sukses di upload ke $fullpath
";
print "Mau lihat isinya
";
}
else
{
print "File {$_FILES[upload][name]} tidak bisa di upload, mungkin karena salah tipe data atau data telah ada.
";
$name = $_FILES['upload']['name'];
$type = $_FILES['upload']['type'];
$size = $_FILES['upload']['size'];
$tmp = $_FILES['upload']['name'];
print "Nama: $name
Type: $type
Size: $size
Tmp: $tmp
";
print "lihat isi folder";
}
}
print "
PHP : UBAH STRING
$kata = "abcdefgh";
print "Variabel \$kata = '$kata' di rubah menjadi string baru sebagai berikut :
";
print substr("$kata",2) ."
";
print substr("$kata",3,2) ."
";
print substr("$kata",0,6) ."
";
print substr("$kata",-3,2) ."
";
?>
print "Variabel \$kata = '$kata' di rubah menjadi string baru sebagai berikut :
";
print substr("$kata",2) ."
";
print substr("$kata",3,2) ."
";
print substr("$kata",0,6) ."
";
print substr("$kata",-3,2) ."
";
?>
PHP : UBAH KOLOM
$koneksi=mysql_connect("localhost","","") or die ("Koneksi gagal");
$tabel="mahasiswa";
$kolom_lama="Alamat";
$kolom_baru="Alamat_lengkap varchar(45) not null";
$sql="ALTER TABLE $tabel CHANGE $kolom_lama $kolom_baru";
$qry=mysql_db_query("contoh",$sql,$koneksi);
if ($qry) {
print "Nama kolom berhasil di rubah";
} else {
print "Nama kolom gagal di rubah";
}
mysql_close();
?>
$tabel="mahasiswa";
$kolom_lama="Alamat";
$kolom_baru="Alamat_lengkap varchar(45) not null";
$sql="ALTER TABLE $tabel CHANGE $kolom_lama $kolom_baru";
$qry=mysql_db_query("contoh",$sql,$koneksi);
if ($qry) {
print "Nama kolom berhasil di rubah";
} else {
print "Nama kolom gagal di rubah";
}
mysql_close();
?>
PHP : TIPE VAR
$angka=78;
$huruf="Belajar PHP dengan cepat";
$bilangan=22.78;
print "Variabel \$angka ";
if (is_int($angka)) {
print "Benar, bertipe integer
";
}
else {
print "Salah, bukan bertipe integer
";
}
print "
";
print "Apakah variabel \$huruf bertipe string?
";
if (is_string($huruf)){
print "Benar, bertipe string
";
}
else{
print "Salah, bukan bertipe string";
}
print "
";
print "Apakah variabel \$bilangan bertipe boolean?
";
if (is_bool($bilangan)){
print "Benar, variabel ini bertipe boolean
";
}
else {
print "Salah, bukan bertipe boolean tapi tipe ". gettype($bilangan);
}
?>
fungsi mendeteksi type variabel:
is_string(namavariabel) = apakakah variabel string
is_int(namavariabel) = ? var integer
is_numerical(namavariabel) = ? var numerik/amgka
is_double(namavariabel) = ? var double
is_array(namavariabel) = ? var array
is_bool(namavariabel) = ? var boolean
fungsi gettype(nama_variavel)= untuk mengetahui tipe var yg dgunakan
$huruf="Belajar PHP dengan cepat";
$bilangan=22.78;
print "Variabel \$angka ";
if (is_int($angka)) {
print "Benar, bertipe integer
";
}
else {
print "Salah, bukan bertipe integer
";
}
print "
";
print "Apakah variabel \$huruf bertipe string?
";
if (is_string($huruf)){
print "Benar, bertipe string
";
}
else{
print "Salah, bukan bertipe string";
}
print "
";
print "Apakah variabel \$bilangan bertipe boolean?
";
if (is_bool($bilangan)){
print "Benar, variabel ini bertipe boolean
";
}
else {
print "Salah, bukan bertipe boolean tapi tipe ". gettype($bilangan);
}
?>
fungsi mendeteksi type variabel:
is_string(namavariabel) = apakakah variabel string
is_int(namavariabel) = ? var integer
is_numerical(namavariabel) = ? var numerik/amgka
is_double(namavariabel) = ? var double
is_array(namavariabel) = ? var array
is_bool(namavariabel) = ? var boolean
fungsi gettype(nama_variavel)= untuk mengetahui tipe var yg dgunakan
PHP : TIPE ARRAY
$campur = array ("25", "Selamat", "34.5");
print "$campur[0]
"; //tipe data integer
print "$campur[1]
"; //tipe data string
print "$campur[2]"; //tipe data double
?>
print "$campur[0]
"; //tipe data integer
print "$campur[1]
"; //tipe data string
print "$campur[2]"; //tipe data double
?>
PHP : TESTKU1
$status=0;
if (isset($Cookieku) && ($Cookieku == "cooki"))
{
$status=1;
if (!isset($cek)) {
setcookie("Cookieku", "cooki");
header("Location: $PHP_SELF?cek=1");
exit;
}
}
print " Browser Anda : ";
printf ('%s
',
$status ? "000000" : "FF0000",
$status ? "mendukung Cookie" : "tidak mendukung Cookie!");
?>
if (isset($Cookieku) && ($Cookieku == "cooki"))
{
$status=1;
if (!isset($cek)) {
setcookie("Cookieku", "cooki");
header("Location: $PHP_SELF?cek=1");
exit;
}
}
print " Browser Anda : ";
printf ('%s
',
$status ? "000000" : "FF0000",
$status ? "mendukung Cookie" : "tidak mendukung Cookie!");
?>
PHP : TESTKU
ob_start();
if (isset($_GET["logout"])) {
setcookie("cookieku", "");
header("location: cookie.php?isloggedout");
}
if ($_GET["passed"] == 1) {
$status = 1;
} else {
if (isset($_COOKIE["cookieku"]) && $_COOKIE["cookieku"] == 'cooki') {
$status = 1;
} else {
$status = 0;
}
}
// if cookieku belum diset
if (!isset($_GET["isloggedout"])) {
if (!isset($_COOKIE["cookieku"])) {
setcookie("cookieku", "cooki");
header("location: cookie.php?passed=1");
}
}
echo $status == 1 ? "Browser Anda mendukung cookie
Nilai cookie : ". $_COOKIE["cookieku"] ."
hapus cookie" : "Browser anda tidak support cookie";
?>
if (isset($_GET["logout"])) {
setcookie("cookieku", "");
header("location: cookie.php?isloggedout");
}
if ($_GET["passed"] == 1) {
$status = 1;
} else {
if (isset($_COOKIE["cookieku"]) && $_COOKIE["cookieku"] == 'cooki') {
$status = 1;
} else {
$status = 0;
}
}
// if cookieku belum diset
if (!isset($_GET["isloggedout"])) {
if (!isset($_COOKIE["cookieku"])) {
setcookie("cookieku", "cooki");
header("location: cookie.php?passed=1");
}
}
echo $status == 1 ? "Browser Anda mendukung cookie
Nilai cookie : ". $_COOKIE["cookieku"] ."
hapus cookie" : "Browser anda tidak support cookie";
?>
PHP : TEST
// Define variable to prevent hacking
define('IN_CB',true);
// Including all required classes
require('class/index.php');
require('class/FColor.php');
require('class/BarCode.php');
require('class/FDrawing.php');
// including the barcode technology
include('class/code39.barcode.php');
// Creating some Color (arguments are R, G, B)
$color_black = new FColor(0,0,0);
$color_white = new FColor(255,255,255);
/* Here is the list of the arguments:
1 - Thickness
2 - Color of bars
3 - Color of spaces
4 - Resolution
5 - Text
6 - Text Font (0-5) */
$code_generated = new code39(30,$color_black,$color_white,1,'HELLO',2);
/* Here is the list of the arguments
1 - Width
2 - Height
3 - Filename (empty : display on screen)
4 - Background color */
$drawing = new FDrawing(1024,1024,'',$color_white);
$drawing->init(); // You must call this method to initialize the image
$drawing->add_barcode($code_generated);
$drawing->draw_all();
$im = $drawing->get_im();
// Next line create the little picture, the barcode is being copied inside
$im2 = imagecreate($code_generated->lastX,$code_generated->lastY);
imagecopyresized($im2, $im, 0, 0, 0, 0, $code_generated->lastX, $code_generated->lastY, $code_generated->lastX, $code_generated->lastY);
$drawing->set_im($im2);
// Header that says it is an image (remove it if you save the barcode to a file)
header('Content-Type: image/png');
// Draw (or save) the image into PNG format.
$drawing->finish(IMG_FORMAT_PNG);
?>
define('IN_CB',true);
// Including all required classes
require('class/index.php');
require('class/FColor.php');
require('class/BarCode.php');
require('class/FDrawing.php');
// including the barcode technology
include('class/code39.barcode.php');
// Creating some Color (arguments are R, G, B)
$color_black = new FColor(0,0,0);
$color_white = new FColor(255,255,255);
/* Here is the list of the arguments:
1 - Thickness
2 - Color of bars
3 - Color of spaces
4 - Resolution
5 - Text
6 - Text Font (0-5) */
$code_generated = new code39(30,$color_black,$color_white,1,'HELLO',2);
/* Here is the list of the arguments
1 - Width
2 - Height
3 - Filename (empty : display on screen)
4 - Background color */
$drawing = new FDrawing(1024,1024,'',$color_white);
$drawing->init(); // You must call this method to initialize the image
$drawing->add_barcode($code_generated);
$drawing->draw_all();
$im = $drawing->get_im();
// Next line create the little picture, the barcode is being copied inside
$im2 = imagecreate($code_generated->lastX,$code_generated->lastY);
imagecopyresized($im2, $im, 0, 0, 0, 0, $code_generated->lastX, $code_generated->lastY, $code_generated->lastX, $code_generated->lastY);
$drawing->set_im($im2);
// Header that says it is an image (remove it if you save the barcode to a file)
header('Content-Type: image/png');
// Draw (or save) the image into PNG format.
$drawing->finish(IMG_FORMAT_PNG);
?>
PHP : TEST PANJANG
$cekpanjang=strlen($tele);
if ((($cekpanjang >=7) && ($cekpanjang <=14)) && (is_numeric($cekpanjang)))
{
print "input OK";
}
else
{
print "Data tidak valid";
}
?>
if ((($cekpanjang >=7) && ($cekpanjang <=14)) && (is_numeric($cekpanjang)))
{
print "input OK";
}
else
{
print "Data tidak valid";
}
?>
PHP : TEST DB1
$conn=mysql_connect("localhost","eko","eko") or die("Koneksi gagal");
if ($conn) {
print "Koneksi ok";
} else {
print "Koneksi gagal";
}
?>
if ($conn) {
print "Koneksi ok";
} else {
print "Koneksi gagal";
}
?>
PHP : TEST COOKIE 1
//include_once("buatcookie.php"):
if (isset($test)) {
print "Cookies yang masih aktif adalah $test";
} else {
print "Nilai Cookies sudah kosong";
}
?>
if (isset($test)) {
print "Cookies yang masih aktif adalah $test";
} else {
print "Nilai Cookies sudah kosong";
}
?>
PHP : TEST COOKIE
/*
$tes_cookie="tes browser";
if ($er != $tes_cookie)
{
setcookie("cookieku",$tes_cookie);
header("Location:$PHP_SELF?$tes_cookie);
}
if ($cookieku==$tes_cookie)
{
print "browser Anda mendukung";
}
else
{
print "browser anda tidak mendukung";
}
*/
$status=0;
if (isset($cookieku) && ($cookieku == "cooki")) { $status = 1;
if (!isset($cek)) {
setcookie("cookieku", "cooki");
header("location: $PHP_SELF?cek=1");
exit;
}
}
printf ('%s
',
$status ? "000000" : "FF0000",
$status ? "Browser Anda mendukung cookie" : "Browser anda tidak support cookie");
?>
$tes_cookie="tes browser";
if ($er != $tes_cookie)
{
setcookie("cookieku",$tes_cookie);
header("Location:$PHP_SELF?$tes_cookie);
}
if ($cookieku==$tes_cookie)
{
print "browser Anda mendukung";
}
else
{
print "browser anda tidak mendukung";
}
*/
$status=0;
if (isset($cookieku) && ($cookieku == "cooki")) { $status = 1;
if (!isset($cek)) {
setcookie("cookieku", "cooki");
header("location: $PHP_SELF?cek=1");
exit;
}
}
printf ('%s
',
$status ? "000000" : "FF0000",
$status ? "Browser Anda mendukung cookie" : "Browser anda tidak support cookie");
?>
PHP : TEST BARCODE
// Define variable to prevent hacking
define('IN_CB',true);
// Including all required classes
require('class/index.php');
require('class/FColor.php');
require('class/BarCode.php');
require('class/FDrawing.php');
// including the barcode technology
include('class/code39.barcode.php');
// Creating some Color (arguments are R, G, B)
$color_black = new FColor(0,0,0);
$color_white = new FColor(255,255,255);
/* Here is the list of the arguments:
1 - Thickness
2 - Color of bars
3 - Color of spaces
4 - Resolution
5 - Text
6 - Text Font (0-5) */
$code_generated = new code39(30,$color_black,$color_white,1,'HELLO',2);
/* Here is the list of the arguments
1 - Width
2 - Height
3 - Filename (empty : display on screen)
4 - Background color */
$drawing = new FDrawing(1024,1024,'',$color_white);
$drawing->init(); // You must call this method to initialize the image
$drawing->add_barcode($code_generated);
$drawing->draw_all();
$im = $drawing->get_im();
// Next line create the little picture, the barcode is being copied inside
$im2 = imagecreate($code_generated->lastX,$code_generated->lastY);
imagecopyresized($im2, $im, 0, 0, 0, 0, $code_generated->lastX, $code_generated->lastY, $code_generated->lastX, $code_generated->lastY);
$drawing->set_im($im2);
// Header that says it is an image (remove it if you save the barcode to a file)
header('Content-Type: image/png');
// Draw (or save) the image into PNG format.
$drawing->finish(IMG_FORMAT_PNG);
?>
define('IN_CB',true);
// Including all required classes
require('class/index.php');
require('class/FColor.php');
require('class/BarCode.php');
require('class/FDrawing.php');
// including the barcode technology
include('class/code39.barcode.php');
// Creating some Color (arguments are R, G, B)
$color_black = new FColor(0,0,0);
$color_white = new FColor(255,255,255);
/* Here is the list of the arguments:
1 - Thickness
2 - Color of bars
3 - Color of spaces
4 - Resolution
5 - Text
6 - Text Font (0-5) */
$code_generated = new code39(30,$color_black,$color_white,1,'HELLO',2);
/* Here is the list of the arguments
1 - Width
2 - Height
3 - Filename (empty : display on screen)
4 - Background color */
$drawing = new FDrawing(1024,1024,'',$color_white);
$drawing->init(); // You must call this method to initialize the image
$drawing->add_barcode($code_generated);
$drawing->draw_all();
$im = $drawing->get_im();
// Next line create the little picture, the barcode is being copied inside
$im2 = imagecreate($code_generated->lastX,$code_generated->lastY);
imagecopyresized($im2, $im, 0, 0, 0, 0, $code_generated->lastX, $code_generated->lastY, $code_generated->lastX, $code_generated->lastY);
$drawing->set_im($im2);
// Header that says it is an image (remove it if you save the barcode to a file)
header('Content-Type: image/png');
// Draw (or save) the image into PNG format.
$drawing->finish(IMG_FORMAT_PNG);
?>
PHP : TERNARI
$a=1;
$b=2;
$c=$a < $b;
print "Nilai \$a adalah $a
";
print "Nilai \$b adalah $b
";
print "\$c = \$a < \$b
";
print "Hasil operasi diatas adalah
";
if ($c)
{
print "True atau 1
";
}
else
{
print "False atau 0
";
}
print "Jika menggunakan operator ternary yaitu :
";
print "\$c =((\$a < \$b)) ? 'True atau 1' : 'False atau 0' "."
";
print "Hasilnya adalah : ";
print $c =(($a < $b)) ? 'True atau 1' : 'False atau 0';
?>
$b=2;
$c=$a < $b;
print "Nilai \$a adalah $a
";
print "Nilai \$b adalah $b
";
print "\$c = \$a < \$b
";
print "Hasil operasi diatas adalah
";
if ($c)
{
print "True atau 1
";
}
else
{
print "False atau 0
";
}
print "Jika menggunakan operator ternary yaitu :
";
print "\$c =((\$a < \$b)) ? 'True atau 1' : 'False atau 0' "."
";
print "Hasilnya adalah : ";
print $c =(($a < $b)) ? 'True atau 1' : 'False atau 0';
?>
PHP : TERAKHIR MODIF
$terakhir = date("d-F-Y. H:i:s a", getlastmod());
print "Terakhir kali di modifikasi adalah pada tanggal $terakhir ";
?>
print "Terakhir kali di modifikasi adalah pada tanggal $terakhir ";
?>
PHP : TEXT GAMBAR
header("Content-Type:image/jpeg");
$background=@imagecreate(100,100);
$kuning=imagecolorallocate($background,250,250,300);
$hitam=imagecolorallocate($background,0,0,0);
imagestring($background,5,10,40,"Tips PHP",$hitam);
imageJPEG($background);
imagedestroy($background);
?>
$background=@imagecreate(100,100);
$kuning=imagecolorallocate($background,250,250,300);
$hitam=imagecolorallocate($background,0,0,0);
imagestring($background,5,10,40,"Tips PHP",$hitam);
imageJPEG($background);
imagedestroy($background);
?>
PHP : TANGGAL FORMAT1
mysql_connect("localhost","","") or die ("Koneksi gagal...");
mysql_select_db("contoh");
$sql=mysql_query("select date_format(tanggal,'%d-%m-%Y') as tanggal from waktu");
if ($baris=mysql_fetch_array($sql))
{
print "Tanggal sekarang adalah : $baris[tanggal]";
}
else
{
print "Data tidak ditemukan...";
}
?>
mysql_select_db("contoh");
$sql=mysql_query("select date_format(tanggal,'%d-%m-%Y') as tanggal from waktu");
if ($baris=mysql_fetch_array($sql))
{
print "Tanggal sekarang adalah : $baris[tanggal]";
}
else
{
print "Data tidak ditemukan...";
}
?>
PHP : TANGGAL FORMAT
$tanggal=date("d-m-Y H:i:s",time());
print "Tanggal dan waktu sekarang adalah $tanggal";
?>
print "Tanggal dan waktu sekarang adalah $tanggal";
?>
PHP : TAMPIL DATA1
$p= 12345;
$s= -12345;
$d= 12/5;
$f= 162.67 - 12;
printf ("%d
" ,$p);
printf ("%d
",$s);
printf ("%f
",$d);
printf ("%d
",$f);
?>
$s= -12345;
$d= 12/5;
$f= 162.67 - 12;
printf ("%d
" ,$p);
printf ("%d
",$s);
printf ("%f
",$d);
printf ("%d
",$f);
?>
PHP : TAMPIL DATA
include_once("konek_db.php");
//$connect=mysql_connect("localhost","priyo","priyo") or die ("Koneksi gagal");
$sql="SELECT NIK,Nama,Alamat,Phone,date_format(Tglmasuk,'%d-%m-%Y') as tanggal,Bagian,Jabatan,Keterangan from karyawan";
$qry=mysql_db_query("karyawan",$sql,$connect);
print "
print "NIK";
print " Nama";
print " Alamat";
print " Phone";
print " Tanggal Masuk";
print " Bagian";
print " Jabatan";
print " Keterangan";
while ($row=mysql_fetch_row($qry))
{
print " $row[0]";
print " $row[1]";
print " $row[2]";
print " $row[3]";
print " $row[4]";
print " $row[5]";
print " $row[6]";
print " $row[7]";
print " edit";
print " hapus";
}
print "";
?>
//$connect=mysql_connect("localhost","priyo","priyo") or die ("Koneksi gagal");
$sql="SELECT NIK,Nama,Alamat,Phone,date_format(Tglmasuk,'%d-%m-%Y') as tanggal,Bagian,Jabatan,Keterangan from karyawan";
$qry=mysql_db_query("karyawan",$sql,$connect);
print "
print "
print "
print "
print "
print "
print "
print "
while ($row=mysql_fetch_row($qry))
{
print "
print "
print "
print "
print "
print "
print "
print "
print "
print "
}
print "";
?>
PHP : TAMPIL KOLOM
$conn=mysql_connect("localhost","","") or die ("Koneksi gagal");
$database="contoh";
$tabel="mahasiswa";
mysql_select_db($database,$conn) or die ("Database $database tidak dapat dibuka");
$sql=mysql_query("select * from $tabel", $conn);
print "Kolom yang ada pada tabel $tabel adalah :
";
print mysql_field_name($sql, 0) ."
";
print mysql_field_name($sql, 1) ."
";
print mysql_field_name($sql, 2) ."
";
print mysql_field_name($sql, 3) ."
";
print mysql_field_name($sql, 4) ."
";
?>
$database="contoh";
$tabel="mahasiswa";
mysql_select_db($database,$conn) or die ("Database $database tidak dapat dibuka");
$sql=mysql_query("select * from $tabel", $conn);
print "Kolom yang ada pada tabel $tabel adalah :
";
print mysql_field_name($sql, 0) ."
";
print mysql_field_name($sql, 1) ."
";
print mysql_field_name($sql, 2) ."
";
print mysql_field_name($sql, 3) ."
";
print mysql_field_name($sql, 4) ."
";
?>
PHP : TAMBAH KOLOM1
$koneksi=mysql_connect("localhost","","") or die ("Koneksi gagal");
$tabel = "mahasiswa";
$kolom1 = "id int not null FIRST";
$kolom2 = "email varchar(20) not null";
$sql="ALTER TABLE $tabel ADD $kolom1";
$qry=mysql_db_query("contoh",$sql,$koneksi);
$sql1="ALTER TABLE $tabel ADD $kolom2 AFTER telepon";
$qry1=mysql_db_query("contoh",$sql1,$koneksi);
if (($qry) && ($qry1)) {
print "Kolom-kolom baru telah berhasil di tambahkan";
} else {
print "Kolom baru gagal di tambahkan";
}
mysql_close();
?>
$tabel = "mahasiswa";
$kolom1 = "id int not null FIRST";
$kolom2 = "email varchar(20) not null";
$sql="ALTER TABLE $tabel ADD $kolom1";
$qry=mysql_db_query("contoh",$sql,$koneksi);
$sql1="ALTER TABLE $tabel ADD $kolom2 AFTER telepon";
$qry1=mysql_db_query("contoh",$sql1,$koneksi);
if (($qry) && ($qry1)) {
print "Kolom-kolom baru telah berhasil di tambahkan";
} else {
print "Kolom baru gagal di tambahkan";
}
mysql_close();
?>
PHP : TAMBAH KOLOM
$koneksi=mysql_connect("localhost","","") or die ("Koneksi gagal");
$tabel = "mahasiswa";
$kolom_baru = "kelulusan varchar(15) not null";
$sql="ALTER TABLE $tabel ADD $kolom_baru";
$qry=mysql_db_query("contoh",$sql,$koneksi);
if ($qry) {
print "Kolom baru telah berhasil di tambahkan";
} else {
print "Kolom baru gagal di tambahkan";
}
mysql_close();
?>
$tabel = "mahasiswa";
$kolom_baru = "kelulusan varchar(15) not null";
$sql="ALTER TABLE $tabel ADD $kolom_baru";
$qry=mysql_db_query("contoh",$sql,$koneksi);
if ($qry) {
print "Kolom baru telah berhasil di tambahkan";
} else {
print "Kolom baru gagal di tambahkan";
}
mysql_close();
?>
PHP : TABEL WARNA
$conn=mysql_connect("localhost","","") or die ("Koneksi gagal");
$tabel="mahasiswa";
$sql="select * from $tabel";
$qry=mysql_db_query("contoh",$sql,$conn);
$jumlah=mysql_num_fields($qry);
print " Jumlah kolom pada tabel $tabel adalah $jumlah
";
print "Atributnya adalah sebagai berikut :
";
//Disusun dalam bentuk tabel
print "";
";
mysql_close();
?>
$tabel="mahasiswa";
$sql="select * from $tabel";
$qry=mysql_db_query("contoh",$sql,$conn);
$jumlah=mysql_num_fields($qry);
print " Jumlah kolom pada tabel $tabel adalah $jumlah
";
print "Atributnya adalah sebagai berikut :
";
//Disusun dalam bentuk tabel
print "
"; print mysql_field_name($qry, $i); print " | ";
"; print mysql_field_type($qry, $i) ." "; print "(" .mysql_field_len($qry, $i). ")"; print " | ";
mysql_close();
?>
PHP : TAMPIL ATRIBUT
$conn=mysql_connect("localhost","","") or die ("Koneksi gagal");
$tabel="mahasiswa";
$sql="select * from $tabel";
$qry=mysql_db_query("contoh",$sql,$conn);
$jumlah=mysql_num_fields($qry);
print " Jumlah kolom pada tabel $tabel adalah $jumlah
";
print "Atributnya adalah sebagai berikut :
";
//Disusun dalam bentuk tabel
print "";
";
mysql_close();
?>
$tabel="mahasiswa";
$sql="select * from $tabel";
$qry=mysql_db_query("contoh",$sql,$conn);
$jumlah=mysql_num_fields($qry);
print " Jumlah kolom pada tabel $tabel adalah $jumlah
";
print "Atributnya adalah sebagai berikut :
";
//Disusun dalam bentuk tabel
print "
"; print mysql_field_name($qry, $i); print " | ";
"; print mysql_field_type($qry, $i) ." "; print "(" .mysql_field_len($qry, $i). ")"; print " | ";
mysql_close();
?>
PHP : TAMBAH ARRAY
$asli = array ("satu","dua","tiga");
array_push ($asli,"empat","lima");
while (list($nilai,$hasil) = each($asli))
{
print "$nilai : $hasil
";
}
?>
array_push ($asli,"empat","lima");
while (list($nilai,$hasil) = each($asli))
{
print "$nilai : $hasil
";
}
?>
PHP : SWITCH
$a=15;
print "Nilai \$a adalah $a
";
switch ($a)
{
case '$a = 20':
{
print "Nilai variabel a sama dengan 20";
}
break;
case '$a < 20':
{
print "Nilai variabel a lebih kecil dari 20";
}
break;
default:
{
print "Nilai variabel a lebih besar dari 20";
}
break;
}
?>
print "Nilai \$a adalah $a
";
switch ($a)
{
case '$a = 20':
{
print "Nilai variabel a sama dengan 20";
}
break;
case '$a < 20':
{
print "Nilai variabel a lebih kecil dari 20";
}
break;
default:
{
print "Nilai variabel a lebih besar dari 20";
}
break;
}
?>
php : string
$a = "Selamat";
$b = $a . " Datang";
print "$b
";
$a = "Selamat";
$a .= " Datang";
print "$a
";
?>
. adalah merangkaikan kata argumen kiri dan kanan
.= adalah argumen kanan dgn yg dikiri
$b = $a . " Datang";
print "$b
";
$a = "Selamat";
$a .= " Datang";
print "$a
";
?>
. adalah merangkaikan kata argumen kiri dan kanan
.= adalah argumen kanan dgn yg dikiri
php : spasi
$kata = " Program PHP ";
print "Kata '$kata' dengan spasi di depan dan di belakang
";
$hilang1 = ltrim($kata);
print "Di hilangkan spasi di depan menjadi :
";
print "'$hilang1'
";
$hilang2 = rtrim($kata);
print "Di hilangkan spasi di belakang menjadi :
";
print "'$hilang2'
";
$hilang3 = trim($kata);
print "Di hilangkan spasi di depan dan di belakang menjadi :
";
print "'$hilang3'
";
?>
print "Kata '$kata' dengan spasi di depan dan di belakang
";
$hilang1 = ltrim($kata);
print "Di hilangkan spasi di depan menjadi :
";
print "'$hilang1'
";
$hilang2 = rtrim($kata);
print "Di hilangkan spasi di belakang menjadi :
";
print "'$hilang2'
";
$hilang3 = trim($kata);
print "Di hilangkan spasi di depan dan di belakang menjadi :
";
print "'$hilang3'
";
?>
PHP : SPACE
// tanda /home;/usr dan /www adalah direktori untuk Linux
$systempartitions = array("/", "/home","/usr", "/www");
foreach ($systempartitions as $partition)
{
//1 MB = 1048576 bytes
//1 MB != 1000000 bytes
$totalSpace = disk_total_space($partition) / 1048576;
$usedSpace = $totalSpace - disk_free_space($partition) / 1048576;
echo "Partisi: $partition (Total Space: $totalSpace MB. Yang digunakan: $usedSpace MB.)
";
}
print "
";
print "Total space yang ada pada drive C adalah : ".disk_total_space("C:")." byte
";
print "Total space yang ada pada drive D adalah : ".disk_total_space("D:")." byte
";
print "Space yang masih ada pada drive C adalah : ".disk_free_space("C:")." byte
";
print "Space yang masih ada pada drive D adalah : ".disk_free_space("D:")." byte
";
?>
$systempartitions = array("/", "/home","/usr", "/www");
foreach ($systempartitions as $partition)
{
//1 MB = 1048576 bytes
//1 MB != 1000000 bytes
$totalSpace = disk_total_space($partition) / 1048576;
$usedSpace = $totalSpace - disk_free_space($partition) / 1048576;
echo "Partisi: $partition (Total Space: $totalSpace MB. Yang digunakan: $usedSpace MB.)
";
}
print "
";
print "Total space yang ada pada drive C adalah : ".disk_total_space("C:")." byte
";
print "Total space yang ada pada drive D adalah : ".disk_total_space("D:")." byte
";
print "Space yang masih ada pada drive C adalah : ".disk_free_space("C:")." byte
";
print "Space yang masih ada pada drive D adalah : ".disk_free_space("D:")." byte
";
?>
PHP : SIFAT FILE1
function outputFileTestInfo( $f ){
if ( ! file_exists( $f ) ){
print "$f tidak ada
";
return;
} else {
print "$f di rubah pada ".date( "D d M Y g:i A", filectime( $f ) )."
";
print "$f di modifikasi ".date( "D d M Y g:i A", filemtime( $f ) )."
";
print "$f di akses pada ".date( "D d M Y g:i A", fileatime( $f ) )."
";
}
}
$file = "file.txt";
outputFileTestInfo( $file );
?>
if ( ! file_exists( $f ) ){
print "$f tidak ada
";
return;
} else {
print "$f di rubah pada ".date( "D d M Y g:i A", filectime( $f ) )."
";
print "$f di modifikasi ".date( "D d M Y g:i A", filemtime( $f ) )."
";
print "$f di akses pada ".date( "D d M Y g:i A", fileatime( $f ) )."
";
}
}
$file = "file.txt";
outputFileTestInfo( $file );
?>
PHP : SIFAT FILE
function InfoFile( $file1 ){
if (! file_exists( $file1 ) ){
print "$file1 tidak ada
";
return;
} else {
print "$file1 adalah ".(is_file( $file1 )?"":"not ")."sebuah file
";
print "$file1 adalah file ".(is_executable( $file1 )?"":"not ")."executable / di eksekusi
";
print "$file1 adalah file ".(is_readable( $file1 )?"":"not ")."readable / di baca
";
print "$file1 adalah file ".(is_writable( $file1 )?"":"not ")."writable / di tulis
";
print "$file1 adalah file ".(is_dir( $file1 )?"":"not ")." sebuah direktori
";
}
}
$file = "file.txt";
InfoFile( $file );
?>
if (! file_exists( $file1 ) ){
print "$file1 tidak ada
";
return;
} else {
print "$file1 adalah ".(is_file( $file1 )?"":"not ")."sebuah file
";
print "$file1 adalah file ".(is_executable( $file1 )?"":"not ")."executable / di eksekusi
";
print "$file1 adalah file ".(is_readable( $file1 )?"":"not ")."readable / di baca
";
print "$file1 adalah file ".(is_writable( $file1 )?"":"not ")."writable / di tulis
";
print "$file1 adalah file ".(is_dir( $file1 )?"":"not ")." sebuah direktori
";
}
}
$file = "file.txt";
InfoFile( $file );
?>
PHP : SEKOLAH
include_once("barmaker.php");
$b = new Bar();
$b->AddValue("Semester 1", array("4.0", "4000"));
$b->AddValue("Semester 2", array("3.5", "3000"));
$b->AddValue("Semester 3", array("3.7", "2750"));
$b->AddValue("Semester 4", array("3.9", "3500"));
$b->SetTitle("My school days");
$b-SetTitleColor(255, 0, 0); //red
$b->SetAxesColor(100, 100, 100);
$b->SetBGColor(255, 255, 255);
$b->Spit("jpg");
?>
$b = new Bar();
$b->AddValue("Semester 1", array("4.0", "4000"));
$b->AddValue("Semester 2", array("3.5", "3000"));
$b->AddValue("Semester 3", array("3.7", "2750"));
$b->AddValue("Semester 4", array("3.9", "3500"));
$b->SetTitle("My school days");
$b-SetTitleColor(255, 0, 0); //red
$b->SetAxesColor(100, 100, 100);
$b->SetBGColor(255, 255, 255);
$b->Spit("jpg");
?>
PHP : PROSES EDIT
include_once("konek_db.php");
$sql="UPDATE karyawan SET Nama='$nama',
Alamat='$alam',
Phone='$phon',
Tglmasuk='$tgl',
Bagian='$bagi',
Jabatan='$jaba',
Keterangan='$kete'
where NIK='$nik'";
$qry=mysql_db_query("karyawan",$sql,$connect);
if ($qry)
{
print "Data telah berhasil di update
";
print "lihat data";
}
else
{
print "Data gagal di update";
}
?>
$sql="UPDATE karyawan SET Nama='$nama',
Alamat='$alam',
Phone='$phon',
Tglmasuk='$tgl',
Bagian='$bagi',
Jabatan='$jaba',
Keterangan='$kete'
where NIK='$nik'";
$qry=mysql_db_query("karyawan",$sql,$connect);
if ($qry)
{
print "Data telah berhasil di update
";
print "lihat data";
}
else
{
print "Data gagal di update";
}
?>
PHP : CARI PROSES
include_once("konek_db.php");
$sql="SELECT NIK,Nama,Alamat,Phone,date_format(Tglmasuk,'%d-%m-%Y') as tanggal,Bagian,Jabatan,Keterangan from karyawan where $pilihan LIKE '%$cari%'";
$qry=mysql_db_query("karyawan",$sql,$connect);
print "";
$sql="SELECT NIK,Nama,Alamat,Phone,date_format(Tglmasuk,'%d-%m-%Y') as tanggal,Bagian,Jabatan,Keterangan from karyawan where $pilihan LIKE '%$cari%'";
$qry=mysql_db_query("karyawan",$sql,$connect);
print "
NIK";
print " | Nama";
print " | Alamat";
print " | Phone";
print " | Tanggal Masuk";
print " | Bagian";
print " | Jabatan";
print " | Keterangan";
while ($row=mysql_fetch_row($qry)) { print " | ||||||||||||||||
$row[0]";
print " | $row[1]";
print " | $row[2]";
print " | $row[3]";
print " | $row[4]";
print " | $row[5]";
print " | $row[6]";
print " | $row[7]";
} ?> PHP : PROSES CARI
include_once("konek_db.php");
$sql="SELECT NIK,Nama,Alamat,Phone,date_format(Tglmasuk,'%d-%m-%Y') as tanggal,Bagian,Jabatan,Keterangan from karyawan where $pilihan LIKE '%$cari%'"; $qry=mysql_db_query("karyawan",$sql,$connect); print "
|