HOBALL 筆記簿

April 1, 2010

PHP: Run two foreach loops at the same time

Filed under: PHP — Tags: , , , — hoball @ 11:25 am

@reference: http://stackoverflow.com/questions/2556998/how-do-you-combine-two-foreach-loops-into-one

SPL MultipleIterator
@reference: http://docs.php.net/class.multipleiterator

// ArrayIterator is just an example, could be any Iterator.
$a1 = new ArrayIterator(array(1, 2, 3, 4, 5, 6));
$a2 = new ArrayIterator(array(11, 12, 13, 14, 15, 16));

$it = new MultipleIterator;
$it->attachIterator($a1);
$it->attachIterator($a2);

foreach($it as $e) {
  echo $e[0], ' | ', $e[1], "\n";
}

Prints

1 | 11
2 | 12
3 | 13
4 | 14
5 | 15
6 | 16

March 12, 2010

ip2long vs INET_ATON

Filed under: PHP, SQL — Tags: , , , — hoball @ 9:15 pm

Source: http://mwillis.co.uk/mysql/ip2long-vs-inet_aton/

PHP ip2long() sometimes return negative integers,
while MySQL INET_ATON() function returns only positive numbers.

Ensure PHP ip2long() returns only positive intergers:

sprintf("%u", ip2long("254.254.254.254"));
$ip = ip2long($ip_address);
if ($ip < 0){ $ip += 4294967296; }

October 23, 2009

PHP, Connection Timeout for XMLReader->open()

Filed under: PHP — Tags: , , — hoball @ 3:23 pm

All XML extensions use the PHP streams.

To set a connection timeout, we can either edit the php.ini, or use ini_set() to configure it on runtime.

if ( (int) ini_get('default_socket_timeout') > 5)
{
    ini_set('default_socket_timeout',5);
}

http://bugs.php.net/bug.php?id=45785

September 29, 2009

HTML Tidy on linux under XAMPP

Filed under: PHP — Tags: , , , — hoball @ 11:53 pm

Installing tidy on PHP5 under linux is pain. The manual says  “compile with –with-tidy”, but it is not really happening.

Finally I made it work on my fedora (using XAMPP) by this method:

# Install libtidy
yum -y install tidy

# Download PHP 5 source
http://www.php.net/downloads.php

# Unpack the source
tar xvzf php-5.3.0.tar.gz

# Configure tidy for installed php5 API
cd php-5.3.0/ext/tidy
/opt/lampp/bin/phpize

# Stay at the same folder, Configure & Compile the source
./configure --with-php-config=/opt/lampp/bin/php-config --with-tidy
make clean    <-- Without this the compile builds a bad module for some reason
make
make install

# Install module into php.ini - Add:: extension="tidy.so"
vi /opt/lampp/etc/php.ini

References:

ubuntuforums.org: LAMP: php5-tidy doesn’t exist!

newsn.net 给AndLinux下面的xampp装tidy模块的经验 (Chinese)

/opt/lampp/etc/php.i

July 24, 2009

For-loop with two counting variables

Filed under: PHP — Tags: , — hoball @ 2:38 pm

Perma Link: http://stackoverflow.com/questions/1172553/php-for-loop-with-2-variables/1172565#1172565

for ($i=0, $k=10; $i<=10 ; $i++, $k--) {
    echo "Var " . $i . " is " . $k . "<br>";
}

Gumbo, Author of this code snippet:
The two variables $i and $k are initialized with 0 and 10 respectively. 
At the end of each each loop $i will be incremented by one ($i++) and $k decremented by one ($k—).
So $i will have the values 0, 1, …, 10 and $k the values 10, 9, …, 0.

December 31, 2008

PHP Multi-Threads / Multi-Processes

Filed under: PHP — Tags: , , , , , — hoball @ 4:51 pm

By the use of curl_multi_init() (PHP5) or stream_select() (PHP5) or  pcntl_fork (PHP4+), we can stimulate multi-thread/multi-process environment on PHP

External Links:

using stream_select: Develop multitasking applications with PHP V5

using curl_multi_init: PHP curl_multi example of parallel GET requests

using pcntl_fork (chinese): 多采多姿的程式筆記#1, #2

forum quote(chinese): EcStart PHP 技術討論論壇

PHP是與host環境相關的腳本語言,通常狀況下不會使用thread的,有的話也是host的問題,與php沒有關係。(你用的話,會有同步等等問題,甚至會有可能造成host運作不穩定)

但是如果把php當作shell script等等獨立的語言工具來用的話,是可以模擬的。

php本身應該沒有提供thread的函數,但是有multi-process的吧。

請用google找找。

我在酷學園有看過一些討論:
http://phorum.study-area.org/index.php/topic,44942.0.html
http://phorum.study-area.org/index.php/topic,47238.0.html(這篇有提到一點同步機制)

另外,參考一下這一篇:
http://www.ibm.com/developerworks/web/library/os-php-multitask/?ca=dgr-lnxw06PHP5multitasking
(用php5模擬多工)

December 7, 2008

PHP Exceptions

Filed under: PHP — Tags: , , , — hoball @ 10:02 pm

Part 1: http://i-novice.com/php-exceptions-part-1/

Part 2: http://i-novice.com/php-exceptions-part-2/

PHP variables, Memory Handling

Filed under: PHP — Tags: , , , , , — hoball @ 9:51 pm

Adam Charnock: The Truth About PHP Variables

Sara Golemon: How to Manage Memory in PHP

December 3, 2008

SQL: Insert data to multiple tables?

Filed under: PHP, SQL — Tags: , , , , , — hoball @ 2:16 pm

By the help of

mysqli_insert_id() in MySQLi extension

or

PDO->lastInsertId() in PDO

Reference: http://www.desilva.biz/mysql/insertid.html

October 31, 2008

PHP *_once vs require/include

Filed under: PHP — Tags: , , , , — hoball @ 4:08 pm

There are criticisms that require_once/include_once are slow. To speed up them, I find this solution from http://www.php.net/require_once

antoine dot pouch at mcgill dot ca
10-Mar-2006 11:13

require_once (and include_once for that matters) is slow.
Furthermore, if you plan on using unit tests and mock objects (i.e. including mock classes before the real ones are included in the class you want to test), it will not work as require() loads a file and not a class.

To bypass that, and gain speed, I use :

<?php
class_exists(‘myClass’) || require(‘path/to/myClass.class.php’);
?>

I tried to time 100 require_once on the same file and it took the script 0.0026 seconds to run, whereas with my method it took only 0.00054 seconds. 4 times faster ! OK, my method of testing is quite empirical and YMMV but the bonus is the ability to use mock objects in your unit tests.

Reference: http://www.php.net/manual/en/language.operators.logical.php

Older Posts »

Blog at WordPress.com.