Monday, March 4, 2013

Difference between foo() and @foo() in php?

@foo() suppresses errors. If foo() triggers an error, warning or a notice, it won't get displayed/logged even if error_reporting is set to display/log them. (Note that errors still cause PHP to stop executing - it just won't display/log anything).

In general, you should NOT be using this kind of error suppression unless you're checking for errors another way - it's not a good idea to keep anyone from knowing about errors that come up. Furthermore, if your code is ALWAYS returning some kind of non-fatal error, you need to fix that, not hide it - also, what happens if an error you DIDN'T expect come up?

If foo() returns FALSE and raises a warning on failure (for example), you could do something like this: (bar() is called only if foo() fails and returns false)

 So simply using a @ sign before a function call will not output errors related to that function

foo() executes the function and any parse/syntax/thrown errors will be displayed on the page.

@foo() will mask any parse/syntax/thrown errors as it executes.
You will commonly find most applications use @mysql_connect() to hide mysql errors or @mysql_query. However, I feel that approach is significantly flawed as you should never hide errors, rather you should manage them accordingly and if you can, fix them.
<code>
@foo() or bar();
</code>

or

<code>
if ( @foo() === FALSE ) {
// foo() failed! do something here
}
</code>

What is difference between array_merge and array_combine? in php

What is array_merge and array_combine?

array_merge — Merge one or more arrays
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
If only one array is given and the array is numerically indexed, the keys get reindexed in a continuous way.
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
Array
(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)
array_combine — Creates an array by using one array for keys and another for its values