PHPackages                             1tomany/get-safely - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. 1tomany/get-safely

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

1tomany/get-safely
==================

A simple library to safely get scalar types from values of unknown origin or type

v0.0.1(10mo ago)020MITPHPPHP &gt;=8.4

Since Jul 10Pushed 10mo agoCompare

[ Source](https://github.com/1tomany/get-safely)[ Packagist](https://packagist.org/packages/1tomany/get-safely)[ RSS](/packages/1tomany-get-safely/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

get-safely for PHP
==================

[](#get-safely-for-php)

This simple library exposes several PHP functions that assist with static analysis by ensuring values of unknown or mixed types are a scalar type.

Installation
------------

[](#installation)

```
composer require 1tomany/get-safely

```

Functions
---------

[](#functions)

### `get_string(): string`

[](#get_string-string)

This function returns the `$value` argument if it is a string or implements the `\Stringable` interface, otherwise it returns the value of the `$default` argument. If the `$default` argument is of type `callable`, the `$value` is passed to it and the result of the callable is returned. An exception is thrown if the callable does not return a string.

#### Arguments

[](#arguments)

- `mixed $value`
- `string|callable $default = ''`

### `get_string_loose(): string`

[](#get_string_loose-string)

This function is identical to `get_string()` *except* the `$value` argument is first cast to a string if it is a scalar value (`boolean`, `int`, `float`, or `string` in PHP).

#### Arguments

[](#arguments-1)

- `mixed $value`
- `string|callable $default = ''`

Examples
--------

[](#examples)

```
