A
A
Artem00712018-03-09 22:47:03
PHP
Artem0071, 2018-03-09 22:47:03

How to convert null, true, ... to string?

There is an array: You need to convert it to a string like: testnullfalsetrue I do this, but null, false, etc. are not displayed:
$data = ['test', null, false, true];

$string = '';
foreach ($data as $datum) {
     $string .= (string)$datum;
     // Или так:
     $string .= "$datum";
}

How to force to translate everything into text?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Artem0071, 2018-03-09
@Artem0071

It seems so, but maybe someone will have a better solution ..

$data = ['test', null, false, true];

        $string = '';

        foreach ($data as $datum) {
            $string .= is_string($datum) ? $datum : json_encode($datum);
        }

        return $string;

A
Anton Osipov, 2018-03-09
@tohaosipow

According to the syntax, something is wrong, but the essence is this:

$string = '';
foreach ($data as $datum) {
switch ($datum) {
    case null: {$string .= 'null'};
    case false: {$string .= 'false'};
    case true: {$string .= 'true'};
   default: {$string.=$datum};
}
}

S
Sergey Epifanov, 2018-03-09
@kacheleff

you can try var_export

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question