PHP Tutorials - Mahmuddin
PHP Tutorials - Mahmuddin
BASIC I/O
<?php echo "whatsup"; ?> Hasil: whatsup <?php echo 123; ?> Hasil: 123 <?php echo Hello World! <br> New line; ?> Hasil: Hello World! New line <?php $sometext = harry poter; echo $sometext; ?> Hasil: harry poter <?php $sometext = harry poter; $somenumber = 32;
C. VARIABEL
echo My name is $name and my age is $age.; ?> Hasil: My name is Alex and my age is 19.
D. IF ELSE STATEMENT
<?php $first = 10; $second = 20; $third = 30; if ($first<$second) {echo this is true;} else {echo this is false;} ?> Hasil: this is true Fungsi yang dapat dipakai : <, >, <=,>=,==,!=
E. IF ELSEIF IF
<?php $var = steve; if ($var == steve) echo hey stevo; elseif ($var == brian)
echo hey there bri; else echo who are you; ?> Hasil: hey stevo <?php $var = brian; if ($var == steve) echo hey stevo; elseif ($var == brian) echo hey there bri; else echo who are you; ?> Hasil: hey there bri
F. SWITCH STATEMENT
<?php $country = USA; switch ($country) { case USA: echo your in USA; break;
case japan: echo your in japan; break; default: echo you messed up; break; } ?> Hasil: your in USA
G. WHILE LOOP
<?php $num = 1; While ($num <= 5) { echo $num.</br>; $num++; } ?> Hasil: 1 2 3 4 5
H. DO LOOP
<?php $num = 1; do { $num++; echo $num.</br>; } while($num<10); ?> Hasil: 2 3 : : 10
I. FOR LOOP
<?php for ($num=1; $num<=5; $num++) { echo hello world. </br>; } ?> Hasil:
hello world hello world hello world hello world hello world
J. ARRAY
<?php $people = array (tom,jake,tony); $people[0]=tom; $people[1]=jake; $people[2]=tony; echo $people[2]; ?> Hasil: tony
K. ASSOCIATIVE ARRAY
<?php $actions = array (fingers=>hand,toes=>foot,teeth=>mouth); echo $actions[fingers]; ?> Hasil: hand
O. FUNCTIONS
<?php function testfun() { echo easy hoss! } echo take it ; testfun(); echo </br>.i said take it; testfun(); ?> Hasil: take it easy hoss! i said take it easy hoss!
P. PARAMETER IN FUNCTIONS
<?php function somefun($sub) { echo $sub.hoss </br>!; }
somefun(take it easy ); somefun(i said take it easy); ?> Hasil: take it easy hoss! i said take it easy hoss!
Q. RETURN VALUES
<?php function add($a,$b) { $total = $a + $b; return $total; } echo add(8,9) ?> Hasil: 17
R. BEGINNING FORMS
welcome.php ==========
<html> <body>
<form action=testerpage.php method=post> Name: <input type=text name=name/> <input type=submit value=Kirim/> </form> </body> </html>
testerpage.php ===========
<html> <body>
</body> </html>
Hasil:
S. DATE FUNCTION
<?php echo date(m/d/Y); ?> Hasil: 17/20/2008 {berdasarkan tanggal komputer saat file ini dibuka} {gunakan / atau -}
T. INCLUDE FUNCTION Buat 2 buah file bernama welcome.php dan testerpage.php welcome.php ==========
<?php
testerpage.php ==========
<html> <body> <?php include (welcome.php);?> <h1> Welcome People </h1> <p> Hey now all star</p> </body> </html>
Hasil:
U. GET VARIABEL
<?php echo $_GET(myname); ?>
Hasil:
<html> <form action=get.php method=GET> <input type=textname=myname><br> <input type=submit value=Click here> </form> </html> Hasil:
<html> <form action=get.php method=GET> <input type=textname=myname><br> <input type=submit value=Click here> </form> </html> <?php $name = $_GET[myname]; if($name) echo Hello, $name.;
?> Hasil:
V. COMPARISON OPERATORS
<?php if (1 == 1) echo True; else echo False; ?> Hasil: True <?php $password = abc if ($password = def) { echo ACCESS GRANTED; }
W. Creating database
X.