Panduan Ringkas Pengaturcaraan PHP - Asas
PHP adalah sebuah tool yang berkeupayaan tinggi untuk membuat laman web dinamik dan interaktif. PHP digunakan secara meluas, percuma, dan merupakan alternatif yang sangat efisien kepada pesaing seperti Microsoft ASP.
PHP adalah sebuah bahasa skrip pelayan web. Sebelum anda mempelajari asas-asas PHP, anda juga perlu mengetahui selok belok mengenai HTML/XHTML dan juga JavaScript.
Apa itu PHP?
Apa itu fail PHP?
Apa itu MySQL?
PHP + MySQL
Mengapa PHP?
Bagaimana Mulakan?
Untuk mendapatkan akses ke web server dengan sokongan PHP, anda boleh:
Dicadangkan anda menggunakan perisian XAMPP bagi tujuan pembelajaran dan pembangunan sistem.
Contoh:
Hello World
Hello World! What a nice day!
Fungsi strlen()
Untuk menilai panjang rentetan. Contoh:
12
Fungsi strpos()
Menentu kedudukan dalam rentetan. Contoh:
Output larian:
6
Operator Umpukan
Comparison Operators
Operator Logical
Penyataan if ... else ...
Penyataan if ... elseif ... else ...
Penyataan switch
Sintaksis 1
if (condition) code to be executed if condition is true;
Contoh:
Contoh:
Sintaksis 2
Sintaksis
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;
Contoh:
Sintaksis
switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
default:
code to be executed if n is different from both label1 and label2;
}
Contoh:
Contoh : diindeks secara automatik
Contoh: diindeks secara manual
Contoh : panggilan atau operasi array
Output : Saab and Volvo are Swedish cars.
Array bersekutu
Kunci ID dikaitkan dengan nilai array.
Contoh : mengumpukkan umur kepada array dan dikaitkan dengan nama individu.
Contoh : seperti di atas tetapi dengan cara lain.
Array multi dimensi
Jika array $families dipapar dengan penyataan print_r($families); output berikut dihasilkan.
echo "Is " . $families['Griffin'][2] . " a part of the Griffin family?";
Output
Is Megan a part of the Griffin family?
Gelung dalam PHP
Contoh : Gelung while
while (condition)
{
code to be executed;
}
output
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
Contoh: Gelung do ... while
do
{
code to be executed;
}
while (condition);
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
Gelung For
for (init; condition; increment)
{
code to be executed;
}
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
Gelung foreach
foreach ($array as $value)
{
code to be executed;
}
Output
one
two
three
{
code to be executed;
}
Contoh 1:
<?php
function writeName()
{
echo "Kai Jim Refsnes";
}
echo "My name is ";
writeName();
?>
Output
My name is Kai Jim Refsnes
Contoh 2:
My name is Kai Jim Refsnes.
My sister's name is Hege Refsnes.
My brother's name is Stale Refsnes.
Contoh 3:
My name is Kai Jim Refsnes.
My sister's name is Hege Refsnes!
My brother's name is Ståle Refsnes?
Contoh 4
1 + 16 = 17
Rujukan: w3school.com, PHP manual.
PHP adalah sebuah bahasa skrip pelayan web. Sebelum anda mempelajari asas-asas PHP, anda juga perlu mengetahui selok belok mengenai HTML/XHTML dan juga JavaScript.
Apa itu PHP?
- PHP singkatan dari PHP: Hypertext Preprocessor
- PHP adalah bahasa skrip server-side, seperti ASP
- PHP script dijalankan di pelayan
- PHP menyokong pelbagai database (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, dll)
- PHP adalah software open source
- PHP percuma untuk dimuat turun dan digunakan
Apa itu fail PHP?
- Fail PHP boleh mengandungi teks, tag HTML dan skrip
- Fail PHP dikembalikan ke browser sebagai laman HTML biasa
- Fail PHP mempunyai sambungan fail "php", ". Php3",. Atau ". Phtml"
Apa itu MySQL?
- MySQL adalah aplikasi pelayan database
- MySQL sangat ideal untuk aplikasi kecil dan besar
- MySQL menyokong standard SQL
- MySQL dikompilasi pada sebilangan platform
- MySQL adalah percuma dimuat turun dan digunakan
PHP + MySQL
- PHP digabungkan dengan MySQL adalah cross-platform (anda boleh membangunkan di Windows dan dilaksanakan pada platform Unix)
Mengapa PHP?
- PHP boleh dilaksana pada platform yang berbeza (Windows, Linux, Unix, dll)
- PHP serasi dengan hampir semua pelayan yang digunakan hari ini (Apache, IIS, dll)
- PHP adalah PERCUMA untuk dimuat turun dari sumber rasmi PHP: www.php.net
- PHP mudah dipelajari dan dilarikan dengan efisien di pelayan
Bagaimana Mulakan?
Untuk mendapatkan akses ke web server dengan sokongan PHP, anda boleh:
- Memasang Apache (atau IIS) di pelayan anda sendiri, pasang PHP, dan MySQL
- Atau cari web hosting rencana dengan PHP dan MySQL
- Atau memasang perisian pakej pelayan XAMPP yang terdiri daripada pelayan web Apache, pelayan MySQL, sokongan PHP, Perl dan lain-lain. Perisian ini memudahkan anda memasang pelayan2 dan juga komponen berkaitan, ia adalah salah satu solution terbaik untuk pembangunan web dan pembelajaran PHP/MySQL. Muat turun di sini. Panduan pemasangan disediakan di laman web itu.
Dicadangkan anda menggunakan perisian XAMPP bagi tujuan pembelajaran dan pembangunan sistem.
Tag PHP
Tag PHP boleh berada di mana2 dalam dokumen, ia bermula <?php dengan ?>. Contoh :<?php ?>
Contoh:
<html>
<body>
<?php
// komen
echo "Hello World";
/*
blok komen
baris ke dua
baris ketiga
*/
?>
</body>
</html>
Pemboleh ubah (variable)
Semua pemboleh ubah dalam PHP bermula dengan simbol $. Dalam PHP jenis data ditentukan secara automatik, tidak perlu ditakrifkan. Contoh:$nama_pembolehubah = nilai;
<?php
$txt="Hello World!";
$x=16;
?>
Manipulasi rentetan (string)
Memaparkan nilai.<?php
$txt="Hello World";
echo $txt;
?>
Output larian :Hello World
Operator pengabungan
Untuk menyambung rentetan2, gunakan operator titik (.). Contoh:<?php
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?>
Output larian : Hello World! What a nice day!
Fungsi strlen()
Untuk menilai panjang rentetan. Contoh:
<?php
echo strlen("Hello world!");
?>
Output larian:12
Fungsi strpos()
Menentu kedudukan dalam rentetan. Contoh:
<?php
echo strpos("Hello world!","world");
?>
Output larian:
6
Operator PHP
Operator ArimetikOperator | Penerangan | Contoh | Output |
---|---|---|---|
+ | Tambah | x=2 x+2 | 4 |
- | Tolak | x=2 5-x | 3 |
* | Darab | x=4 x*5 | 20 |
/ | Bahagi | 15/5 5/2 | 3 2.5 |
% | Modulus (baki pembahagian) | 5%2 10%8 10%2 | 1 2 0 |
++ | Tokokkan / Increment | x=5 x++ | x=6 |
-- | Susutan / Decrement | x=5 x-- | x=4 |
Operator Umpukan
Operator | Contoh | Adalah sama seperti |
---|---|---|
= | x=y | x=y |
+= | x+=y | x=x+y |
-= | x-=y | x=x-y |
*= | x*=y | x=x*y |
/= | x/=y | x=x/y |
.= | x.=y | x=x.y |
%= | x%=y | x=x%y |
Comparison Operators
Operator | Penerangan | Contoh |
---|---|---|
== | sama dengan | 5==8 hasil false |
!= | tidak sama dengan | 5!=8 hasil true |
<> | tidak sama dengan | 5<>8 hasil true |
> | lebih besar daripada | 5>8 hasil false |
< | kurang daripada | 5<8 hasil true |
>= | lebih besar atau sama dengan | 5>=8 hasil false |
<= | kurang atau sama dengan | 5<=8 hasil true |
Operator Logical
Operator | Penerangan | Contoh |
---|---|---|
&& | and | x=6 y=3 (x < 10 && y > 1) hasil true |
|| | or | x=6 y=3 (x==5 || y==5) hasil false |
! | not | x=6 y=3 !(x==y) hasil true |
Struktur Kawalan bersyarat
Penyataan if ...Penyataan if ... else ...
Penyataan if ... elseif ... else ...
Penyataan switch
Sintaksis 1
if (condition) code to be executed if condition is true;
Contoh:
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri") echo "Have a nice weekend!";
?>
</body>
</html>
<body>
<?php
$d=date("D");
if ($d=="Fri") echo "Have a nice weekend!";
?>
</body>
</html>
Contoh:
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
{
echo "Hello!<br />";
echo "Have a nice weekend!";
echo "See you on Monday!";
}
?>
</body>
</html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
{
echo "Hello!<br />";
echo "Have a nice weekend!";
echo "See you on Monday!";
}
?>
</body>
</html>
Sintaksis 2
if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
Contoh:code to be executed if condition is true;
else
code to be executed if condition is false;
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>
</body>
</html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>
</body>
</html>
Sintaksis
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;
Contoh:
<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>
<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>
Sintaksis
switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
default:
code to be executed if n is different from both label1 and label2;
}
Contoh:
<html>
<body>
<?php
switch ($x)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>
</body>
</html>
<body>
<?php
switch ($x)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>
</body>
</html>
Array
Array bernomborContoh : diindeks secara automatik
$cars=array("Saab","Volvo","BMW","Toyota");
Contoh: diindeks secara manual
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
Contoh : panggilan atau operasi array
<?php
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";
?>
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";
?>
Output : Saab and Volvo are Swedish cars.
Array bersekutu
Kunci ID dikaitkan dengan nilai array.
Contoh : mengumpukkan umur kepada array dan dikaitkan dengan nama individu.
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
Contoh : seperti di atas tetapi dengan cara lain.
<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
echo "Peter is " . $ages['Peter'] . " years old.";
?>
Output: Peter is 32 years old. $ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
echo "Peter is " . $ages['Peter'] . " years old.";
?>
Array multi dimensi
$families = array
(
"Griffin"=>array
(
"Peter",
"Lois",
"Megan"
),
"Quagmire"=>array
(
"Glenn"
),
"Brown"=>array
(
"Cleveland",
"Loretta",
"Junior"
)
);
(
"Griffin"=>array
(
"Peter",
"Lois",
"Megan"
),
"Quagmire"=>array
(
"Glenn"
),
"Brown"=>array
(
"Cleveland",
"Loretta",
"Junior"
)
);
Jika array $families dipapar dengan penyataan print_r($families); output berikut dihasilkan.
Array
(
[Griffin] => Array
(
[0] => Peter
[1] => Lois
[2] => Megan
)
[Quagmire] => Array
(
[0] => Glenn
)
[Brown] => Array
(
[0] => Cleveland
[1] => Loretta
[2] => Junior
)
)
Larian penyataan(
[Griffin] => Array
(
[0] => Peter
[1] => Lois
[2] => Megan
)
[Quagmire] => Array
(
[0] => Glenn
)
[Brown] => Array
(
[0] => Cleveland
[1] => Loretta
[2] => Junior
)
)
echo "Is " . $families['Griffin'][2] . " a part of the Griffin family?";
Output
Is Megan a part of the Griffin family?
Gelung dalam PHP
Contoh : Gelung while
while (condition)
{
code to be executed;
}
<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
output
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
Contoh: Gelung do ... while
do
{
code to be executed;
}
while (condition);
<?php
$i=1;
do
{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<=5);
?>
$i=1;
do
{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<=5);
?>
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
Gelung For
for (init; condition; increment)
{
code to be executed;
}
<?php
for ($i=1; $i<=5; $i++)
{
echo "The number is " . $i . "<br />";
}
?>
for ($i=1; $i<=5; $i++)
{
echo "The number is " . $i . "<br />";
}
?>
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
Gelung foreach
foreach ($array as $value)
{
code to be executed;
}
<?php
$x=array("one","two","three");
foreach ($x as $value)
{
echo $value . "<br />";
}
?>
$x=array("one","two","three");
foreach ($x as $value)
{
echo $value . "<br />";
}
?>
Output
one
two
three
Fungsi dalam PHP
function functionName(){
code to be executed;
}
Contoh 1:
<?php
function writeName()
{
echo "Kai Jim Refsnes";
}
echo "My name is ";
writeName();
?>
Output
My name is Kai Jim Refsnes
Contoh 2:
<?php
function writeName($fname)
{
echo $fname . " Refsnes.<br />";
}
echo "My name is ";
writeName("Kai Jim");
echo "My sister's name is ";
writeName("Hege");
echo "My brother's name is ";
writeName("Stale");
?>
function writeName($fname)
{
echo $fname . " Refsnes.<br />";
}
echo "My name is ";
writeName("Kai Jim");
echo "My sister's name is ";
writeName("Hege");
echo "My brother's name is ";
writeName("Stale");
?>
My name is Kai Jim Refsnes.
My sister's name is Hege Refsnes.
My brother's name is Stale Refsnes.
Contoh 3:
<?php
function writeName($fname,$punctuation)
{
echo $fname . " Refsnes" . $punctuation . "<br />";
}
echo "My name is ";
writeName("Kai Jim",".");
echo "My sister's name is ";
writeName("Hege","!");
echo "My brother's name is ";
writeName("Ståle","?");
?>
function writeName($fname,$punctuation)
{
echo $fname . " Refsnes" . $punctuation . "<br />";
}
echo "My name is ";
writeName("Kai Jim",".");
echo "My sister's name is ";
writeName("Hege","!");
echo "My brother's name is ";
writeName("Ståle","?");
?>
My name is Kai Jim Refsnes.
My sister's name is Hege Refsnes!
My brother's name is Ståle Refsnes?
Contoh 4
<?php
function add($x,$y)
{
$total=$x+$y;
return $total;
}
echo "1 + 16 = " . add(1,16);
?>
function add($x,$y)
{
$total=$x+$y;
return $total;
}
echo "1 + 16 = " . add(1,16);
?>
1 + 16 = 17
Rujukan: w3school.com, PHP manual.
info menarik..bleh dijadikan rujukan trutamanya bdk skolah cm saya..minat sgt2 ilmu ICT
BalasPadam