Skip to content

Instantly share code, notes, and snippets.

@alphaolomi
Created December 6, 2021 05:32
Show Gist options
  • Save alphaolomi/109c5acc747fc3a760c56fa379340a73 to your computer and use it in GitHub Desktop.
Save alphaolomi/109c5acc747fc3a760c56fa379340a73 to your computer and use it in GitHub Desktop.
interview
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="/backend.php" method="post">
<h1>Selection Form</h1>
<small>*User name & colors selection is mandatory</small> <br>
User Name: <input type="text" name="user_name"><br>
Color: <input type="color" name="colors[]"><br>
<input type="color" name="colors[]"><br>
<input type="color" name="colors[]"><br>
<br>
<input type="submit">
</form>
</body>
</html>
<?php
$username = $_POST["user_name"];
$colors = $_POST["colors"];
echo "Hello $username, your favorite colors are:";
echo "<ul>";
foreach ($colors as $color) {
echo "<li>$color</li>";
}
echo "</ul>";
<?php
// Qn1 a
// a PHP function to remove specific element by value from an array
function removeElement($array, $element)
{
$key = array_search($element, $array);
if ($key !== false) {
unset($array[$key]);
}
return $array;
}
// Qn1 b
// a PHP function to remove specific element by value from an array using foreach
function removeElementForeach($array, $element)
{
foreach ($array as $key => $value) {
if ($value == $element) {
unset($array[$key]);
}
}
return $array;
}
// Qn 2a
// a PHP function to reverse the string
function reverseString($string)
{
$string = str_split($string);
$string = array_reverse($string);
$string = implode($string);
return $string;
}
// Qn 2b
// a PHP function that convert Array to String
function arrayToString($array)
{
$string = implode("", $array);
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment