PHP code example of fort / php

1. Go to this page and download the library: Download fort/php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fort / php example snippets



nv\Dotenv::createImmutable(__DIR__)->load();
 



use Fort\PHP\Support\DB;

DB::connect();

// PDO Object: connected


use Fort\PHP\Support\DB;
use Carbon\Carbon;

DB::insert('users', [
 'name'=> 'Phil Everette',
 'email'=> '[email protected]',
 'phone'=> '0205550368',
 'created_at'=> Carbon::now() // fort/php already ships with carbon
]);

// @return (bool) true, false otherwise


use Fort\PHP\Support\DB;
use Carbon\Carbon;

DB::update('users', 1, [
 'name'=> 'Sam Everette',
 'email'=> '[email protected]',
 'phone'=> '0205550368',
 'created_at'=> Carbon::now()  
]);

// 1 is the id of the record to update
// @return (bool) true, false otherwise


use Fort\PHP\Support\DB;
 

public function createInvoice()
{
  try
   {
  DB::beginTransaction();
  //start some database queries
  DB::commit();
  // commit the queries
 }
 
 catch (Exception $exception)
 {
   DB::rollBack();
  // rollback the transaction in case of error
 }
}


use Fort\PHP\Support\DB;

$unpaidOrders = DB::table('orders')
  ->where('payment_status', '=', 'unpaid')
  ->orderBy('id', 'desc')
  ->get();
  
foreach ($unpaidOrders as $item){
  echo  $item['amount'];
}


use Fort\PHP\Support\DB;

$items = DB::rawQuery("SELECT users.id FROM users, 
 INNER JOIN orders ON users.id = orders.user_id")->get();
  
foreach ($items as $item){
 // echo ... ;
}


use Fort\PHP\Support\DB;

$adult = DB::table('users')
  ->select(['name', 'age'])
  ->where('age', '>', 18)
  ->orderBy('id', 'desc')
  ->get();
  
foreach ($adult as $individual){
  echo  $individual['age'];
}


use Fort\PHP\Support\DB;

DB::table('invoices')->all();
// @returns all invoices


use Fort\PHP\Support\DB;

DB::table('users')->where('email', '=','[email protected]')->first();
// @returns the record in the query set


use Fort\PHP\Support\DB;

DB::table('invoices')->where('amount_paid', '<', 1)->get();


use Fort\PHP\Support\DB;

DB::table('invoices')->where('amount_paid', '<', 1)
  ->orWhere('amount_paid', '=', false)
  ->get();


use Fort\PHP\Support\DB;

DB::table('invoices')->sum('amount_paid');
// @returns (int|float) a sum of the specified column



use Fort\PHP\Support\DB;

  DB::table('orders')->count();
  // @returns total number of orders


use Fort\PHP\Support\DB;

DB::table('invoices')->max('amount');
// @returns (int|float) the highest number in the column



use Fort\PHP\Support\DB;

DB::table('invoices')->min('amount');
// @returns (int|float) the smallest number in the column



 use Fort\PHP\Support\Http;
 
 Http::post('https://api.velstack.com/send',[$data],
            ["Accept" => "application/json", "Authorization" => "Bearer API_KEY}"];



 use Fort\PHP\Support\Http;
 
 Http::get('https://api.velstack.com/resource', 
            ["Accept" => "application/json", "Authorization" => "Bearer API_KEY}"],
            ['timeout'=> 20, 'return_transfer'=> true, 'maxredirs'=> 10, 'encoding'=> ""]);



 use Fort\PHP\Support\Http;
 
 Http::put('https://api.velstack.com/resource', [$data],
             ["Accept" => "application/json", "Authorization" => "Bearer API_KEY}"],



 use Fort\PHP\Support\Http;
 
 Http::delete('https://api.velstack.com/resource', 
             ["Accept" => "application/json", "Authorization" => "Bearer API_KEY}"],



 use Fort\PHP\Support\Http;
 
 Http::asPostMultipart('https://api.velstack.com/send', [$data],
             ["Accept" => "application/json", "Authorization" => "Bearer API_KEY"]
             );



 use Fort\PHP\Str;

 $haystack = array('FORT', 'Everette', 'Mike');
 // returns true if the needle exist in case insensitive, false otherwise
 Str::valueExist($haystack, 'foRT');
  // true


 use Fort\PHP\Str;

$slice = Str::contains('I was raised in Ghana', 'Ghana');
// 'true'



 use Fort\PHP\Str;

$string = Str::containsAll('My dad is from UK but i live in the US', ['UK', 'US']);

// 'true'



 use Fort\PHP\Str;

$slice = Str::after('His name is fort', 'His name');
// ' is fort'



 use Fort\PHP\Str;

 $slice = Str::afterLast('app\Http\Controllers\Controller', '\\');
 // 'Controller'



 use Fort\PHP\Str;

$slice = Str::before('He is married', 'married');
// 'He is'



 use Fort\PHP\Str;

$slice = Str::beforeLast('He is married', 'is');
// 'He'



 use Fort\PHP\Str;

$slice = Str::between('He was born in March', 'He', 'March');
// 'was born in'



 use Fort\PHP\Str;
 
 
$string = 'Transporter is a brilliant !';
 
$replaced = Str::replace('brilliant', 'genius', $string);
 
// Transporter is a genius !



 use Fort\PHP\Str;
 
 
$string = 'Transporter !';
 
$char = Str::charAt($string, 3);
 
// t



 use Fort\PHP\Str;
 
 
$string = 'Everette is 1 year old';
 
$string = Str::endsWith($string, 'old');
 
// true



 use Fort\PHP\Str;
 
 
$string = 'Everette is 1 year old';
 
$string = Str::finish($string, '!');
 
// Everette is 1 year old!


 use Fort\PHP\Str;
 
 
$matches = Str::is('foo*', 'foobar');
 
// true
 
$matches = Str::is('baz*', 'foobar');
 
// false



 use Fort\PHP\Str;
 
 
$isUuid = Str::isUuid('a0a2a2d2-0b87-4a18-83f2-2529882be2de');
 
// true
 
$isUuid = Str::isUuid('laravel');
 
// false



 use Fort\PHP\Str;
 
 
$isUuid = Str::length('Velstack');
 
// 8



 use Fort\PHP\Str;
 
  Str::words('Though he came, he could not make it.', 3, ' >>>');
 
// Though he came, >>>



 use Fort\PHP\Str;
 
  Str::words('He was born in July');
 
// 5



 use Fort\PHP\Str;
 
 
$truncated = Str::limit('He is a good man', 4, ' ...');
 
// He is ...



 use Fort\PHP\Str;
 
$padded = Str::padBoth('sammy', 10, '_');
 
// 'sammy'
 
$padded = Str::padBoth('James', 10);
 
// '  sammy   '


 use Fort\PHP\Str;
 
 
$padded = Str::padLeft('sammy', 6, '@');
 
// @sammy'
 
$padded = Str::padLeft('sammy', 6);
 
// '    sammy'


 use Fort\PHP\Str;
 
 $padded = Str::padRight('sammy', 10, '-');
 
// 'sammy-----'
 
$padded = Str::padRight('sammy', 10);
 
// 'sammy     '


 use Fort\PHP\Str;
 
$random = Str::random(5);

// 3Cewm


 use Fort\PHP\Str;
 
$output = Str::uuid();

// a0a2a2d2-0b87-4a18-83f2-2529882be2de
 
 


 use Fort\PHP\Str;
 
$isAscii = Str::isAscii('Accra');
 
// true
 
$isAscii = Str::isAscii('ü');
 
// false
 


 use Fort\PHP\Str;
 
$string = Str::isUuid('a0a2a2d2-0b87-4a18-83f2-2529882be2de');
 
// true
 
$string = Str::isUuid('Accra');
 
// false
 


 use Fort\PHP\Str;
 
 
$converted = Str::upper('fort');
 
// FORT


 use Fort\PHP\Str;
 
 
$converted = Str::lower('FORT');
 
// fort


 use Fort\PHP\Str;
 
 
$converted = Str::title('how to make money from home');
 
// How To Make Money From Home


 use Fort\PHP\Str;
 
 
$converted = Str::slug('how to make money from home','-');
 
// How-To-Make-Money-From-Home


 use Fort\PHP\Str;
 
 
$output = Str::snake('FootBall', '_');
 
// Foot_Ball


 use Fort\PHP\Str;
 
 $output = Str::studly('foo_bar');
 
// FooBar


 use Fort\PHP\Str;
 
 
$output = Str::kebab('fooBar');
 
// foo-bar


 use Fort\PHP\Str;
 
 
$string = Str::mask('[email protected]', '*', -15, 3);
 
// sam***@example.com


 use Fort\PHP\Str;
 
$adjusted = Str::start('sammy_fort', '@');
 
// @sammy_fort
 
$adjusted = Str::start('@fortameyaw', '@');
 
// @fortameyaw


 use Fort\PHP\Str;
 
$reversed = Str::reverse('Hello');
 
// olleH


 use Fort\PHP\Str;
 
 
$converted = Str::substr('The Symfony PHP Framework', 4, 7);
 
// Laravel


 use Fort\PHP\Str;
 
$count = Str::substrCount('If you like ice cream, you will like snow cones.', 'like');
 
// 2


 use Fort\PHP\Str;
 
$result = Str::substrReplace('1300', ':', 2);
// 13:
 
$result = Str::substrReplace('1300', ':', 2, 0);
// 13:00


 use Fort\PHP\Str;
 
$string  = 'He is lazy';

$output = Str::replaceFirst('He', 'Tom', $string);
 
// Tom is lazy


 use Fort\PHP\Str;
 
$string  = 'Kwaku is a handsome boy';

$output = Str::replaceLast('a', 'the', $string);
 
// Kwaku is the handsome boy


 use Fort\PHP\Str;
 
$output = Str::lcfirst('Handsome');
 
// handsome


 use Fort\PHP\Str;
 
$output = Str::ucfirst('beautiful');
 
// Beautiful


 use Fort\PHP\Arr;
 
$array = ['Transporter'=> 3, 'Phil'=> 0, 'Ever'=> 1];

print_r(Arr::maximum($array));

// Transporter


 use Fort\PHP\Arr;
 
$array = ['Transporter'=> 3, 'Phil'=> 1, 'Ever'=> 0];

print_r(Arr::minimum($array));

// Ever


 use Fort\PHP\Arr;
 
$array = ['Transporter', 'Phil', 'Ever'];

 print_r(Arr::valueExist($array, 'FORT'));

// true


 use Fort\PHP\Arr;
 
$array = ['Transporter' => 1, 'Phil'=> 2, 'Ever' => 3];

 print_r(Arr::accessible($array));

// true

$array = "fort";

 print_r(Arr::accessible($array));

// false


 use Fort\PHP\Arr;
 
$array = [0, null];
 
$filtered = Arr::whereNotNull($array);
 
// [0 => 0]


 use Fort\PHP\Arr;
 
$array = [100, '200', '300', '400', '500'];
 
$filtered = Arr::where($array, function ($value, $key) {
    return is_numeric($value);
});
 
// [0 => '100']


 use Fort\PHP\Arr;
 
$array = [1, 15, 35];
 
$first = Arr::first($array, function ($value, $key) {
    return $value >= 20;
});
 
// 35


 use Fort\PHP\Arr;
 
$array = [1, 15, 35, 40];
 
$first = Arr::last($array, function ($value, $key) {
    return $value >= 40;
});
 
// 40
 
 


 use Fort\PHP\Arr;
 
$string = 'PHP is fun';
 
$array = Arr::wrap($string);
 
// ['PHP is fun']


 use Fort\PHP\Arr;
 
$array = Arr::add(['name' => 'Desk'], 'price', 100);
 
// ['name' => 'Desk', 'price' => 100]
 
$array = Arr::add(['name' => 'Desk', 'price' => null], 'price', 100);
 
// ['name' => 'Desk', 'price' => 100]


 use Fort\PHP\Arr;
 
$array = ['orders' => ['desk' => ['price' => 100]]];
 
Arr::set($array, 'orders.desk.price', 200);
 
// ['orders' => ['desk' => ['price' => 200]]]


 use Fort\PHP\Arr;
 
$array = Arr::shuffle([1, 2, 3, 4, 5]);
 
// [3, 2, 5, 1, 4] - (generated randomly)


 use Fort\PHP\Arr;
$array = Arr::collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
 
// [1, 2, 3, 4, 5, 6, 7, 8, 9]


 use Fort\PHP\Arr;
 
[$keys, $values] = Arr::divide(['name' => 'Desk']);
 
// $keys: ['name']
 
// $values: ['Desk']


 use Fort\PHP\Arr;
 
$array = ['name' => 'Desk', 'price' => 100];
 
$filtered = Arr::except($array, ['price']);
 
// ['name' => 'Desk']


 use Fort\PHP\Arr;
 
$array = ['name' => 'Desk', 'price' => 100];
$array = ['name' => 'John Doe', 'age' => 17];
 
$exists = Arr::exists($array, 'name');
 
// true

$exists = Arr::exists($array, 'salary');
 
// false


 use Fort\PHP\Arr;
 
$array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']];
 
$flattened = Arr::flatten($array);
 
// ['Joe', 'PHP', 'Ruby']
 
// false


 use Fort\PHP\Arr;
 
$array = ['products' => ['desk' => ['price' => 100]]];
 
Arr::forget($array, 'products.desk');
 
// ['products' => []]


 use Fort\PHP\Arr;
 
$array = ['products' => ['desk' => ['price' => 100]]];
 
$price = Arr::get($array, 'products.desk.price');
 
// 100


 use Fort\PHP\Arr;
 
$array = ['product' => ['name' => 'Desk', 'price' => 100]];
 
$contains = Arr::has($array, 'product.name');
 
// true
 
$contains = Arr::has($array, ['product.price', 'product.discount']);
 
// false


 use Fort\PHP\Arr;
 
$array = ['product' => ['name' => 'Desk', 'price' => 100]];
 
$contains = Arr::hasAny($array, 'product.name');
 
// true
 
$contains = Arr::hasAny($array, ['product.name', 'product.discount']);
 
// true
 
$contains = Arr::hasAny($array, ['category', 'product.discount']);
 
// false


 use Fort\PHP\Arr;
 
$isAssoc = Arr::isAssoc(['product' => ['name' => 'Desk', 'price' => 100]]);
 
// true
 
$isAssoc = Arr::isAssoc([1, 2, 3]);
 
// false


 use Fort\PHP\Arr;
 
$array = ['one', 'two', 'three', 'four'];
 
$array = Arr::prepend($array, 'zero');
 
// ['zero', 'one', 'two', 'three', 'four']

$array = ['price' => 100];
 
$array = Arr::prepend($array, 'Desk', 'name');
 
// ['name' => 'Desk', 'price' => 100]


 use Fort\PHP\Arr;
 
$array = [
    ['product' => ['id' => 1, 'name' => 'Drink']],
    ['product' => ['id' => 2, 'name' => 'Wheat']],
];
 
$names = Arr::pluck($array, 'product.name');
 
// ['Drink', 'Wheat']


 use Fort\PHP\Arr;
 
$array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];
 
$slice = Arr::only($array, ['name', 'price']);
 
// ['name' => 'Desk', 'price' => 100]


 use Fort\PHP\Arr;
 
$array = ['name' => 'Desk', 'price' => 100];
 
$name = Arr::pull($array, 'name');
 
// $name: Desk
 
// $array: ['price' => 100]


 use Fort\PHP\Arr;
 
$array = [
    'name' => 'Mike',
    'order' => [
        'column' => 'created_at',
        'direction' => 'desc'
    ]
];
 
 $q = Arr::query($array);
 
// name=Mike&order[column]=created_at&order[direction]=desc


 use Fort\PHP\Arr;
 
$array = [1, 2, 3, 4, 5];
 
$random = Arr::random($array);
 
// 4 - (retrieved randomly)


 use Fort\PHP\Support\SMS;
 
 return SMS::send('+233205550368', 'Hello, API messaging is just awesome');


 use Fort\PHP\Math;
 
 Math::percentage(1.5, 200);
 // 3



 use Fort\PHP\Math;
 
 Math::expo(2, 2);
 // 4


 use Fort\PHP\Math;
 
 Math::sqrRoot(20);
  // 4.4721359549996


 use Fort\PHP\Math;
 
 Math::sum(11, 11);
 // 22


 use Fort\PHP\Math;
 
 Math::sub(10, 11);
 // -1


 use Fort\PHP\Math;
 
 Math::div(19, 5);
 // 3.8


 use Fort\PHP\Math;
 
 Math::mul(21, 8);
 // 168


 use Fort\PHP\Math;
 
 Math::max(2, 6, 4);
 // 6
bash
  composer 
php

 use Fort\PHP\Math;
 
 Math::min(2, 6, 4);
 // 2