PHP code example of mpscholten / value-or-default
1. Go to this page and download the library: Download mpscholten/value-or-default 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/ */
mpscholten / value-or-default example snippets
// Env variables
$tempDir = \MPScholten\valueOrDefault($_ENV['TEMP_DIR'], '/tmp');
// $tempDir is now either $_ENV['TEMP_DIR'] or '/tmp' dependending on whether $_ENV['TEMP_DIR'] is set
// JSON input
$user = json_decode(...);
$userName = \MPScholten\valueOrDefault($user['name'], 'n/a');
// $userName is now either $user['name'] or 'n/a' depending on whether $user['name'] is set and non-null
// Null values
$value = null;
\MPScholten\valueOrDefault($value, 0); // returns 0 because $value == null