W3 PemrogramanWeb PHP Predefined Function-1
W3 PemrogramanWeb PHP Predefined Function-1
array directory
functions functions
String Function
• strtolower(); = digunakan untuk dapat mengubah string
menjadi huruf kecil
https://www.w3schools.com/php/php_ref_string.asp
String Function
• stristr(); = digunakan untuk dapat mencari kemunculan
pertama string di dalam string lain.
• latihan
<?php
echo strlen("Hello");
?>
strlen();
• Output
5
trim();
• latihan
<?php
$str = "Hello World!";
echo $str . "<br>";
echo trim($str,"Hed!");
?>
trim();
• Output
Hello World!
llo Worl
Numeric Function
• abs(); • pi();
• sqrt(); • min();
• round(); • max();
• floor(); • fmod();
• ceil(); • bindec();
• rand(); • decbin();
• mt_rand(); • deg2rad();
• pow(); • rad2deg();
https://www.w3schools.com/php/php_ref_math.asp
sqrt();
• latihan
<?php
echo(sqrt(0) . "<br>");
echo(sqrt(1) . "<br>");
echo(sqrt(9) . "<br>");
echo(sqrt(0.64) . "<br>");
echo(sqrt(-9));
?>
sqrt();
• Output
0
1
3
0.8
NAN
round();
• latihan
<?php
echo(round(0.60) . "<br>");
echo(round(0.50) . "<br>");
echo(round(0.49) . "<br>");
echo(round(-4.40) . "<br>");
echo(round(-4.60));
?>
round();
• Output
1
1
0
-4
-5
floor();
• latihan
<?php
echo(floor(0.60) . "<br>");
echo(floor(0.40) . "<br>");
echo(floor(5) . "<br>");
echo(floor(5.1) . "<br>");
echo(floor(-5.1) . "<br>");
echo(floor(-5.9));
?>
floor();
• Output
0
0
5
5
-6
-6
ceil();
• latihan
<?php
echo(ceil(0.60) . "<br>");
echo(ceil(0.40) . "<br>");
echo(ceil(5) . "<br>");
echo(ceil(5.1) . "<br>");
echo(ceil(-5.1) . "<br>");
echo(ceil(-5.9));
?>
ceil();
• Output
1
1
5
6
-5
-5
Array Function
• Array(); = Membuat sebuah Array
• Array_Change_key_case(); = mengembalikan
sebuah Array dengan semua semua Key dalam
bentuk Lowercase atau Uppercase
• Array_Chunk(); = Membagi array menjadi
potongan array
• Array_diff(); = Membandingkan nilai array, dan
mengembalikan perbedaan
• Etc…
Sumber:
https://www.tutorialspoint.com/php/php_array_functions.htm
Date and Time Function
• date(); = digunakan untuk menampilkan data tanggal sesuai dengan format yang
diinginkan
https://www.w3schools.com/php/php_ref_date.asp
Date and Time Function
• date_diff(); = digunakan untuk mengembalikan
perbedaan antara dua objek DateTime
https://www.w3schools.com/php/php_ref_date.asp
getdate();
• latihan
<?php
print_r(getdate());
?>
getdate();
• Output
Array ( [seconds] => 46 [minutes] => 59 [hours]
=> 6 [mday] => 1 [wday] => 2 [mon] => 3 [year]
=> 2022 [yday] => 59 [weekday] => Tuesday
[month] => March [0] => 1646114386 )
date_add();
• latihan
<?php
$date=date_create("2013-03-15");
date_add($date,date_interval_creat
e_from_date_string("40 days"));
echo date_format($date,"Y-m-d");
?>
date_add();
• Output
2013-04-24
date_diff();
• latihan
<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
date_diff();
• Output
+272 days
PHP Date Function
<?php
echo "The time is " . date("h:i:sa");
?>
Membuat tanggal dan waktu
• mktime() berfungsi untuk membuat tanggal dan
waktu yang spesifik
mktime(jam, menit, detik, bulan, hari , tahun).
<?php
$d=mktime(11, 14, 54, 3,12, 2020);
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>
Membuat tanggal dan waktu
• date_create() untuk membuat objek DateTime
• date_format() untuk memformat tampilan objek
DateTime
<?php
$date=date_create("2013-03-15");
echo date_format($date,"Y/m/d H:i:s");
echo “<br>”;
date_default_timezone_set('Asia/
Jakarta’);
echo date(‘m-d-Y H:i:s');
?>
PHP Include & Require
Isi body
footer.php
index.php
Latihan 1