Tag Archives: php function

array_diff_key

(PHP 5 >= 5.1.0RC1)

array_diff_key – Computes the difference of arrays using keys for comparison

Description

array array_diff_key ( array array1, array array2 [, array ...] )
array_diff_key() returns an array containing all the values of array1 that have keys that are not present in any of the other arguments. Note that the associativity is preserved. This function is like array_diff() except the comparison is done on the keys instead of the values.

Example 1. array_diff_key() example

<?php
$array1
= array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);

var_dump(array_diff_key($array1, $array2));
?>

The above example will output:

array(2) {
  ["red"]=>
  int(2)
  ["purple"]=>
  int(4)
}

The two keys from the key => value pairs are considered equal only if (string) $key1 === (string) $key2 . In other words a strict type check is executed so the string representation must be the same.

Note: Please note that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff_key($array1[0], $array2[0]);.

source: http://ph.php.net/array_diff





string to constant

constant (PHP 4 >= 4.0.4, PHP 5) constant – Returns the value of a constant Description mixed constant ( string name )constant() will return the value of the constant indicated by name. constant() is useful if you need to retrieve the value of a constant, but do not know its name. I.e. it is stored in a variable or returned by a function. This function works also with class constants.





Example: define(‘MYCONSTANT’, ‘HELLO’);

echo constant(‘MYCONSTANT’);
HELLO // result

source:http://www.php.net/constant

preg_match case insensitive


preg_match case insensitive
<?php
// The "i" after the pattern delimiter indicates a case-insensitive search
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}
?>

file_get_contents vs fread

The main difference between a complete file read using fread and
file_get_contents is that file_get_contents may use mmap (if your OS supports
it) to use file memory mapping, which can eliminate a little of the copying
and/or allocation of memory in getting it from the file to PHP’s memory space

Whether it makes any difference to your system depends on your system and you
should benchmark it in relation to the rest of your script before worrying too
much about it.

Always beware of premature optimisation – you probably have larger things to
worry about first – file_get_contents() may be a slightly faster way to read
the whole file into memory, but it’s possible that it may be more efficient to
read the file in chunks and process those smaller chunks – everything depends
on context.

For reference, here are some timings from a trivial pair of scripts against a
70kB file:

Rate fread file_get_contents
fread 1562/s — -9%
file_get_contents 1724/s 10% –
source: http://bytes.com/forum/thread560079.html

list alphabet using php

I was looking for ways on how to list the alphabet without manually writing each one of them…
I guess I found an answer here http://xqus.com/printing-alphabet-php.

There were actually different methods being suggested. One that came from the author is by using range(). It can also be used with numbers.

range

(PHP 4, PHP 5)

range — Create an array containing a range of elements

Sample below:

Letters

<?php
foreach(range('a', 'z') as $letter) {
    echo $letter;
}
?>

Numbers

<?php
foreach(range(0, 12) as $number) {
    echo $number;
}
?>

<?php
foreach(range(0, 100, 10) as $number) {
    echo $number;
}

And some suggested these:
array_walk(range('a', 'z'), "printf");
For UPPERCASE: for($i=65; $i<91; $i++) { echo chr($i); }

For lowercase: for($i=97; $i<123; $i++) { echo chr($i); }

I haven’t tried any methods yet but I guess I’ll be trying out range() instead :)