Created
July 15, 2019 19:22
-
-
Save ImtiazEpu/2649ad78dcf954b01beb85da0798ccf2 to your computer and use it in GitHub Desktop.
Problem solving
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Basis Knowledge Test: | |
//========================== | |
//1. Write a PHP function that to remove all zeroes from a string and return 3 different output. | |
//Input String : '000892021.2408000' | |
//Expected below outputs | |
//Output 1: '892021.2408' (all zeros remove from start and end) | |
//Output 2: '892021.2408000' (remove only start zero) | |
//Output 3: '000892021.2408' (remove only end zero) | |
//Example Uses: echo remove_zero('000892021.2408000', 'all'); | |
function remove_zero($str, $con = 'all') | |
{ | |
if ($con) { | |
$left_trim = ltrim($str, "0"); | |
$right_trim = rtrim($str, "0"); | |
$all_trim = ltrim($right_trim); | |
//Output | |
echo "Output All: '" . $all_trim . "' (all zeros remove from start and end)<br>"; | |
echo "Output Left: '" . $left_trim . "' (remove only start zero)<br>"; | |
echo "Output Right: '" . $right_trim . "' (remove only end zero)"; | |
} | |
} | |
echo remove_zero('000892021.2408000', 'all'); | |
//2. Write a PHP function that accept 1 parameter as string and return all value in JSON format as bellow reference. | |
//Input Array: array("gateway" => array("bkash"), "currency" => array("usd", "cad")); | |
//Expected Output: '{"type":"currency","value":["usd", "cad"]}' | |
//Example Uses: var_dump(get_data('currency)); | |
function get_data($a){ | |
$data = array(); | |
$data['gateway'] = array("bkash"); | |
$data['currency'] = array("usd", "cad"); | |
$b = array(); | |
$b[type] = $a; | |
$b[value] = $data[$a]; | |
return Json_encode($b); | |
} | |
var_dump(get_data('currency')); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment