Showing posts with label PHP array_key_exists. Show all posts
Showing posts with label PHP array_key_exists. Show all posts

Thursday, 30 August 2018

PHP: with an associative array of counters, is it more idiomatic to explicitly set each value to 0 before the first increment?


I'm writing some code that builds up associative array of counters. When it encounters a new item for the first time it creates a new key and initializes it to zero. I.e.:


if (!array_key_exists($item, $counters)) {
    $counters[$item] = 0;
}
$counters[$item]++;

However, PHP actually does that first part implicitly. If I just do...
 $counters[$item]++;

... then $counters[$item] will evaluate to NULL and be converted to 0 before it's incremented. Obviously the second way is simpler and more concise, but it feels a little sleazy because it's not obvious that $counters[$item] might not exist yet. Is one way or the other preferred in PHP?
For comparison, in Python the idiomatic approach would be to use collections.Counter when you want keys that initialize themselves to 0, and a regular dictionary when you want to initialize them yourself. In PHP you only have the first option.

the first is preferred. the second option will generate a Notice in your logs that $counters[$item] is undefined. it still works but if you change display_errors = On; and error_reporting = E_ALL. in your php.ini file you will see these notices in your browser.

Tuesday, 2 June 2015

PHP: Check Referrer This checks if the referrer is from the site you expect it to be.

<?php
function check_ref($site = 'thiscode4u.blogspot.com')
{
 if (!array_key_exists('HTTP_REFERER', $_SERVER))
  return false;
 
 $parse = parse_url($_SERVER['HTTP_REFERER']);
 $host = $parse['host'];
 $host = str_replace('www.', '', $host);
 
 return ($host == $site);
}
?>

Usage

if (check_ref('etogre.com')) // they came from etogre.com

die('We love you =^_^= !');

else

die('We hate you -_- !');

Wednesday, 24 September 2014

array_key_exists in PHP

array_key_exists function checks the input key is exists in a given array if key available in the array then the function returns true.

Syntax: array_key_exists(key,array)

Parameters Description:
key : Required. Define the key.
array : Required. Define an array.

Tip: if key not defined in an array then an integer key is returned to starting 0.

Example:

<?php
$daysarr1 = array("a"=>"Sunday","b"=>"Monday");
if(array_key_exists("b",$daysarr1))
{
       echo " "b" Key Exists in array $daysarr1"."<br />";
}
else
{
        echo " "b" Key does not exists in array $daysarr1"."<br />";
}
$daysarr2 = array("a"=>"Sunday","b"=>"Monday");
if(array_key_exists("c",$daysarr2))
{
       echo " "c" Key Exists in array $daysarr2"."<br />";
}
else
{
         echo " "c" Key does not exists in array $daysarr2"."<br />";
}
$daysarr3 = array("Sunday","Monday");
if(array_key_exists(0,$daysarr3))
{
         echo " 0 Key Exists in array $daysarr3"."<br />";
}
else
{
           echo " 0 Key does not exists in array $daysarr3"."<br />";
}
?>

O/P:


"b" Key Exists in array $daysarr1
"c" Key does not exists in array $daysarr2
0 Key Exists in array $daysarr3