Archive for June 2011

A Simple PHP class

Let’s start our thinking of the class with a simple PHP class example. In this example we want to print a variable value.

<?php
class SimpleClass
{
    // property declaration
    public $variable = 'This is state of an PHP object';

    // method declaration
    public function display_variable() {
        echo $this->variable." Under PHP class Method";
    }
}
?>

Save the code and run it. This program print nothing. It is important to remember that a class declaration only creates a template, it does not create the actual object. To print the variable value you need to create a instance of this class.

Creating an instance

You can create an instance using new operator. A class must be created before instantiating object. So the whole code is

<?php
class SimpleClass
{
    // property declaration
    public $variable = "This is state of an PHP object";

    // method declaration
    public function display_variable() {
        echo $this->variable." Under PHP class Method";
    }
}
$instance=new SimpleClass();
$instance->display_variable();

?>

Run this code and it will show This is state of an PHP object Under PHP class Method. In the display_variable method we use this keyword. It says that it is the variable of this class not other class. 

roll dice in php

roll_dice.php :

<center><?php
$rand=rand(1,6);
echo "dice one : $rand".'<br>';
$rand1=rand(1,6);
echo "dice two : $rand1";

?>
<form action="roll_dice.php" method="POST">
<input type="submit" value="roll dice">
</form>


output :

dice one : 3
dice two : 3

dice one : 3
dice two : 5

dice one : 4
dice two : 2

dice one : 4
dice two : 3

dice one : 1
dice two : 5

note :
for basics : rand()

arithmetic operators in php


                                    arithmetic operators in php : + , - , * , / , % , ++ , --


<?php


$a=10;


echo $a+10;


?>


output : 20



<?php


$a=10;


echo $a-10;


?>
output :0 


<?php


$a=10;


echo $a*10;


?>
output : 100

<?php


$a=10;


echo $a/10;


?>
output : 1

<?php


$a=10;


echo $a+10;


?>
output : 20


<?php


$a=10;


echo $a--;


?>
output : 10

<?php


$a=10;


echo $a++;


?>


output : 10




<?php


$a=10;


echo --$a;


?>
output : 9

<?php


$a=10;


echo ++$a;


?>


output : 11





when opendir() using it close with closedir() in php

closedir.php :

<?php

//Open send directory
if(@$dir = opendir("send"))
{
echo '<br>dir opened<br>';

//List files in send directory
while (($file = readdir($dir)) !== false)
{
echo "filename: " . $file . "<br />";
}
closedir($dir);
echo '<br>dir closed<br>';
}
else
echo 'directory not opened';
?>
=========================================
outputs :
 1)
directory not opened

2)
dir opened
filename: .
filename: ..
filename: sendsms.html
filename: sendsms.php
filename: send_vard.php
filename: send_vcard_sms.html
filename: smser.php

dir closed



PHP Decision Making



The if, elseif ...else and switch statements are used to take decision based on the different condition.
You can use conditional statements in your code to make your decisions. PHP supports following three decision making statements:
  • if...else statement - use this statement if you want to execute a set of code when a condition is true and another if the condition is not true
  • else if statement - is used with the if...else statement to execute a set of code if one of several condition are true
  • Switch statement - is used if you want to select one of many blocks of code to be executed, use the Switch statement. The switch statement is used to avoid long blocks of if..Else if...else code.
The If...Else Statement
If you want to execute some code if a condition is true and another code if a condition is false, use the if....else statement.
Syntax
if (condition)
  code to be executed if condition is true;
else
  code to be executed if condition is false;
Example
The following example will output "Have a nice weekend!" if the current day is Friday, otherwise it will output "Have a nice day!":
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
  echo "Have a nice weekend!";
else
  echo "Have a nice day!";
?>
</body>

</html>
If more than one line should be executed if a condition is true/false, the lines should be enclosed within curly braces:
<html>

<body>
<?php
$d=date("D");
if ($d=="Fri")
  {
  echo "Hello!<br />";
  echo "Have a nice weekend!";
  echo "See you on Monday!";
  }
?>
</body>
</html>
The ElseIf Statement
If you want to execute some code if one of several conditions are true use the elseif statement
Syntax
if (condition)
  code to be executed if condition is true;
elseif (condition)
  code to be executed if condition is true;
else
  code to be executed if condition is false;
Example
The following example will output "Have a nice weekend!" if the current day is Friday, and "Have a nice Sunday!" if the current day is Sunday. Otherwise it will output "Have a nice day!":
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
  echo "Have a nice weekend!";
elseif ($d=="Sun")
  echo "Have a nice Sunday!";
else
  echo "Have a nice day!";
?>
</body>
</html>
The Switch Statement
If you want to select one of many blocks of code to be executed, use the Switch statement.
The switch statement is used to avoid long blocks of if..elseif..else code.
Syntax
switch (expression)
{
case label1:
  code to be executed if expression = label1;
  break; 
case label2:
  code to be executed if expression = label2;
  break;
default:
  code to be executed
  if expression is different
  from both label1 and label2;

}
Example
The switch statement works in an unusual way. First it evaluates given expression then seeks a lable to match the resulting value. If a matching value is found then the code associated with the matching label will be executed or if none of the lables match then statement will will execute any specified default code.
<html>
<body>
<?php
$d=date("D");
switch ($d)
{
case "Mon":
  echo "Today is Monday";
  break;
case "Tue":
  echo "Today is Tuesday";
  break;
case "Wed":
  echo "Today is Wednesday";
  break;
case "Thu":
  echo "Today is Thursday";
  break;
case "Fri":
  echo "Today is Friday";
  break;
case "Sat":
  echo "Today is Saturday";
  break;
case "Sun":
  echo "Today is Sunday";
  break;
default:
  echo "Wonder which day is this ?";
}
?>
</body>
</html>

Mengembalikan GRUB bootloader yang hilang pada Ubuntu Linux


Pernahkah Anda mengalami kejadian pada komputer atau laptop kesayangan yang kehilangan bootloader-nya? bila Anda pernah mengalaminya bisa jadi komputer Anda tidak dapat booting sama sekali atau secara otomatis nyelonong masuk ke Operating System lain yang ada di komputer tanpa melalui GRUB untuk memilih Operating System yang akan digunakan, biasanya hal ini terjadi pada pengguna yang menginstall dual OS di komputernya, seperti saya (evilsmirk)
Hal seperti ini pernah saya alami ketika saya masih menggunakan Linux Debian Woodydahulu, akan tetapi setelah beralih ke Ubuntu 8.10 Interpid Ibex dan sekarang ke Ubuntu 9.04 Jaunty Jackalope Alhamdulillah saya belum pernah mengalami kejadian tersebut lagi. Hal ini biasanya terjadi jika instalasi Ubuntu tidak beres, atau ketika Windows bootloader menimpa MBR dan tidak mengenali instalasi Linux. Maka kita harus mengembalikan GRUB bootloader agar dapat booting kedalam Linux. Tulisan ini sekaligus menjawabpertanyaan dari mas Hasbi yang mengalami masalah setelah menginstal Ubuntu di komputernya.
Ada dua cara yang bisa kita lakukan untuk mengembalikan GRUB yang hilang,
CARA PERTAMA
  1. Pertama-tama kita harus mempersiapkan Ubuntu Live CD, jika ngga punya bisa juga menggunakan bootloader dari flash disk
  2. Kemudian ubah settingan pada BIOS, CD atau flash disk pada urutan pertama (first boot option)
  3. Jika PC Anda sudah diset untuk boot ke CD-ROM, Anda bisa menunggu sampai layar pilihan boot Ubuntu muncul pilih saja Try Ubuntu (without any change on your computer)
  4. Setelah masuk Live CD Desktop jalankan Terminal dengan meng-klik menuApplications > Accessories > Terminal
  5. Cek partisi Linux kita dengan menggunakan perintah
    sudo fdisk -l
    Mengembalikan GRUB Linux yang hilang 1
  6. Setelah diketahui partisi Linuxnya (misalkan partisi Linux pada laptop saya adalah sda7), kemudian lakukan mounting seperti berikut ini (lihat gambar ke-2)
    sudo mount -t ext4 /dev/sda7 /mnt/
    sudo mount -t proc proc /mnt/proc/
    sudo mount -t sysfs sys /mnt/sys/
    sudo mount -o bind /dev/ /mnt/dev/
    sudo chroot /mnt /bin/bash
    Mengembalikan GRUB Linux yang hilang 2
  7. Sekarang kita akan mengembalikan GRUB ke MBR, perintahnya
    grub-install /dev/sda
  8. sekarang kita akan mengembalikan kedalam partisi Linuxnya (dalam hal ini lokasi partisi Linux laptop saya berada di /dev/sda7), perintahnya
    grub-install /dev/sda7
  9. Jika tidak ada pesan error maka setelah Anda melakukan Reboot, bootloader akan kembali seperti semula. (banana_cool)
CARA KEDUA
  1. Pertama-tama kita harus mempersiapkan Ubuntu Live CD, jika ngga punya bisa juga menggunakan bootloader dari flash disk
  2. Kemudian ubah settingan pada BIOS, CD atau flash disk pada urutan pertama (first boot option)
  3. Jika PC Anda sudah diset untuk boot ke CD-ROM, Anda bisa menunggu sampai layar pilihan boot Ubuntu muncul pilih saja Try Ubuntu (without any change on your computer)
  4. Setelah masuk Live CD Desktop jalankan Terminal dengan meng-klik menuApplications > Accessories > Terminal
  5. Cek partisi Linux kita dengan menggunakan perintah
    sudo fdisk -l
  6. Masuk dalam GRUB, perintahnya
    sudo grub
    Mengembalikan GRUB Linux yang hilang 3
  7. Selanjutnya lakukan langkah berikut :
    grub> find /boot/grub/stage1
    misalkan didapatkan hasil (hd0,6), ini adalah informasi yang akan kita gunakan untuk mensetup pada tahap selanjutnya.
  8. Kemudian melakukan setup instalasi GRUB :
    grub> root (hd0,6)
    grub> setup (hd0)
  9. Jika proses installasi sudah selesai, keluar dari shell GRUB.
    grub> quit
  10. Jika tidak ada pesan error maka setelah Anda melakukan Reboot, bootloader akan kembali seperti semula. (banana_cool)
Semoga setelah membaca petunjuk ini kedepannya apabila Anda mendapati permasalahan yang sama bisa segera diatasi, semoga bermanfaat… :)
sumber: http://masiqbal.net