empty null is_set

The main difference between an unset variable and a null variable is that the unset variable doesn't exist in the current scope, whereas the null variable is a defined variable with no value (because null is essentially the i-have-no-value value).
- The isset() function is in that way confusing because it not only checks whether the variable is defined in the current scope, but also if it has a value which is not null.
- The is_null() function is indeed the best way to determine if a variable has a null value, or, if you must, the === operator.
- The empty() function is not a good way, because it will also return true for empty strings and a zero.


String: A string is a sequence of characters, like "hello" or "abracadabra". String values may be enclosed in either double quotes ("") or single quotes(''). (Quotation marks within the string itself can be "escaped" with a backslash (\) character.) String values enclosed in double quotes are automatically parsed for special characters and variable names; if these are found, they are replaced with the appropriate value.


array functions:

array index
$arr = array(5 => 1, 12 => 2);
$arr[] = 56;    // This is the same as $arr[13] = 56; at this point of the script
// This:
$a = array( 'color' => 'red',
            'taste' => 'sweet',
            'shape' => 'round',
            'name'  => 'apple',
            4        // key will be 0
$b = array('a', 'b', 'c');
// . . .is completely equivalent with this:
$a = array();
$a['color'] = 'red';
$a['taste'] = 'sweet';
$a['shape'] = 'round';
$a['name']  = 'apple';
$a[]        = 4;        // key will be 0
$b = array();
$b[] = 'a';
$b[] = 'b';
$b[] = 'c';
// After the above code is executed, $a will be the array
// array('color' => 'red', 'taste' => 'sweet', 'shape' => 'round', 
// 'name' => 'apple', 0 => 4), and $b will be the array 
// array(0 => 'a', 1 => 'b', 2 => 'c'), or simply array('a', 'b', 'c').
$firstquarter  = array(1 => 'January', 'February', 'March');
/* that is 
    [1] => 'January'
    [2] => 'February'
    [3] => 'March'

if no key is specified, the maximum of the existing integer indices is taken, and the new key will be that maximum value plus 1. If no integer indices exist yet, the key will be 0 (zero).Note that the maximum integer key used for this need not currently exist in the array. It need only have existed in the array at some time since the last time the array was re-indexed. for example array_values[…]

how to get a index of array list

$pages = array(
    'intro' => array(
      'title' => t('Introduction'),
      'description' => t('Select the ....'),
    'type' => array(
      'title' => t('Select content type'),
      'description' => t('Select the ...'),
    'file' => array(
      'title' => t('Select file'),

so we can do this :

$page_names = array_keys($pages);
  $first_page_name = $page_keys[0];
  $last_page_name = $page_keys[count($page_keys) - 1];
  $pages_total = count($pages);
  $index = array_search('file', $page_names);

variable scope

To have variables within a function accessible from outside it (and vice-versa), all you need to do is first declare them with the global keyword.

  • $GLOBALS is a type of superglobals

$a = 1;
$b = 2;
function Sum()
    $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
echo $b;

The $GLOBALS array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element.


1)When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function

2)Please also note that although you can call a function that is DEFINED later in the code, you cannot call a function that is defined in a file which is INCLUDED later.

3)When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.

inlcude path


<?php include($folder.DIRECTORY_SEPARATOR.$file); ?>
  • getting the proper include path relative to the current file, rather than the working directory is:
include realpath(dirname(__FILE__) . "/" . "relative_path");
  • root directory

A very EASY way to get 'include' to find its way to another directory, other than setting the 'include path', and useful for fetching one or two files:

<?php include ($_SERVER['DOCUMENT_ROOT']."/foo/bar.php"); ?>

This creates an include that is relative to the root rather than the current directory.

  • change include path

you need to have permissions to change the server's include path.

$BASE_PATH = dirname(__FILE__);
$DEPENDS_PATH .= ";".$BASE_PATH."/lib";
$DEPENDS_PATH .= ";".$BASE_PATH."/test";
ini_set("include_path", ini_get("include_path").";".$DEPENDS_PATH);
  • combination

<?php set_include_path( get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] ); ?>

  • special dealing

When I'm dealing with a package that uses relative includes of its own, rather than modify all of their includes, I found it was easier to change PHP's working directory before and after the include, like so:

$wd_was = getcwd();

measure performance

 $start_time = microtime(true);
 $end_time = microtime(true);

error handling

//turn off warnings
error_reporting(E_ALL & ~E_WARNING);

command line mode

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License