PHP code example of ctw / ctw-cast

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

    

ctw / ctw-cast example snippets


(int) "42abc";     // 42 (silently ignores "abc")
(int) [];          // 0 (arrays become 0 or 1)
(bool) "false";    // true (non-empty string)
(float) "invalid"; // 0.0 (no error)

use Ctw\Cast\Cast;

// Environment variables
$debug    = Cast::toBool($_ENV['DEBUG'] ?? 'false');
$port     = Cast::toInt($_ENV['DB_PORT'] ?? '3306');
$timeout  = Cast::toFloat($_ENV['TIMEOUT'] ?? '30.0');
$appName  = Cast::toString($_ENV['APP_NAME'] ?? '');

// GET/POST parameters
$page     = Cast::toInt($_GET['page'] ?? '1');
$limit    = Cast::toInt($_GET['limit'] ?? '10');
$active   = Cast::toBool($_POST['active'] ?? 'false');
$tags     = Cast::toArray($_POST['tags'] ?? '[]');

// Server variables
$port     = Cast::toInt($_SERVER['SERVER_PORT'] ?? '80');
$https    = Cast::toBool($_SERVER['HTTPS'] ?? 'off');

// Session data
$userId   = Cast::toInt($_SESSION['user_id'] ?? '0');
$prefs    = Cast::toArray($_SESSION['preferences'] ?? '{}');

use Ctw\Cast\Cast;

// Legacy database result (returns strings for all columns)
$row    = $legacyDb->fetchRow("SELECT id, price, active FROM products");
$id     = Cast::toInt($row['id']);
$price  = Cast::toFloat($row['price']);
$active = Cast::toBool($row['active']);

// Legacy configuration loader
$config     = $legacyLoader->load('app.ini');
$maxSize    = Cast::toInt($config['upload_max_size']);
$debugMode  = Cast::toBool($config['debug']);
$allowedIps = Cast::toArray($config['allowed_ips']);

// Legacy API client
$response = $legacyClient->get('/api/users');
$users    = Cast::toArray($response);
$jsonOut  = Cast::toJson($users);

// Untyped function returns
$result = some_legacy_function();
$count  = Cast::toInt($result);

use Ctw\Cast\Cast;

$string = Cast::toString($value);
$int    = Cast::toInt($value);
$float  = Cast::toFloat($value);
$bool   = Cast::toBool($value);
$array  = Cast::toArray($value);
$json   = Cast::toJson($value);

Cast::toString(42);           // "42"
Cast::toString(true);         // "1"
Cast::toString(null);         // ""
Cast::toString([1, 2, 3]);    // ""

Cast::toInt("42");        // 42
Cast::toInt(3.7);         // 4 (rounded)
Cast::toInt(null);        // 0
Cast::toInt("hello");     // 0
Cast::toInt(INF);         // 0

Cast::toFloat("3.14");    // 3.14
Cast::toFloat(42);        // 42.0
Cast::toFloat(true);      // 1.0
Cast::toFloat("hello");   // 0.0

Cast::toBool("yes");   // true
Cast::toBool(0);       // false
Cast::toBool(null);    // false
Cast::toBool("maybe"); // false
Cast::toBool(42);      // false

Cast::toArray('{"a":1}');   // ["a" => 1]
Cast::toArray(42);          // [42]
Cast::toArray(null);        // []

Cast::toJson(['name' => 'John']);   // '{"name":"John"}'
Cast::toJson(true);                 // 'true'
Cast::toJson(null);                 // 'null'
Cast::toJson(INF);                  // '{}'

use Ctw\Cast\Cast;

$int   = Cast::toInt($userInput);   // 0 if not castable
$str   = Cast::toString($userInput); // '' if not castable
$bool  = Cast::toBool($userInput);   // false if not castable
$float = Cast::toFloat($userInput);  // 0.0 if not castable
$arr   = Cast::toArray($userInput);  // [] if not castable
$json  = Cast::toJson($userInput);   // '{}' if not castable