0% found this document useful (0 votes)
62 views4 pages

w01s02p Exercise

The document provides instructions and examples for accessing PHP files in a web browser using XAMPP, as well as examples of PHP code demonstrating variables, data types, operators, conditional statements, loops, and arrays. It includes code snippets to print "Hello World", define and use variables, perform calculations, display the current date and time, use if/else and switch conditional statements, iterate through arrays with a foreach loop, and access array elements. The goal is to practice basic PHP concepts through examples and exercises.

Uploaded by

zefanya sharon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views4 pages

w01s02p Exercise

The document provides instructions and examples for accessing PHP files in a web browser using XAMPP, as well as examples of PHP code demonstrating variables, data types, operators, conditional statements, loops, and arrays. It includes code snippets to print "Hello World", define and use variables, perform calculations, display the current date and time, use if/else and switch conditional statements, iterate through arrays with a foreach loop, and access array elements. The goal is to practice basic PHP concepts through examples and exercises.

Uploaded by

zefanya sharon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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