196. PHP Program to Print Even position Elements in given array of elements.
How does this program work?
- In this program you are going to learn about how to print even position elements in given array of elements using PHP.
- By using given code we can get the even position elements only.
Here is the code
<?php
if(isset($_POST[ 'submit']))
{
$arr = array ();
$arr = $_POST['Fnum'];
echo "The array of Elements :<br>";
foreach($arr as $value)
{
echo $value." ";
}
echo("<br>Elements of given array present on even position:<br>");
for($i = 1; $i < count($arr); $i = $i+2) //To print even position elements
{
echo($arr[$i] . " ");
}
return 0;
}
?>
<html>
<head>
<title>PHP Program To print even position elements in given array of elements</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" id="num1" name="num1" value="" placeholder="Enter array size"/> </td>
</tr>
<div id="screens">
</div>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
function create(param) //To print no.of textbox depends on array size
{
'use strict';
var i;
$("#screens"). empty();
for (i = 0; i < param; i += 1)
{
$('#screens'). append('<input type="text" name="Fnum[]" placeholder="Enter number">');
}
}
$('#num1').change( function ()
{
create($(this).val());
});
</script>
</html>