You are on page 1of 4

Cara mengakses file PHP di web browser:

1. Buka XAMPP
2. Jalankan Apache
3. Ketikkan di web browser => localhost/psw2/namaFolder/namaFile.php

Latihan!

1. Hello World!
<?php
echo "Hello, World!";
?>
Perbaiki potongan kode berikut!
<?php
echo "Hello, World!"
ECHO "Hello, World!"
ecHO "Hello, World!"
echo 'Hello, World!'
echo ('Hello, World!')
echo Hello!
?>

2. Using variable and types


<?php
$name = 'Wiro Sableng';
$height = 170;
$weight = 72.6;
$weapon = "the 2-1-2 dangerous axe";
$motto = 'me + PHP = kicks the problem away!';
?>
String: Perbaiki potongan kode berikut!
<?php
echo "Hello $name.\n"
?>
Integer: Perbaiki potongan kode berikut!
<?php
echo "Hello $name. You are $age years old.\n"
?>
Global Variable
<?php
$x = 'Hello'; //global $x
function myFunc(){
global $x; //use global $x
$x .= ' World'; //change global x
}
myFunc();
echo $x;
?>

1131204/PSW/W01S02/TNT 05/02/2020 12.52 1


Variable Parameter List
<?php
function myArgs()
{
$x = func_get_arg(0);
$y = func_get_arg(1);
$z = func_get_arg(2);
echo $x . $y . $z;
}
myArgs('Fee', 'Fi', 'Fo');
?>

3. Operators
<?php
// Sum up the variables x and y and
// put the result in the sum variable.
$x = 19;
$y = 25;
$sum = NULL;
echo "The sum of $x and $y is $sum."
?>
Perbaiki potongan kode berikut!
<?php
$x = 1;
$y = 3;
$sum = $x + $y;
echo $Sum;
?>

4. PHP first look


<html>
<head><title>01_today_time</title></head>
<body>today is 05th February 2019, 10:00 AM</body>
</html>
<html>
<head><title>PHP: first look[2]</title></head>
<body>
now is: <?php echo(date('d F Y H:i a'));?>
</body>
</html>

5. Constant
<?php
define("PI", 3.1415927);
$radius = 7.0;
echo PI * ($radius * $radius);
?>

1131204/PSW/W01S02/TNT 05/02/2020 12.52 2


6. If Conditional
<?php
$name = 'Wiro';

if($name == 'Wiro'){
echo('Your weapon is the 2-1-2 axe.');
} else if($name == 'Wolverine'){
echo ('Your weapon is the cat-paw.');
} else{
echo ('You have no weapon.');
}
?>

7. Switch Case
<?php
$name = 'Wiro';
switch($name){
case 'Wiro':
echo('Your weapon is the 2-1-2 axe.');
break;
case 'Wolverine':
echo ('Your weapon is the cat-paw.');
break;
defalut:
echo ('You have no weapon.');
break;
}
?>

8. Foreach Looop
<?php
$arrName = array(
'Wiro Sableng', 'Jaka Sembung', 'Saras 008',
'Milkyman', 'Ranger Merah', 'Pikachu'
);
?>
<html>
<head><title>Arrays</title></head>
<body>
<h2>My Super hero</h2>
<ul>
<?php
foreach($arrName as $name){
echo('<li>' .$name.'</li>');
}
?>
</ul>
</body>
</html>

1131204/PSW/W01S02/TNT 05/02/2020 12.52 3


9. Arrays
<?php
$arrName = array(
'Wiro Sableng', 'Jaka Sembung', 'Saras 008',
'Milkyman', 'Ranger Merah', 'Pikachu'
);
?>
<html>
<head><title>Arrays</title></head>
<body>
<h2>My Super hero</h2>
<ul>
<?php
echo('<li>' .$arrName[0].'</li>');
echo('<li>' .$arrName[1].'</li>');
echo('<li>' .$arrName[2].'</li>');
?>
</ul>
</body>
</html>

- Selamat berlatih  -

1131204/PSW/W01S02/TNT 05/02/2020 12.52 4

You might also like