How to find length of array using php

we are luck enough to find the length of an array using php, we have two build in function to find the count of an array using php.Lets see in details that function

1) sizeof() function

2) count() function

First let we check out the syntax of sizeof() function

 sizeof($Array);
example

$colors=array("RED","BLUE","GREEN");
echo sizeof($colors);

Output

3


let we check out the syntax of count() function

 count($Array);
example

$colors=array("RED","BLUE","GREEN");
echo count($colors);

Output

3

How to use html encode function in php to prevent browsers from using it as an HTML

In this section we are going check the usage of html encode in PHP here our question is how to use html encode function in php to prevent browsers from using it as an HTML .sometimes we have to display html element in the front end as it is without coveting it.for example we need to display below line

<h2>Hello</h2> <p>Hello here here we are disusing the usage of html encode function php to prevent browsers from using it as an HTML   </p>

but we know that normally it display like below

use html encode function php to prevent browsers from using it as an HTML

we are lucky enough to handle this situation we have a html encode function in php it prevent browsers from using it as an HTML lets see the function htmlentities

htmlentities(string,flags,character-set,double_encode)
htmlentities ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = TRUE ]]] ) 

default
htmlentities($htmlcode);

See example how it is working

 $htmlcode='<h2>Hello</h2>
Hello here here we are disusing the usage of html encode function php to prevent browsers from using it as an HTML';
echo htmlentities($htmlcode);

Output

Hello

Hello here here we are disusing the usage of html encode function php to prevent browsers from using it as an HTML

How to apply array_slice in PHP object array

Here my requirement is to fetch first five items from an Object if it is a normal array means i can fetch the first nth item from the php array using the PHP build in function array_slice() for example we have an array like below
let we check how to get first element of array in using array_slice()
after we will discuss how to apply array_slice in PHP object array

$myarray=array("Mangoes","Jack Fruits","coconuts","Bananas","Apple","Orange");

print_r(array_slice($myarray,0,2));

Output

Array ( [0] => Mangoes [1] => Jack Fruits)

In the given example fetching first 2 values from the array using build in function array_slice();

Now my issues is here my array is not normal array it is an object array so i can’t use directly array_slice()

First i have to convert Object array to normal array and i can i apply the code see example below

$Myarray=(array)$OBJ_array;

print_r(array_slice($Myarray,0,5));

it will return an array with first five value of $OBJ_array

how to add google news feed in your websites using PHP

Here we are going to discus how to add google news feed in your websites using PHP ,we know that google is proving XML for news feed here is the link for that

https://news.google.com/rss/search?q=KEYWORD

Here KEYWORD is search query what we are going to add as news feed in your website

This link output xml formatted news feed from that latest websites we can use same xml in your site ,we can use PHP function to convert from one suitable format

Suppose we need to display Cricket World Cup news feed in your website you have to replace KEYWORD with ‘Cricket World Cup’ see example API link below

https://news.google.com/rss/search?q=Cricket+World+Cup&hl=en-IN&gl=IN&ceid=IN:en

Next Step to get the data from the XML

we need to use simplexml_load_file for that

See below code

 


$xml=simplexml_load_file("https://news.google.com/rss/search?q=Cricket+World+Cup&amp;hl=en-IN&amp;gl=IN&amp;ceid=IN:en");
$arrayG = json_decode(json_encode((array)$xml), TRUE);

TO display latest 10 item we have to use

$arrayFeed=array_slice($arrayG['channel']['item'], 0,2);
foreach($arrayFeed as $key=&gt;$value)
{

echo "</pre>
<div class="col-md-9 nfspara">".$value['description']."</div>
"; }

see demo out from the below link
News Feed

Out Put

add google news feed in your websites using PHP