PHP code example of theodorejb / polycast

1. Go to this page and download the library: Download theodorejb/polycast 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/ */

    

theodorejb / polycast example snippets


use function theodorejb\polycast\{ safe_int, safe_float, safe_string };

if (!safe_string($_POST['name'])) {
    echo 'Name must be a string';
} elseif (!safe_int($_POST['quantity'])) {
    echo 'Quantity must be an integer';
} elseif (!safe_float($_POST['price'])) {
    echo 'Price must be a number';
} else {
    addProduct($_POST['name'], (int)$_POST['quantity'], (float)$_POST['price']);
}

function addProduct(string $name, int $quantity, float $price)
{
    // ... a database query would go here
}

use theodorejb\polycast;

try {
    $totalRevenue = 0.0;
    $totalTransactions = 0;

    foreach ($csvRows as $row) {
        $totalRevenue += polycast\to_float($row['monthly_revenue']);
        $totalTransactions += polycast\to_int($row['monthly_transactions']);
    }

    // do something with totals
} catch (polycast\CastException $e) {
    echo "Error: " . $e->getMessage();
    var_dump($e->getTrace());
}