So I found this SHORTHAND PHP methods on google like:
if(isset($a))
{
$a = TRUE;
}
else
{
$a = FALSE;
}
can be converted on a single line statement as:
$a = isset($a) ? TRUE : FALSE;
which is working correctly and I have a script which I fail to apply the SHORT HAND method.
PHP:
<?php
$letters = ['1' => 'A', '2' => 'B', '3' => 'C'];
$data['LETTER'] = "";
foreach($letters as $id => $letter)
{
$data['LETTER'] .= "<option value=$id>".$letter."</option>";
}
$html = file_get_contents('test.html');
echo $html = str_replace(array_keys($data),array_values($data),$html);
?>
I've end using with this SHORTHAND METHOD which won't work at all
$data['LETTER'] = foreach($letters as $id => $letter) ? "<option value=$id>".$letter."</option>" : "";
is there any possibilities to shorten the script above?
There is no shorthand method for foreach
, however you can achieve it with array_map
:
$data['LETTER'] = implode(array_map(function($id, $letter){ return "<option value=$id>".$letter."</option>"; }, $letters));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With