Recent Notes
Displaying keyword search results 1 - 10
Created by jinx on May 16, 2011 20:55:53
Last update: May 16, 2011 20:55:53
The PHP break statement takes an optional level parameter, which indicates how many levels of loop structure to break out.
Example code:
<?php
for ($i = 0; $i < 5; $i++) {
for (...
It's a fatal error if the level parameter exceeds the maximum number of enclosing loop structures. For example, " break 3; " in above code.
Created by jinx on May 03, 2011 11:43:26
Last update: May 03, 2011 11:43:26
The PHP function strval returns the string value of a variable. It does the same thing as casting a variable to string, or automatic conversion where string is expected.
Conversion rules:
Type String Value
Boolean TRUE '1'
Boolean FALSE '' (empty string)
integer of float string representing the number
Array The string 'Array'
Object Result of __toString() if defined, error otherwise
Resource 'Resource id #n' , where n is a number assigned to the resource.
NULL '' (empty string)
Example:
<?php
set_error_handler(function($errno, $errst...
Results:
bool(true)
---------------------
strval: '1'...
Created by jinx on May 03, 2011 09:42:12
Last update: May 03, 2011 09:42:12
The @ operator suppresses error messages.
A usual example is:
<?php
$v = @$a['non-existing-key'];
?>
which suppresses the "undefined index" PHP notice.
Another example is:
<?php
@include('non_existent_file');
?>
In this case, @ not only suppresses error messages for non-existing file, it also suppresses any error messages coming from the included file when it does exist.
Create file test.php :
<?php
@include('test.inc');
?>
Create test.inc :
<?php
<?php
garbage
?>
The "undefined constant" PHP notice message is suppressed by @ .
Created by jinx on May 03, 2011 09:12:05
Last update: May 03, 2011 09:12:19
Both include and require include and evaluate the file specified as argument. The only difference is, when the included file cannot be found, include emits a warning while require emits a fatal error.
Example:
<?php
include('non-existing-file');
require(...
Outputs:
PHP Warning: include(non-existing-file): failed t...
Suppress error with @ :
<?php
@include('non-existing-file');
echo "A...
Outputs:
After include
Created by jinx on May 03, 2011 08:56:55
Last update: May 03, 2011 08:57:54
These error level constants are defined in PHP : Constant Value Description E_ERROR 1 Fatal run-time errors. Execution of the script is halted. E_WARNING 2 (1<<1) Run-time warnings (non-fatal errors). Execution of the script is not halted. E_PARSE 4 (1<<2) Compile-time parse errors. Parse errors should only be generated by the parser. E_NOTICE 8 (1<<3) Notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script. E_CORE_ERROR 16 (1<<4) Fatal errors that occur during PHP's initial startup. This is like an E_ERROR, except it is generated by the core of PHP. E_CORE_WARNING 32 (1<<5) Warnings (non-fatal errors) that occur during PHP's initial startup. This is like an E_WARNING, except it is generated...
Created by jinx on May 02, 2011 20:48:49
Last update: May 02, 2011 20:58:01
A PHP try catch block looks like this:
<?php
try {
throw new MyException('foo!'...
Normally, no exceptions are thrown from internal function errors. For example:
<?php
$a = 1;
$b = 0;
try {
$v = $...
The division by zero error is never caught as an exception.
However , you can translate errors into exceptions with an error handler and ErrorException :
<?php
function exception_error_handler($errno, ...
which prints:
Exception: exception 'ErrorException' with message...
PHP does not support finally clause.
Created by jinx on April 29, 2011 15:39:05
Last update: April 29, 2011 15:39:05
When you call a non-existing function in PHP, it dies immediately with "PHP Fatal error: Call to undefined function". The error handler is not bothered even when you have one in place:
<?php
set_error_handler(function($errno, $e...
It's safer to use call_user_func :
<?php
set_error_handler(function($errno...
Created by jinx on April 29, 2011 15:26:44
Last update: April 29, 2011 15:27:53
The PHP function set_error_handle sets a user defined error handler function. The example below uses a global variable to alter the execution path after error occurs:
<?php
$continue = true;
set_error_ha...
Output:
[Error Handler]
errno: 2
errstr: Division by...
Created by jinx on April 29, 2011 15:03:10
Last update: April 29, 2011 15:04:02
The PHP function is_callable verifies that a variable can be invoked as a function.
Example:
<?php
define('F', 'f');
function...
Output:
var_dump: string(1) "f"
is_callable: 1
Calla...
Created by jinx on April 28, 2011 20:52:45
Last update: April 29, 2011 13:27:31
This is normally a syntax error just before some variable. Try some of these:
Missing operator before a variable:
<?php
$a = 'ab';
echo('Missing conca...
Missing semicolon at end of line:
<?php
echo('Missing semicolon at end of lin...
Missing ( and ) in for loop:
<?php
foreach $a as $i {
echo "$i\n";
...
Missing keyword before class variable:
<?php
class A {
// should be var $a;
...