Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added learn php #347

Merged
merged 2 commits into from
Oct 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Contributors.html
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,6 @@
<a class="box-item" href="https://github.com/YellowFoxH4XOR"><span>Akshat Katiyar</span></a>
<a class="box-item" href="https://github.com/VivekBhand"><span>Vivek Bhand</span></a>
<a class="box-item" href="https://github.com/Anmol55555"><span>Anmol Kesarwani</span></a>

<a class="box-item" href="https://github.com/Aryan-Srivastava""><span>Aryan Srivastava</span></a>
<a class=" box-item" href="https://github.com/Hackerrcracker"><span>Rahul Sharma</span></a>
<a class="box-item" href="https://github.com/kindise"><span>Kindi Setiadi</span></a>
Expand All @@ -528,7 +527,7 @@
<a class="box-item" href="https://github.com/mahatorakesh"><span>Rakesh Mahato</span></a>
<a class="box-item" href="https://github.com/Palaksharma23"><span>Palak Sharma</span></a>
<a class="box-item" href="https://github.com/Shivamdpandey"><span>Shivam Dhananjay Pandey</span></a>

<a class="box-item" href="https://github.com/fikriks"><span>Fikri Khairul Shaleh</span></a>



Expand Down
41 changes: 41 additions & 0 deletions Program's_Contributed_By_Contributors/PHP_Programs/Attribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

#[Attribute]
class Cart
{

}

class Checkout
{
#[NotEmpty]
public ?string $item;
}

// use reflaction
function validate(object $object)
{
$class = new ReflectionClass($object);
$properties = $class->getProperties();
foreach ($properties as $property) {
validateNotEmpty($property, $object);
}
}

// for get property
function validateNotEmpty(ReflectionProperty $property, object $object)
{
$attributes = $property->getAttributes(NotEmpty::class);
if (count($attributes) > 0) {
if (!$property->isInitialized($object))
throw new Exception("Property $property->name is null");
if ($property->getValue($object) == null)
throw new Exception("Property $property->name is null");
}
}


$request = new Checkout();
// $request->item = "";
$request->item = "1";
validate($request);
17 changes: 17 additions & 0 deletions Program's_Contributed_By_Contributors/PHP_Programs/ClassObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

// class
class Order
{

}

// create object
$check_class = new Order();

// pada php 7
var_dump(get_class($check_class));
var_dump(Order::class);

// pada php 8
var_dump($check_class::class);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

strlen([]);
// TypeError: strlen(): Argument #1 ($str) must be of type string, array given

array_chunk([], -1);
// ValueError: array_chunk(): Argument #2 ($length) must be greater than 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

class Order{

public function __construct(Public string $id, Public string $sender, Public string $receiver, Private int $quantity, Private string $item)
{}

public function getOrder(){
return "order id {$this->id} dengan pengirim bernama {$this->sender} dan penerimanya bernama {$this->receiver}";
}
}

$detail_order = new Order(id: "1", sender: "Fikri Khairul", receiver: "Shaleh", quantity: 2, item: "jajanan");
var_dump($detail_order);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
$word = "Saya ingin berbelanja di shopee karena gratis ongkir";

$result = match(true){
str_contains($word, "shopee") => "Kalimat anda mengandung kata sebuah aplikasi online store",
str_contains($word, "gojek") => "Kalimat anda mengandung kata sebuah aplikasi ojek online",
default => "Kalimat anda tidak mengandung kata apa pun"
};

echo $result;
16 changes: 16 additions & 0 deletions Program's_Contributed_By_Contributors/PHP_Programs/MixedTypeV2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

function funcMixed(mixed $param): mixed
{
if (is_string($param)) {
return "String";
}else if(is_int($param)){
return 1;
}else if(is_array($param)){
return [];
}
}

var_dump(funcMixed('Ini adalah sebuah kalimat'));
var_dump(funcMixed(120));
var_dump(funcMixed(['array 1', 'array 2']));
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
function funcTest($box_one = "", $box_two = "kambing", $box_three = "", $box_four = "sapi"){
echo $box_one .' '. $box_two .' '. $box_three .' '. $box_four;
}

return funcTest(box_one: "ikan", box_three: "kerbau");
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

// contains - mengandung kata kata yang kita cari
var_dump(str_contains("Saya ingin Belanja Online di Shopee", "Shopee"));

// start with - mengandung kata kata yang kita cari di awal kata / kalimat
var_dump(str_starts_with("PHP 8 adalah versi terbaru saat ini dari PHP", "PHP"));

// end with - mengandung kata kata yang kita cari di akhir kata / kalimat
var_dump(str_ends_with("Hari Jumat", "Jumat"));
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
class Order{
public ?string $item;
}

class Transaction{
public ?Order $order;
}

function getItem(?Transaction $transaction): ?string
{
return $transaction?->order?->item;
}

echo getItem(null);
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

$compare_1 = 0 == "0"; // PHP 7 or earlier (TRUE) | PHP 8 (TRUE)
$compare_2 = 0 == "0.0"; // PHP 7 or earlier (TRUE) | PHP 8 (TRUE)
$compare_3 = 0 == "kata"; // PHP 7 or earlier (TRUE) | PHP 8 (FALSE)
$compare_4 = 0 == ""; // PHP 7 or earlier (TRUE) | PHP 8 (FALSE)
$compare_5 = 12 == " 60 "; // PHP 7 or earlier (TRUE) | PHP 8 (TRUE)
$compare_6 = 12 == "ini adalah string60"; // PHP 7 or earlier (TRUE) | PHP 8 (FALSE)

var_dump($compare_1);
var_dump($compare_2);
var_dump($compare_3);
var_dump($compare_4);
var_dump($compare_5);
var_dump($compare_6);
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

function orderFood(string $store, string $item)
{

}

orderFood(
"KFC",
"Paket Hemat 1",
);

$listOrder = [
"store" => "KFC",
"item" => "Paket Hemat 1",
"delivery_price" => "15.000",
"discount" => "5.000",
"total" => "50.000",
"driver" => "Fikri Khairul Shaleh",
"driver" => "Fikri Khairul Shaleh",
"driver" => "Fikri Khairul Shaleh",
"driver" => "Fikri Khairul Shaleh",
"driver" => "Fikri Khairul Shaleh",
"driver" => "Fikri Khairul Shaleh",
"driver" => "Fikri Khairul Shaleh",
"driver" => "Fikri Khairul Shaleh",
"driver" => "Fikri Khairul Shaleh",
];

print_r($listOrder);
24 changes: 24 additions & 0 deletions Program's_Contributed_By_Contributors/PHP_Programs/UnionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

// Union Type Pada Property
class word{
public float|string $data;
}

$get_word = new word();
echo $get_word->data = "tipe string ini adalah kalimat";

// Union Type Pada Value
function funcTest(float|string|int $data): float|string|int {
if(is_int($data)) {
return 10;
} else if(is_float($data)) {
return 1.5;
} else if(is_string($data)) {
return 'ini adalah return string';
}
}

var_dump(funcTest("masukan string"));
var_dump(funcTest(80));
var_dump(funcTest(5.0));