To get even and odd numbers from a loop by using function

<?php
    function evenOdd($start,$end,$type){
        for($i=$start; $i<=$end; $i++){
            if($type == "even" && $i%2==0){
                echo "<pre>";
                echo $i;
            }elseif($type == "odd" && $i%2!=0){
                echo "<pre>";
                echo $i;
            }
        }
    }

    evenOdd(10,20,"even");
?>

Here we have created a function named evenOdd(); this is user defined function which have been created.
There are three parameters in this function. So total three values will pass through this function.
Loop will start from 10 and will be end to 20 and in string part if you write even you will just get even numbers and if you write odd you will get only odd numbers.

Comments

Popular posts from this blog

WP register_post_type() with custom CMB2 meta box

Git post receive setup at server for git push to the production from local machine