Php Programlama Kodlar
1
2
3
4
5
6
7
8
9
10
|
<?php
echo 'Mutlak değer : ' . abs(-20) . '<br>';
echo '16nın karekökü : ' . sqrt(16) . '<br>';
echo '3^4 (3 üssü 4) : ' . pow(3, 4) . '<br>';
echo '12 nin 5e bölümünden kalan : ' . fmod (12, 5) . '<br>';
echo 'log(6) : ' . log (6) . '<br>';
echo '10 tabanında log(6) : ' . log10 (6) . '<br>';
echo 'e üssü -2 : ' . exp (-2) . '<br>';
echo 'uzunluğu 3 ve 4 olan üçgenin hipotenüsü : ' . hypot (3, 4) . '<br>';
?>
|
1
2
3
4
5
6
7
|
<?php
$pi = pi();
echo $pi . '<br>'; # Sonuç: 3.14159265359
echo ceil($pi) . '<br>'; # Sonuç: 4
echo floor($pi) . '<br>'; # Sonuç: 4
echo round($pi, 3); # Sonuç: 3.142
?>
|
1
2
3
4
|
<?php
$sayi = 12;
echo base_convert ( $sayi, 10, 8 ); # Sonuç: 14
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php
echo bindec (1111000) . '<br>'; # Sonuç: 120
echo decbin (120) . '<br><br>'; # Sonuç: 1111000
echo hexdec ('1A') . '<br>'; # Sonuç: 26
echo dechex (44) . '<br><br>'; # Sonuç: 2c
echo octdec (14) . '<br>'; # Sonuç: 12
echo decoct (12) . '<br><br>'; # Sonuç: 14
echo deg2rad (90) . '<br>'; # Sonuç: 1.57079632679
echo rad2deg (1.57079632679) . '<br>'; # Sonuç: ~ 90
?>
|
1
2
3
4
5
6
|
<?php
$deger = 90;
echo sin($deger) . '<br>'; # Sonuç: 0.893996663601
echo sin(deg2rad($deger)) . '<br>'; # Sonuç: 1
echo sin(pi()/2) . '<br>'; # Sonuç: 1
?>
|
1
2
3
4
5
6
|
<?php
$deger = 1;
$radyan = asin($deger);
echo $radyan . '<br>'; # Sonuç: 1.57079632679
echo rad2deg($radyan) . '<br>'; # Sonuç: 90
?>
|
1
2
3
4
5
6
7
8
9
|
<?php
echo 'rand: ' . getrandmax() . '<br>'; # Sonuç: 32767
echo rand() . '<br>';
echo rand(0, 10) . '<br><br>';
echo 'mt_rand: ' . mt_getrandmax() . '<br>'; # Sonuç: 2147483647
echo mt_rand() . '<br>';
echo mt_rand(20, 30);
?>
|
1
2
3
4
5
6
7
8
|
<?php
echo max(2, 80, 1.5, -20, 70) . '<br>';
echo min(2, 80, 1.5, -20, 70) . '<br>';
# Pi sayısını elde etmenin çeşitli yolları
echo pi() . '<br>';
echo deg2rad(180) . '<br>';
echo M_PI . '<br>';
?>
|
1
2
3
4
5
|
<?php
include 'dosya.txt'; # çalıştırılan dosya ile aynı dizinde dosya.txt arar
include '../dosya.html'; # bir alt dizinde dosya.html arar
include 'dosya/dosya.php'; # dosya dizinin içinde dosya.php arar
?>
|
1
2
3
4
5
|
<?php
require 'dosya.txt';
require '../dosya.html';
require 'dosya/dosya.php';
?>
|
1
2
3
4
5
|
<?php
include 'ekle.php ';
include 'ekle.php ';
include 'ekle.php ';
?>
|
1
2
3
4
5
|
<?php
include_once 'ekle.php ';
include_once 'ekle.php ';
include_once 'ekle.php ';
?>
|
1
2
3
4
5
6
7
8
9
10
11
|
<?php
function dosya_ekle ( $dosya_yolu = NULL ) {
if ( file_exists( $dosya_yolu ) === true ) {
include_once $dosya_yolu;
}
}
# fonksiyonun kullanımı
dosya_ekle('ekle.php');
dosya_ekle('yeni_metin.txt');
?>
|
1
2
3
4
5
|
<?php
if(isset($_SESSION['yetki']))
header("HTTP/1.0 403 Forbidden");
# 403 Forbidden: Adres erişime yasaklanmıştır.
?>
|
1
2
3
4
5
|
<?php
# Aynı URL üzerinde yeni.php sayfasına yönlendirir.
header("Location: yeni.php");
exit; # Yönlendirme sonrası herhangi bir kod çalıştırılmasın
?>
|
1
2
3
4
5
|
<?php
# Google web sayfasına yönlendirme yapar.
header("Location: http://www.google.com/");
exit;
?>
|
1
2
3
4
5
|
<?php
# yeni.php sayfasına "301 Moved Permanently" başlığı ile yönlendirir
header("Location: http://www.google.com/", TRUE, 301);
exit;
?>
|
1
2
3
4
|
<?php
header( "refresh:5;url=yeni.php" );
die('5sn sonra yonlendirileceksiniz. Beklememek için <a href="yeni.php">tiklayin.</a>');
?>
|
1
2
3
4
|
<?php
header('content-type:text/html; charset=utf-8');
echo 'PHP öğrenmek çok kolay.';
?>
|
1
2
3
4
|
<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); # Geçmişte bir tarih
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<?php
if($_POST) {
$yetki = $_POST['yetki'];
if( $yetki == 'indir') {
$dosya = 'indirilecek_dosya.rar';
# dosya gerçekte var mı?
if(file_exists($dosya)) {
# dosya çıkarma
header('Pragma: public');
header("Expires: 0");
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename= ' . $dosya);
header('Content-Length: ' . filesize($dosya));
header('Content-Transfer-Encoding: binary');
ob_clean();
flush();
readfile($file);
} else {
echo 'Dosya bulunamadı.';
}
} else {
echo 'Bu dosyaya erişim izniniz yoktur.';
}
}
?>
<form action="#" method="post">
<input type="text" name="yetki">
<input type="submit" value="indir">
</form>
|
1
2
3
4
5
|
<?php
$dizge = "oun@bu7is";
$sifreli = md5($dizge);
echo $sifreli;
?>
|
1
2
3
4
5
6
7
8
9
10
11
|
<?php
# oun@bu7is dizgesinin şifreli hali
$parola = 'eafdd5d6b58587d1b0b2e338168da940';
$dizge = "oun@bu7is";
if( $parola == md5($dizge) ) {
echo 'Eşit.';
} else {
echo 'Eşit değil.';
}
?>
|
1
2
3
4
5
|
<?php
$dizge = 'oun@bu7is';
$sifreli = crc32($dizge);
echo $sifreli . '<br>';
?>
|
1
2
3
4
5
6
7
8
|
<?php
$parola = 'oun@bu7is';
if (sha1($parola) === '9ec78dc260989bb2bec5ce9eadca6efe76814b9d') {
echo "Parola doğru.";
exit;
}
echo "Parola hatalı";
?>
|
1
2
3
4
5
6
7
|
<?php
$dizge = 'oun@bu7is';
$sifreli = base64_encode($dizge);
echo $sifreli . '<br>';
echo base64_decode($sifreli);
?>
|
1
2
3
4
5
6
7
8
|
<?php
$parola = '1234';
$sifre1 = md5($parola);
$sifre2 = md5($sifre1);
echo $sifre1 . '<br>'; # 81dc9bdb52d04dc20036dbd8313ed055
echo $sifre2 . '<br>'; # ec6a6536ca304edf844d1d248a4f08dc
?>
|
1
2
3
4
5
6
7
8
9
10
|
<?php
$parola = '1234';
$sifre1 = crc32($parola); # -1679564637
$sifre2 = md5($sifre1); # 37a5fe57871252aa56ca225e7d30f920
$sifre3 = sha1($sifre2); # 21f771ab6c2b1bfbfa411c0a32e10b5e3034ce59
if( sha1(md5(crc32('1234'))) === $sifre3 ) {
echo 'Parola doğru.';
}
?>
|
1
2
3
4
5
6
7
|
<?php
# Bugün: 2012-02-25 02:19:13
echo 'Bugün: ' . date("Y-m-d H:i:s");
# Bugün yılın 55. günü
echo 'Bugün yılın ' . date("z") . '. günü';
?>
|
1
2
3
4
5
6
7
8
9
|
<?php
$gun = array('Pazar', 'Pazartesi', 'Salı', 'Çarşamba', 'Perşembe', 'Cuma', 'Cumartesi');
$ay = array('', 'Ocak', 'Şubat', 'Mart', 'Nisan', 'Mayıs', 'Haziran',
'Temmuz', 'Ağustos', 'Eylül', 'Ekim', 'Kasım', 'Aralık');
echo 'Bugün: ' . $gunler[date("w")] . '<br>';
echo 'Bu ay: ' . $aylar[date("n")] . '<br>';
echo 'Şimdi: ' . date("d ") . $aylar[date("n")] . date(" Y ") . $gunler[date("w")];
?>
|
1
2
3
4
5
6
7
|
<?php
$unix = time();
$zaman = date("Y-m-d H:i:s", $unix);
echo 'Unix : ' . $unix . '<br>'; # Unix : 1330130082
echo 'Şimdi: ' . $zaman; # Şimdi: 2012-02-25 02:34:42
?>
|
1
2
3
4
5
|
<?php
$unix = time();
echo 'Bugün : ' . date("Y-m-d", $unix) . '<br>'; # Bugün : 2012-02-25
echo 'Dün: ' . date("Y-m-d", $unix - (60*60*24)); # Dün: 2012-02-24
?>
|
1
2
3
4
5
6
7
8
9
10
|
<?php
#Bugün : 2012-02-10
echo 'Bugün : ' . date("Y-m-d", mktime(0, 0, 0, 2, 10, 2012)) . '<br>';
# 2 ay önce : 2011-12-10
echo '2 ay önce : ' . date("Y-m-d", mktime(0, 0, 0, 0, 10, 2012)) . '<br>';
# 2 ay 15 gün önce : 2011-11-25
echo '2 ay 15 gün önce : ' . date("Y-m-d", mktime(0, 0, 0, 0, -5, 2012));
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php
class ilkSinif
{
# özellik
public $birOzellik;
# davranis
public function birDavranis() {
echo 'Davranis çalıştı';
}
}
?>
|
1
2
3
|
<?php
$nesne = new ilkSinif();
?>
|
1
2
3
4
5
|
<?php
$nesne = new ilkSinif();
$nesne->isim = 'Ali';
echo $nesne->merhaba();
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?php
class ilkSinif
{
# kurucu fonksiyon
function __construct() {
echo 'Kurucu fonksiyon çalıştı.<br>';
}
# yıkıcı fonksiyon
function __destruct() {
echo 'Yıkıcı fonksiyon çalıştı.<br>';
}
}
# nesne örnekleme
$nesne = new ilkSinif();
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?php
class ilkSinif
{
public $ad = 'Ece';
public $soyad = 'Can';
# parametre alabilen kurucu fonksiyon
function __construct($isim = NULL) {
if ($isim != NULL)
$this->ad = $isim;
}
function ekranaYaz() {
echo $this->ad . ' ' . $this->soyad;
}
}
# nesne örnekleme
$nesne = new ilkSinif('Pelin');
$nesne->ekranaYaz();
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php
class gizlilik
{
public $ad = 'Ece';
private $soyad = 'Can';
protected $sehir = 'Bursa';
}
$nesne = new gizlilik();
echo $nesne->ad; # Erişilebilir : public
echo $nesne->soyad; # Erişilemez : private
echo $nesne->sehir; # Erişilemez : protected
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
<?php
class kisiler
{
public $ad;
public $soyad;
public function ekranaYaz() {
echo $this->ad . ' ' . $this->soyad . '<br>';
}
}
class ogrenci extends kisiler
{
public $not;
public function notYaz(){
echo 'Notu: ' . $this->not . '<br>';
}
}
class ogretmen extends kisiler
{
public function ekranaYaz() {
echo 'Ogretmen ' . $this->ad;
}
}
$ogrenci = new ogrenci();
$ogrenci->ad = 'Sercan';
$ogrenci->soyad = 'Çakır';
$ogrenci->not = 80;
$ogrenci->ekranaYaz();
$ogrenci->notYaz();
$ogretmen = new ogretmen();
$ogretmen->ad = 'Ece';
$ogretmen->soyad = 'Can';
$ogretmen->ekranaYaz();
?>
|
1
2
3
4
5
6
7
|
<?php
class ulke { }
class sehir extends ulke { }
final class kasaba extends sehir { }
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?php
class anne
{
public function konus() {
echo 'Anne sınıfındaki konus() fonksiyonu çalıştı.<br>';
}
}
class cocuk extends anne
{
public function __construct() {
self::konus();
parent::konus();
$this->konus();
}
public function konus(){
echo 'Çocuk sınıfındaki konus() fonksiyonu çalıştı.<br>';
}
}
$cocuk = new cocuk();
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?php
class Ornek
{
public static $deger = 1;
public function dogru() {
return self::$deger;
}
public function hatali() {
return $this->$deger;
}
}
echo Ornek::$deger;
echo Ornek::dogru();
echo Ornek::hatali();
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php
class Ornek
{
const numara = 1234;
public function getNumara() {
return self::numara;
}
}
echo Ornek::numara . '<br>';
echo Ornek::getNumara();
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php
class islec {
const sabit = 'sabit';
public static $ozellik = 'static özellik';
public static function metod() {
echo 'static metod';
}
}
echo islec::sabit . '<br>';
echo islec::$ozellik . '<br>';
echo islec::metod();
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<?php
abstract class soyut {
# türeyen sınıfta zorunlu olması gereken metotlar
abstract public function topla($a, $b);
abstract public function yaz();
# türeyen sınıflarda kullanılacak ortak metot
public function ortak(){
echo 'Ortak metot kullanımı';
}
}
class somut extends soyut {
public function topla($c, $d) {
return $c . ' + ' . $d . ' = ' . ($c + $d);
}
public function yaz() {
echo 'Soyut sınıfların kullanımı.';
}
}
$somut = new somut();
echo $somut->topla(10, 20) . '<br>';
echo $somut->yaz() . '<br>';
echo $somut->ortak() . '<br>';
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?php
interface sinifYapi
{
public function al($isim);
public function yaz();
}
class Sinifim implements sinifYapi
{
private $deger;
public function al($veri){
$this->deger = $veri;
}
public function yaz() {
echo $this->deger;
}
}
$nesne = new Sinifim();
$nesne->al('Hilal');
$nesne->yaz();
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?php
class Sinif
{
public $sayi = 5;
public $metin = 'Merhaba';
public $ondalik = M_PI; # pi sayısı sabitidir ve 3.14159265… sayısını verir.
}
function tur_Dayat(Sinif $param) {
echo $param->sayi . '<br>';
echo $param->metin . '<br>';
echo $param->ondalik . '<br>';
}
$nesne = new Sinif();
tur_Dayat($nesne);
# tur_Dayat(5); çalışmayacaktır, çünkü Sinif türünde değil
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
<?php
header('content-type:text/html;charset=utf-8');
class DB
{
/* Özellikler */
protected $conn = false;
private $server, $user, $password, $db;
/**
* Kurucu metot - Sınıf örneklendiğinde çalışır
*
* @param string $server Sunucu adresi
* @param string $user Kullanıcı adı
* @param string $password Parola
* @param string $db Veritabanı adı
*
* @return void
*/
public function __construct($server, $user, $password, $db)
{
$this->server = $server;
$this->user = $user;
$this->password = $password;
$this->db = $db;
$this->open();
}
/**
* Open() - Veritabanı bağlantısını oluşturur
*
* @return void
*/
public function open()
{
$this->conn = mysql_connect($this->server, $this->user, $this->password);
if( ! $this->conn ) {
echo 'Veritabanı ile bağlantı kurulamadı.';
return false;
}
mysql_select_db($this->db, $this->conn);
}
/**
* Uyku modu - serialize(object)
*
* @return void
*/
public function __sleep()
{
mysql_close($this->conn);
$this->conn = false;
return array('server', 'user', 'password', 'db');
}
/**
* Uyandırma modu - unserialize(object);
*
* @return void
*/
public function __wakeup()
{
$this->open();
}
/**
* Yıkıcı metot - Sınıf sonlandırıldığında çalışır.
*
* @return void
*/
public function __destruct()
{
if( $this->conn ) {
mysql_close($this->conn);
$this->conn = null;
}
}
}
/* Sınıf örnekleme ve nesne kullanımı*/
$db = new DB('localhost', 'root', 'oun@buis', 'test');
# bu arada veritabanı ile çalışıyor.
echo "<pre>";
print_r($db);
echo "</pre>";
$db_serial = serialize ($db);
# veritabanına şuan ihtiyaç yok, bağlantıyı kapat.
unserialize ($db_serial);
# veritabanına tekrar ihtiyaç duyulursa bağlantıyı yeniden kur.
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
<?php
class Overloading
{
/**
* Aşırı yükleme yapılacak özellik
*
* @var array
*/
private $data = array();
/**
* Overloading set eden metot – değer yükleme
*
* @param string $name overloading değişken adı
* @param string $value overloading değişken değeri
* @return void
*/
private function __set($name, $value)
{
$this->data[$name] = $value;
}
/**
* Overloading get eden metot – değer döndürme
*
* @param string $name overloading değişken adı
* @return mixed
*/
private function __get($name)
{
return $this->data[$name];
}
/**
* Overloading isset eden metot – değer sorgulama (var mı?)
*
* @param string $name overloading değişken adı
* @return boolean
*/
private function __isset($name)
{
return isset($this->data[$name]);
}
/**
* Overloading unset eden metot – değeri silme
*
* @param string $name overloading değişken adı
* @return boolean
*/
private function __unset($name)
{
unset($this->data[$name]);
}
/**
* Overloading metot – $name parametresinde metot bulunamazsa tetiklenir
*
* @param string $name overloading değişken adı
* @return boolean
*/
private function __call($name, $params)
{
$sonuc = 1;
if($params != null) {
foreach($params as $deger){
$sonuc *= $deger;
}
}
return $sonuc;
}
}
# Nesne örnekle
$alan = new Overloading();
# hayali x ve özelliklere aşırı yükle
$alan->x = 2;
$alan->y = 5;
# hayali özellikleri ekrana yazdır
echo 'X = ' . $alan->x . '<br>';
echo 'Y = ' . $alan->y . '<br>';
# hayali özellikler var ise hayali metoda aşırı yükle
if(isset($alan->x) && isset($alan->y))
echo 'X * Y = ' . $alan->hesapla($alan->x, $alan->y);
# hayali özellikleri sil
unset($alan->x, $alan->y);
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php
class Ebeveyn {
public $ad;
}
$anne = new Ebeveyn();
$cocuk = $anne;
$anne->ad = 'Merve';
$cocuk->ad = 'Aylin';
echo $anne->ad .'<br>';
echo $cocuk->ad;
# anne ve cocuk nesnesinin ad özellikleri Aylin oldu!
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php
class Ebeveyn {
public $ad;
}
$anne = new Ebeveyn();
$cocuk = clone $anne;
$anne->ad = 'Merve';
$cocuk->ad = 'Aylin';
echo $anne->ad .'<br>';
echo $cocuk->ad;
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php
class Ebeveyn {
public $ad;
}
$anne = new Ebeveyn();
$anne->ad = 'Merve';
$cocuk = clone $anne;
echo $anne->ad .'<br>';
echo $cocuk->ad;
# anne ve çocuğun adı Merve oldu
?>
|
1
2
3
4
5
6
7
8
9
|
<?php
class Ebeveyn {
public $ad;
private function __clone() { }
}
$anne = new ebeveyn();
$cocuk = clone $anne;
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?php
class Ogrenci {
public $ad, $soyad, $numara;
public function ekranaYaz() {
echo 'Ad :' . $this->ad . '<br>';
echo 'Soyad :' . $this->soyad . '<br>';
echo 'Numara :' . $this->numara;
}
}
?>
<?php
# nesne örneklendi ve bir takım işler yapıldı
$ogr = new Ogrenci();
$ogr->ad = 'Hilal';
$ogr->soyad = 'Saim';
$ogr->numara = '5';
$ogr->ekranaYaz();
# bu esnada uygulamadan çıkılacak nesne serialize edilip bir dosyay kaydediliyor.
file_put_contents('yedek.txt', serialize($ogr));
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?php
class Ogrenci {
public $ad, $soyad, $numara;
public function ekranaYaz() {
echo 'Ad :' . $this->ad . '<br>';
echo 'Soyad :' . $this->soyad . '<br>';
echo 'Numara :' . $this->numara;
}
}
?>
<?php
# farzedelim ki bir süre sonra geri geldik ve aynı nesneye ihtiyaç duyuldu.
$ogr2 = unserialize(file_get_contents('store'));
$ogr2->numara = '25';
$ogr2->ekranaYaz();
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?php
header('content-type:text/html;charset=utf8');
class ornek {
function isimBul()
{
# Sınıf içinde kullanımı
echo "Sınıf ismi " . get_class($this) . "<br>";
# Bir başka yöntem fakat sadece sınıf içinde kullanılır.
echo "Sınıf ismi " . __CLASS__ . "<br>";
}
}
$o = new ornek();
$o->isimBul();
# Nesne ile kullanımı
echo "Nesnenin ait olduğu sınıf ismi " . get_class($o) . "<br>";
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?php
class ornek {
var $b = 5;
public $c = 9;
protected $d = 10;
private $e = 10;
public function publicDavranis() { }
protected function protectedDavranis() { }
private function privateDavranis() { }
}
$o = new ornek();
echo "<pre>";
print_r( get_class_vars ( get_class($o) ) );
print_r( get_class_methods ('ornek') );
echo "</pre>";
?>
|
1
2
3
4
5
6
7
8
9
|
<?php
class ornek {
public $ozellik = 9;
}
class_alias('ornek', 'example');
$nesne = new example();
echo $nesne->ozellik;
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<?php
header('content-type:text/html;charset=utf8');
class ornek {
var $b = 5;
public $c = 9;
protected $d = 10;
private $e = 10;
public function publicDavranis() { }
protected function protectedDavranis() { }
private function privateDavranis() { }
}
$o = new ornek();
echo "<pre>";
print_r( get_object_vars ( $o ) );
echo "</pre>";
# Özellik mevcut mu?
if( property_exists($o, 'e' ) ) {
echo '$e isimli özellik sınıfta mevcut.<br>';
}
# Metod (davranış) mevcut mu?
if( method_exists($o, 'privateDavranis' ) ) {
echo 'privateDavranis isimli metod sınıfta mevcut.<br>';
}
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<?php
header('content-type:text/html;charset=utf8');
class anne { }
class baba { }
class cocuk extends anne {
function __construct()
{
echo "Ben " , get_parent_class($this) , "nin cocuğuyum.<br>";
}
}
$nesne = new cocuk();
# Ebeveyn/Çocuk ilişkisi
if (is_a($nesne, 'cocuk')) {
echo '$nesne ile cocuk arasında ebeveyn/çocuk ilişkisi var.<br>';
}
# Ebeveyn/Çocuk ilişkisi 2. yöntem
if ($nesne instanceof cocuk) {
echo '$nesne ile cocuk arasında ebeveyn/çocuk ilişkisi var.<br>';
}
# Alt sınıf mı?
if (is_subclass_of ($nesne, 'anne')) {
echo '$nesne ile türetilen sınıf, anne sınıfının bir alt sınıfıdır.<br>';
}
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<?php
class Ogrenci {
public $adsoyad;
public $sinif;
public function ekranaYaz(){
echo 'Adı Soyadı : ' . $this->adsoyad . '<br>';
echo 'Sınıfı : ' . $this->sinif . '<br>';
}
}
class IlkOgretim extends Ogrenci { }
class Lise extends Ogrenci {
public $bolum;
public function ekranaYaz() {
parent::ekranaYaz();
echo 'Bölümü : ' . $this->bolum . '<br>';
}
}
# örnekleme ve sınıfların kullanımı
$ilk = new IlkOgretim();
$ilk->adsoyad = 'Ece Mutlu';
$ilk->sinif = 4;
$ilk->ekranaYaz();
echo '<br>';
$lise = new Lise();
$lise->adsoyad = 'Ayşe Gedik';
$lise->sinif = 11;
$lise->bolum = 'Veritabanı Programcılığı';
$lise->ekranaYaz();
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
<?php
# Yerel zaman dilimi ayarı
date_default_timezone_set('Europe/Istanbul');
trait Tarih {
public function tarihAl() {
return date("Y-m-d ");
}
}
Trait Saat {
public function saatAl () {
return date("H:i:s");
}
}
Trait TarihSaat {
use Tarih, Saat;
}
# Sınıf
class Test {
use TarihSaat;
public function simdi() {
return $this->tarihAl() . ' ' . $this->saatAl();
}
}
# Örnekleme
$o = new Test();
echo $o->simdi(); # Sonuç : 2012-03-24 17:08:28
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
<?php
session_start();
header("content-type:text/html;charset=utf8");
if( $_POST ) {
$uyeadi = trim($_POST["ad"]);
$parola = trim($_POST["parola"]);
$hatirla = trim($_POST["hatirla"]);
if($uyeadi == "demo" && $parola == "12345") {
# doğru ise session değişkenlerini oluştur
$_SESSION["uyead"] = $uyeadi;
$_SESSION["uyemi"] = "evet";
if($hatirla == 1) {
setcookie("uye_cerez", $uyeadi, time()+60*60*24);
}
header("location: index.php");
} else {
echo "Bilgileriniz hatalı, lütfen kontrol edin.<hr>";
}
}
?>
<form action="#" method="post">
<table>
<tr>
<td>Üye Adı:</td>
<td><input type="text" name="ad" value="<?php echo $_COOKIE["uye_cerez"]; ?>"></td>
</tr>
<tr>
<td>Parola:</td>
<td><input type="password" name="parola"></td>
</tr>
<tr>
<td></td>
<td><input type="checkbox" name="hatirla" value="1"> beni hatırla</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Giriş"></td>
</tr>
</table>
</form>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php
session_start();
header("content-type:text/html;charset=utf8");
# uyemi isimli session değişkeni uygulamanın kontrol değişkenidir.
if( $_SESSION["uyemi"] == "evet" ) {
echo "Hoşgeldin, " . $_SESSION["uyead"];
echo " (<a href=\"cikis.php\">çıkış</a>)";
} else {
echo "Bu sayfa sadece üyelere özel.";
echo "Üye isen <a href=\"giris.php\">buraya tıkla.</a>";
}
?>
|
1
2
3
4
5
6
7
8
9
10
|
<?php
$veri = array( 'Ayşe', 'Ece', 'Neşe', 'Merve' );
echo "<pre>";
print_r($veri); # Sonuç : Ayşe, Ece, Neşe, Merve
# 0 ve 3 numaralı indisleri sil
unset($veri[0], $veri[3]);
print_r($veri); # Sonuç : Ece, Neşe
echo "</pre>";
?>
|
1
2
3
4
5
6
|
<?php
$veri = array( 'Ayşe', 'Ece', 'Neşe', 'Merve' );
# dizi ile ilgili işlemleri burada gerçekleştir
$veri = array();
print_r($veri);
?>
|
1
2
3
4
5
6
7
|
<?php
$renkler = array('Mavi', 'Sarı', 'Yeşil', 'Pembe');
foreach ( $renkler as $veri ) {
echo $veri . '<br>';
}
?>
|
1
2
3
4
5
6
7
8
|
<?php
$kitap = array('kitap_adi' => 'HTML5', 'sayfa' => 300, 'basim_yili' => '2012');
foreach ( $kitap as $anahtar => $veri ) {
echo 'İndis : ' . $anahtar . '<br>';
echo 'Değer : ' . $veri . '<br><br>';
}
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?php
# Kullanıcı tanımlı sıralama fonksiyonu
function sirala($a, $b){ return strcasecmp($a, $b); }
$dizi = array( 'edebiyat' => 90, 'tarih' => 80, 'geometri' => 100, 'matematik' => 95 );
echo "<pre>";
# uasort(array &$array, callback function);
uasort($dizi, 'sirala');
print_r($dizi);
# uksort(array &$array, callback function);
uksort($dizi, 'sirala');
print_r($dizi);
# usort(array &$array, callback function);
usort($dizi, 'sirala');
print_r($dizi);
echo "</pre>";
?>
|
1
2
3
4
5
6
7
8
9
10
|
<?php
$liste = array('elma', 'armut', 'portakal', 'kivi', 'ayva', 'mandalina');
echo current($liste) . '<br>'; # elma
echo next($liste) . '<br>'; # armut
echo next($liste) . '<br>'; # portakal
echo prev($liste) . '<br>'; # armut
echo end($liste) . '<br>'; # mandalina
echo pos($liste) . '<br>'; # mandalina
echo reset($liste); # elma
?>
|
1
2
3
4
5
6
7
8
9
10
|
<?php
$liste = array( 1, 3, 3, 6, 2);
echo 'Eleman sayısı: ' . count($liste) . '<br>'; # Sonuç: 5
echo 'Eleman sayısı: ' . sizeof($liste) . '<br>'; # Sonuç: 5
echo 'Dizideki elemanların çarpımı: ' . array_product($liste) . '<br>'; # Sonuç: 108
echo 'Dizideki elemanların toplamı: ' . array_sum($liste) . '<br>'; # Sonuç: 15
echo "<pre>";
print_r(array_count_values($liste));
echo "</pre>";
?>
|
1
2
3
4
|
<?php
$renk = array ('Sarı', 'Mavi', 'Yeşil', 'Kırmızı', 'Siyah');
echo implode($renk); # Sonuç: SarıMaviYeşilKırmızıSiyah
?>
|
1
2
3
4
|
<?php
$renk = array ('Sarı', 'Mavi', 'Yeşil', 'Kırmızı', 'Siyah');
echo implode(' - ', $renk); # Sarı - Mavi - Yeşil - Kırmızı - Siyah
?>
|
1
2
3
4
5
|
<?php
$dizi = array ('Sarı', 'Mavi', 'Beyaz', 'Mavi', 'Kırmızı');
$key = array_search('Mavi', $dizi);
echo $key; # Sonuç: 1
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php
header('content-type: text/html; charset=utf-8');
$sayi = 5;
$bolen = 0;
try {
if (!@$sonuc = $sayi / $bolen)
throw new Exception("Sıfıra bölüm hatası oluştu.");
echo $sayi .' / ' . $bolen . ' = ' . $sonuc;
} catch ( Exception $hata ) {
echo "<pre>";
print_r($hata);
echo "</pre>";
}
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?php
# Tüm hata mesajlarını gizler
error_reporting(0);
# Sadece basit düzeyli hata mesajlarını raporlar
error_reporting(E_ERROR | E_WARNING | E_PARSE);
# E_NOTICE hariç bütün hataları raporlar. Zaten php.ini içindeki ön tanımlı değerdir.
error_reporting(E_ALL ^ E_NOTICE);
# Tüm hataları raporlar
error_reporting(E_ALL);
# error_reporting(E_ALL); ile aynıdır
ini_set('error_reporting', E_ALL);
# error_reporting(E_ALL); ile aynıdır
error_reporting(-1);
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?php
header("content-type:text/html;charset=utf8");
$dosya = $_FILES["bunu_yukle"];
$izin_verilen_tipler = array("image/png", "image/gif", "image/jpeg", "image/jpg");
if(in_array($dosya["type"], $izin_verilen_tipler)) {
if (move_uploaded_file($dosya["tmp_name"], $dosya["name"])) {
echo "Dosya yüklendi.";
} else {
echo "Dosya yüklenemedi.";
}
} else {
echo "Sadece PNG, GIF, JPG veya JPEG uzantılı dosya yüklenebilir.";
}
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?php
# Önce küresel değişkendeki değeri alın
$yukle = $_FILES["yuklenecek_dosya"];
# Kısıtlayıcı yok, dosya çalıştırıldığı dizine yükler.
$sonuc = upload($yukle);
# Kısıtlayıcı yok, dosya upload dizinine yükler
$sonuc = upload($yukle, 'upload');
# Kısıtlayıcı yok, dosya upload dizinine deneme adıyla yükler
$sonuc = upload($yukle, 'upload', 'deneme');
# Sadece PNG dosyaları yüklenir. Dosya upload dizinine deneme adıyla yükler
$sonuc = upload($yukle, 'upload', 'deneme', 'image/png');
# Sadece PNG ve JPG dosyaları yüklenir. Dosya upload dizinine deneme adıyla yükler
$sonuc = upload($yukle, 'upload', 'deneme', array('image/png', 'image/jpg'));
# En fazla 700KB’lık, PNG dosyaları yüklenir. Dosya upload dizinine deneme adıyla yükler
$sonuc = upload($yukle, 'upload', 'deneme', 'image/png', 1024*700);
# işlem sonucunu ekrana yazdıralım.
if($sonuc)
echo "Dosya başarıyla yüklendi.";
else
echo "Bir hata oluştu.";
?>
|
1
2
3
4
5
6
7
8
9
|
<?php
try {
$baglanti = mysql_connect("localhost", "kullanıcı adı", "parola")
or
die("Hata : " . mysql_error() );
} catch (Exception $e) {
die("Beklenmeyen bir hata oluştu : " . $e->getMEssage() );
}
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?php
$DB_NAME = 'phpmyadmin';
if (!$con = mysql_connect('localhost', 'kullanıcı adı', 'parola')) {
die('Bağlantı sağlanamadı.');
exit;
}
mysql_select_db($DB_NAME, $con);
$alan = mysql_query("SHOW COLUMNS FROM pma_history");
if ($alan) {
echo '<table border=1>';
while ($satir = mysql_fetch_row($alan)) {
echo '<tr>';
foreach($satir as $deger) {
echo '<td>' . $deger . '</td>';
}
echo '</tr>';
}
}
mysql_free_result($alan);
?>
|
Yorumlar ( 2 )
tesekkürlerr
İşimize yarayanı alır alır kullanırız artık teşekkürler 🙂