Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

Difference between in_array() and array_search() function in php

 Function in_array() checks if the specified value exits in the array.

Function array_search() searches an array for a given value and returns the key.

Example of in_array() function
$a1 = array(“one”,”b”=>”two”);
if(in_array(“one”,$a1))
{
echo “exist”;
}else
{
echo “not exist”;
}

It will print “exist”

Example of array_search() function
$a1=array(“a”=>”one”,”b”=>”two”);
echo array_search(“one”,$a1);

It will return key “a” which is key of searched value “one”.

PHP Recursive Function
PHP also supports recursive function call like C/C++. In such case, we call current function within function. It is also known as recursion.

Like most programming languages that support functions, PHP lets you write recursive functions. In this tutorial, we’ll explore the concept of recursion in PHP, and discover how to create recursive functions for various tasks.

If the function keeps calling itself, how does it know when to stop? You set up a condition, known as a base case. Base cases tell our recursive call when to stop, otherwise it will loop infinitely.

Let’s write a recursive function to display the numbers.

Example : Printing number

<?php
function display($number) {
//If the number is less or equal to 5.
if($number<=5){
echo “$number <br/>”;
display($number+1);
}
}
//Set our start number to 1.
display(1);
?>

Output:

1
2
3
4
5

The code above is pretty simple. Basically, the function checks to see if the number is less than 5. If the number is less than 5, then we increment the number and call the function again. This process is repeated until the number has reached 5.

must watch some other topics :

cake PHP training institute
cake PHP training course
cake php training online

Post a Comment

0 Comments