Incrementing / Decrementing Operators

++ increment operator

-- decrement operator

Example    Name              Effect
++$a       Pre-increment     Increments $a by one, then returns $a.
$a++       Post-increment    Returns $a, then increments $a by one.
--$a       Pre-decrement     Decrements $a by one, then returns $a.
$a--       Post-decrement    Returns $a, then decrements $a by one.

These can go before or after the variable.

If put before the variable, the increment/decrement operation is done to the variable first then the result is returned. If put after the variable, the variable is first returned, then the increment/decrement operation is done.

$apples = 10;
for ($i = 0; $i < 10; ++$i) {
    echo 'I have ' . $apples-- . " apples. I just ate one.\n";

In the case above ++$i is used, since it is faster. $i++ would have the same results.

Pre-increment is a little bit faster because it really increments the variable and after that ‘returns’ the result. Post-increment creates a special variable, copies there the value of the first variable and only after the first variable is used, replaces its value with second’s.

However, you must use $apples--, since first, you want to display the current number of apples, and then you want to subtract one from it.

You can also increment letters in PHP:

$i = "a";
while ($i < "c") {
    echo $i++;

Once z is reached aa is next, and so on.

Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.


  • Compatible OS: CentOS 5.8+ (Tested), And many other Linux Distros.

  • Space:Capacity of minimum 1.5 GB Hard Disk space.

  • Memory: 1GB RAM.

Undefined variable

From the vast wisdom of the PHP Manual:

Relying on the default value of an uninitialized variable is problematic in the case of including one file into another which uses the same variable name. It is also a major security risk with register_globals turned on. E_NOTICE level error is issued in case of working with uninitialized variables, however not in the case of appending elements to the uninitialized array. isset() language construct can be used to detect if a variable has been already initialized. Additionally and more ideal is the solution of empty() since it does not generate a warning or error message if the variable is not initialized.

From PHP documentation:

$o = [];
@$var = ["",0,null,1,2,3,$foo,$o['myIndex']];
array_walk($var, function($v) {
    echo (!isset($v) || $v == false) ? 'true ' : 'false';
    echo ' ' . (empty($v) ? 'true' : 'false');
    echo "\n";

Test the above snippet in the 3v4l.org online PHP editor

Although PHP does not require a variable declaration, it does recommend it in order to avoid some security vulnerabilities or bugs where one would forget to give a value to a variable that will be used later in the script. What PHP does in the case of undeclared variables is issue a very low level error, E_NOTICE, one that is not even reported by default, but the Manual advises to allow during development.

Ways to deal with the issue:

  1. Recommended: Declare your variables, for example when you try to append a string to an undefined variable. Or use isset() / !empty() to check if they are declared before referencing them, as in:

    //Initializing variable
    $value = ""; //Initialization value; Examples
                 //"" When you want to append stuff later
                 //0  When you want to add numbers later
    $value = isset($_POST['value']) ? $_POST['value'] : '';
    $value = !empty($_POST['value']) ? $_POST['value'] : '';

    This has become much cleaner as of PHP 7.0, now you can use the null coalesce operator:

    // Null coalesce operator - No need to explicitly initialize the variable.
    $value = $_POST['value'] ?? '';
  2. Set a custom error handler for E_NOTICE and redirect the messages away from the standard output (maybe to a log file):

    set_error_handler('myHandlerForMinorErrors', E_NOTICE | E_STRICT)
  3. Disable E_NOTICE from reporting. A quick way to exclude just E_NOTICE is:

    error_reporting( error_reporting() & ~E_NOTICE )

Note: It’s strongly recommended to implement just point 1.

$_POST / $_GET / $_SESSION variable

The notices above appear often when working with $_POST, $_GET or $_SESSION. For $_POST and $_GET you just have to check if the index exists or not before you use them. For $_SESSION you have to make sure you have the session started with session_start() and that the index also exists.

Also note that all 3 variables are superglobals and are uppercase.


  • Space:Capacity of minimum 1.5 GB Hard Disk space.

  • Memory: 1GB RAM.


  • 400+ Applications by Softaculous

  • Multiple Latest PHP Versions

  • Easy Database Management

  • Update, Backup, Restore, Clone your favorite application

Installing LAMP

  • Step 1 : Download the LAMP stack from here : http://www.ampps.com/download. Download the one under Linux section.

  • Note: Replace with x86 for 32 Bit and x86_64 for 64 Bit.
    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspReplace with current Ampps version.

  • Step 3 : Run the file from the GUI to start the Application.

  • Note: First Run of the AMPPS might take little time as it is setting up environment for you.

  • Step 4 : Click on Start Button below both Apache and MySQL to start the Services.

  • *Note: When you run AMPPS for the first time make sure you Internet connection is active.
    AMPPS doesn’t support proxy yet. So you make have a Direct internet Connection.

Undefined index / Undefined offset

This notice appears when you (or PHP) try to access an undefined index of an array.

Ways to deal with the issue:

  1. Check if the index exists before you access it. For this you can use isset() or array_key_exists():

    $value = isset($array['my_index']) ? $array['my_index'] : '';
    $value = array_key_exists('my_index', $array) ? $array['my_index'] : '';
  2. The language construct list() may generate this when it attempts to access an array index that does not exist:

    list($a, $b) = array(0 => 'a');
    list($one, $two) = explode(',', 'test string');

Two variables are used to access two array elements, however there is only one array element, index 0, so this will generate:

Notice: Undefined offset: 1

LAMP Usage

Installing WAMP

  • Step 1 : Download the WAMP stack from here : http://www.ampps.com/download. Download the one under Windows section.

  • Step 2 : Run the setup to start the installation.

  • Step 3 : Select the path where you want to install the WAMP Stack.

  • Step 4 : Let the installation complete.

  • *Note: Setup might take little more time to start.


  • 400+ Applications by Softaculous

  • Multiple Latest PHP Versions

  • Easy Database Management

  • Update, Backup, Restore, Clone your favorite application

Дополнительно:  Почему ноутбук не видит флешку или карту памяти
Оцените статью
Master Hi-technology
Добавить комментарий