php_basic_facts

  1. There is no integer division operator in PHP. 1/2 yields the float 0.5. The value can be casted to an integer to round it downwards, or the round() function provides finer control over rounding.
  2. Assuming 2 compliment when you negate a positive number, the result is the negative of that number plus 1
    ex.
    $b = ~6;
    // $b will be equal to -7
    $b = ~0;
    // $b will be equal to -1

    Assuming 2's compliment when you negate a negative number, the result is the positive of that number subtract 1
    $b = ~ -4;
    // $b will be equal to 3
    $b = ~ -1;
    // $b will be equal to 0
  3. 1) $a & $b //and
    2) $a | $b //or (inclusive or)
    3) $a ^ $b //Xor (exclusive or)
    4) ~ $a //not
    $a << $b //shift left
    $a >> $b //shift right


    1) Set bits that are set in both $a and $b are set.
    2) Set bits that are set in either $a or $b are set.
    3) Set bits that are set in $a or $b but not both are set.
    4) Shift the bits of $a $b steps to the left (each step means "multiply by two")
    5) Shift the bits of $a $b steps to the right (each step means "divide by two")
  4. When setting a cookie on a page that redirects, the cookie must be set after the call to header('Location: ....');

    Such as:
    <?php
    header('Location: http://www.example.com/');
    setcookie('asite', $site, time()+60*60, '/', 'site.com');
    ?>
  5. Associativity Operator
    right [
    right ! ~ ++ — (int) (float)
    (string) (array) (object) @
    left * / %
    left << >>
    non-associative < <= > >=
    non-associative == != === !==
    left &
    left ^
    left |
    left &&
    left ||
    left ?:
    right = += -= *= /= .= %= &= |= ^=
    <<= >>=
    left and
    left xor
    left or
    left ,
  6. ++ and -- need to operate directly on a variable

    The following will result in a parse error

    $a = 4;
    print ++$a++; // cant use 2 increment/decrement operators on same variable
    print (++$a)++; // still illegal
    print ~++$a; // legal
    print ~(++$a); // legal
    print !++$a; // legal
  7. Although = has a lower precedence than most other operators, PHP will still allow expressions similar to the following: if (!$a = foo()), in which case the return value of foo() is put into $a.
  8. The division operator ("/") returns a float value unless the two operands are integers (or strings that get converted to integers) and the numbers are evenly divisible, in which case an integer value will be returned.

    Operands of modulus are converted to integers (by stripping the decimal part) before processing.
  9. If the track_errors feature is enabled, any error message generated by the expression will be saved in the variable $php_errormsg. This variable will be overwritten on each error, so check early if you want to use it.
  10. The @-operator works only on expressions. A simple rule of thumb is: if you can take the value of something, you can prepend the @ operator to it. For instance, you can prepend it to variables, function and include() calls, constants, and so forth. You cannot prepend it to function or class definitions, or conditional structures such as if and foreach, and so forth.

    so what does the @ operator do? It temporarily sets the error reporting level to 0 for that line. If that line triggers an error, the error handler will still be called, but it will be called with an error level of 0
  11. @include("file.php");

    This causes, the error reporting level to be set to zero also for the included file. So if there are some errors in the included file, they will be not displayed.
  12. PHP supports one execution operator: backticks (``). Note that these are not single-quotes! PHP will attempt to execute the contents of the backticks as a shell command; the output will be returned (i.e., it won't simply be dumped to output; it can be
    assigned to a variable). Use of the backtick operator is identical to shell_exec().

    <?php $output = `ls -al`;echo "<pre>$output</pre>";?>

    The backtick operator is disabled when safe mode is enabled or shell_exec() is disabled.
  13. instanceof is used to determine whether a PHP variable is an instantiated object of a certain class:
    <?php
    class MyClass{}; class NotMyClass{};
    $a = new MyClass;
    var_dump($a instanceof MyClass); //true
    var_dump($a instanceof NotMyClass); //false ?>

    instanceof can also be used to determine whether a variable is an instantiated object of a class that inherits from a parent class:
    <?php
    class ParentClass{};
    class MyClass extends ParentClass{};
    $a = new MyClass;
    var_dump($a instanceof MyClass); //true
    var_dump($a instanceof ParentClass); //true ?>

    Lastly, instanceof can also be used to determine whether a variable is an instantiated object of a class that implements an interface:
    <?php
    interface MyInterface{}
    class MyClass implements MyInterface{}
    $a = new MyClass;
    var_dump($a instanceof MyClass); //true
    var_dump($a instanceof MyInterface); //true?>

    Although instanceof is usually used with a literal classname, it can also be used with another object or a string variable:
    $a = new MyClass;
    $b = new MyClass;
    $c = 'MyClass';
    $d = 'NotMyClass';
    var_dump($a instanceof $b); // $b is an object of class
    MyClassvar_dump($a instanceof $c); // $c is a string
    'MyClass'var_dump($a instanceof $d); // $d is a string 'NotMyClass'?>
  14. you cannot break an if else statement between } else {
    ex:
    incorrect:
    <?php if(true){
    }?>
    <?php else{
    }?>
    correct:
    <?php if(true){ ?>
    <?php
    }php else{
    }?>
  15. Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your if/elseif conditions, you must not separate else if into two words, or PHP will fail with a parse error.
  16. Php in, a continue statement in a switch statement has the same effect has a break statement.
    However the continue statement only seem to affect the switch.
    for example 'continue 2' would only affect the switch and not any outer loop
  17. Continue; inside a switch statement acts similar to a break statement. If the switch is inside a loop you can use continue 2; It will break out of the switch statement and continue with the next iteration of the outer loop.
  18. the return() statement immediately ends execution of the current function, and returns its argument as the value of the function call. return() will also end the execution of an eval() statement or script file.

    If no parameter is supplied, then the parentheses must be omitted and NULL will be returned. Calling return() with parentheses but with no arguments will result in a parse error.

    If called from the global scope, then execution of the current script file is ended. If the current script file was include()ed or require()ed, then control is passed back to the calling file. Furthermore, if the current script file was include()ed, then the value given to return() will be returned as the value of the include() call. If return() is called from within the main script file, then script execution ends. If the current script file was named by the auto_prepend_file or auto_append_file configuration options in php.ini, then that script file's execution is ended.

    Note:
    You should never use parentheses around your return variable when returning by reference, as this will not work. You canonly return variables by reference, not the result of a statement. If you use return ($a); then you're not returning a variable, but the result of the expression ($a) (which is, of course, the value of $a).
Author
starkisspk
ID
9590
Card Set
php_basic_facts
Description
Basic PHP FACTS
Updated