in_array() is the function to Check if a value exists in an array using PHP. Here is an example of how it is used: –
1 2 3 4 |
$array = array("IN", "US", "UK", "AUS"); if (in_array("IN", $array)) { echo "<p>IN is in array!</p>"; } |
Full Stack LAMP, MEAN, DevOps Consultant
in_array() is the function to Check if a value exists in an array using PHP. Here is an example of how it is used: –
1 2 3 4 |
$array = array("IN", "US", "UK", "AUS"); if (in_array("IN", $array)) { echo "<p>IN is in array!</p>"; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php // items from db $array[] = [10, 20, 10]; $array[] = [10, 20, 201]; $array[] = [10, 20, 501]; $array[] = [10, 20, 601]; // new items $arrayb[] = [10, 20, 10]; $arrayb[] = [10, 20, 20]; $arrayb[] = [10, 20, 50]; $arrayb[] = [10, 20, 60]; function compare(&$array,&$arrayb) { foreach ($array as $k => $v) { foreach ($arrayb as $k2 => $v2) { if($v == $v2) { unset($array[$k],$arrayb[$k2]); // removed matched items } } } } compare($array,$arrayb); // delete all print_r($array); echo '============'; // insert all print_r($arrayb); |
You can use the fgets() function with combination of fopne() funciton to read the file line by line.
1 2 3 4 5 6 7 8 9 10 |
$filename = "inputfile.txt"; if (file_exists($filename) && is_readable ($filename)) { $fileResource = fopen($filename, "r"); if ($fileResource) { while (($line = fgets($fileResource)) !== false) { echo $line; } fclose($fileResource); } } |
1 2 3 4 |
$file = new SplFileObject("file.txt"); while (!$file->eof()) { echo $file->fgets(); } |
Here is the simple PHP function for testing given data is null or empty string.
1 2 3 |
function IsNullOrEmptyString($data){ return (!isset($data) || trim($data)===''); } |
Here is the simple function for detecting spiders and robots. It will return true or false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
function is_bot(){ $bots = array( 'Googlebot', 'Baiduspider', 'ia_archiver', 'R6_FeedFetcher', 'NetcraftSurveyAgent', 'Sogou web spider', 'bingbot', 'Yahoo! Slurp', 'facebookexternalhit', 'PrintfulBot', 'msnbot', 'Twitterbot', 'UnwindFetchor', 'urlresolver', 'Butterfly', 'TweetmemeBot'); foreach($bots as $b){ if( stripos( $_SERVER['HTTP_USER_AGENT'], $b ) !== false ) return true; } return false; } |
Here is the example snippet for highlighting current menu item using php.
1 2 3 4 5 6 7 8 9 10 |
<ul class="sub-nav" > <?php $full_name = $_SERVER['PHP_SELF']; $name_array = explode('/',$full_name); $count = count($name_array); $page_name = $name_array[$count-1]; ?> <li><a class="<?php echo ($page_name=='about-us.php')?'active':'';?>" href="about-su.php">About Us</a></li> <li><a class="<?php echo ($page_name=='contact-us.php')?'active':'';?>" href="contact-us.php">CONTACT US</a></li> </ul> |
CodeIgniter : https://github.com/bcit-ci/CodeIgniter.git I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – we are here to solve your problems.ArjunI am Arjun […]
1 2 3 |
var image = document.getElementById('my_image'); var width = image.naturalWidth; var height = image.naturalHeight; |
1 2 |
echo $currentScriptFileName = basename($_SERVER['PHP_SELF']); // My Output : main.php |
1 2 |
echo $currentScriptFileName = echo basename(__FILE__); // My Output : main.php |