Recent Notes
Displaying keyword search results 1 - 10
Created by jinx on May 18, 2011 20:09:05
Last update: May 18, 2011 20:09:05
The PHP function file_get_contents reads the entire contents of a file into a string, while the function file reads a file into an array.
file_get_contents('filename') is equivalent to implode('', file('filename')) :
<?php
// reads entire file into array, one elem...
Created by jinx on May 11, 2011 20:38:20
Last update: May 11, 2011 20:38:40
You can check the version of PHP by the function phpversion function, or the PHP_VERSION constant.
<?php
// PHP version
echo phpversion(), "\n"...
The function phpinfo prints extensive info about PHP.
<?php
echo phpinfo();
?>
Created by jinx on May 04, 2011 20:01:08
Last update: May 04, 2011 20:02:59
A static variable in PHP functions and methods keeps its value after the function/method scope is exited, just like static variables in C. A static variable in a class is a class variable, and is accessed by the class name. In the global scope, static has no special meaning.
test.php:
<?php
include("test.inc");
include("...
test.inc:
<?php
static $i = 0;
$i++;
echo __FILE__,...
Output:
E:\phpwork\test.inc included: 1
E:\phpwork\test...
Created by jinx on May 04, 2011 19:39:44
Last update: May 04, 2011 19:42:51
The function include includes the specified file multiple times, while include_once only includes the file once.
This is a test:
Create file test.php with contents:
<?php
for ($i = 0; $i < 5; $i++) {
inclu...
Create file test.inc with contents:
<?php
echo __FILE__, " included: $i\n";
?>
...
Run test.php , the output is:
E:\phpwork\test.inc included: 0
E:\phpwork\test...
Change include to include_once , only one line is printed:
E:\phpwork\test.inc included: 0
You must use include_once to avoid duplicate includes in the case A includes B and C , but B and C both includes D .
The relationship between require and require_once is the same.
Created by jinx on May 03, 2011 12:11:35
Last update: May 03, 2011 12:12:27
The PHP function is_resource returns TRUE if a variable is a resource.
This may come in handy when a function you are calling returns mixed type. For example mysql_query returns TRUE or FALSE for INSERT , UPDATE , DELETE etc., but returns resource or FALSE for SELECT , SHOW , or DESCRIBE .
<?php
$rows = array();
if ($res = mysql_quer...
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 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 May 02, 2011 12:14:23
Last update: May 02, 2011 12:15:07
The PHP function ctype_digit returns true when all characters in a string is numeric. Use this function only on strings. You get unexpected results with other data types.
Example:
<?php
$a = array("124.34", "00088760", "1.3e5",...
Results:
string(6) "124.34"
-----------------
ctype_d...
Created by jinx on May 02, 2011 11:55:53
Last update: May 02, 2011 11:57:20
The PHP function intval returns the integer value of a variable. But there are some gotchas:
The string ' 0x10 ' may look like hex, but it's not recognized as hex by intval unless base 0 is entered as the second argument.
With floating point numbers, round-off errors may produce surprising results (actually, intval truncates, it doesn't round off).
The number base argument is effective only when the variable is a string.
Examples:
<?php
echo 19.99 * 100, "\n"; // 1999
echo i...