Showing posts with label example. Show all posts
Showing posts with label example. Show all posts

Ob_start() Function -Tutorials and Examples in Php5

Ob_start() function- Tutorials,Examples in Php5


ob_start Function is used to turn on output buffering.The PHP output buffering will save all the server outputs to a string variable.it  returns TRUE on success or FALSE on failure.
If you are using ob_start, then you should also use ob_end_flush  function for flushing the output buffer.

Ob_start() and ob_end_flush()  Example:

<?php
function changeColor($buffer)
{
  return (str_replace("Blue", "Red", $buffer));
}
//ob_start() function in PHP5
ob_start("changeColor");
?>
<html>
<tile>Ob_start() funtion Tutorials,Examples in Php5</title>
<body>
<span>Blue Car</span>
</body>
</html>
<?php
//ob_end_flush() function in PHP5
ob_end_flush();
?>
Output:
<html>
<tile>Ob_start() funtion Tutorials,Examples in Php5</title>
<body>
<span>Red Car</span>
</body>
</html>

Ob_start() and ob_get_clean()  Example:

<?php 
ob_start();
 ?>
<html>
<tile>Ob_start() funtion Tutorials,Examples in Php5</title>
<body>
<span>Blue Car</span>
</body>
</html>
<?php
 $code = ob_get_clean();
?>

Output:
<html>
<tile>Ob_start() funtion Tutorials,Examples in Php5</title>
<body>
<span>Blue Car</span>
</body>
</html>


Bookmark and Share

PHP implode() Function


PHP implode() Function


implode : The implode() function returns a string from the elements of an array.


Syntax
implode(separator,array)


Example: implode() 


<?php
$arr = array("PHP","implode","explode" ,"string","functions");
$str = implode(",", $arr);
echo $str; 
?>


OutputPHP, implode, explode, string, functions.


Bookmark and Share

PHP explode() Function


PHP explode() Function


explode :- The explode() function split a string into an array.

Syntax
explode(separator,string)


Note: Separator cannot be an empty string

Example: explode()

<?php


$name= "PHP implode explode string functions";
$arr = (" ", $name);
print_r($arr);
?>


Output:
array([0]=>"PHP",[1]=>"implode",[2]=>"explode",[3]=>"string",[4]=>"functions");


Bookmark and Share

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

Difference between in_array() and array_search() in php?
In this tutorial,we will study difference between in_array and array_search.

in_array : -Checks if a value exists in an array
array_search() :-searches an array for a given value and returns the  corresponding key if successful.

in_array Syntax:
in_array(search value, array , strict Boolean);
search value:- will be the value to search in an array
array: will the array to search
strict Boolean: is optional.

Example:

<?php
$name = array("Rajesh", "Sharma", "PHP", "Linux");
if (in_array("Rajesh", $name)) 
{
    echo "Yes";
}
else
{
echo "False";
}
?>

Output: Yes

array_search() :-searches an array for a given value and returns the  corresponding key if successful.

Syntax:
array_search()(search value, array , strict Boolean);
search value:- will be the value to search in an array
array: will the array to search
strict Boolean: is optional.

Example:

<?php
$name = array(0=>"Rajesh", 1=>"Sharma", 2=>"PHP", 3=>"Linux");
echo $key = array_search("Rajesh", $name);

?>
Output: 0


Bookmark and Share