Tag Archives: php

PHP CLI vs PHP CGI

In many ways, the CLI SAPI works in the same way as the CGI SAPI and other SAPIs. However, it is important to keep track of where it differs and why, because it will directly affect how you write your scripts.
These are the most important differncies between CLI and CGI:

* Unlike the CGI SAPI, CLI writes no headers to the output by default
* There are some php.ini directives which are overridden by the CLI SAPI because they do not make sense in shell environments:

  • html_errors: CLI default is FALSE
  • implicit_flush: CLI default is TRUE
  • max_execution_time: CLI default is 0 (unlimited)
  • register_argc_argv: CLI default is TRUE

* You can have command line arguments with your script! Variable “$argc” provides you with a number of arguments passed to the application. And array “$argv” gives you an array of the actual arguments
* There are 3 new constant defined for the shell enviroment: STDIN, STDOUT, STDERR. All of them are file handlers for correspondent shell devicies. For example STDIN is the handler for fopen(‘php://stdin’, ‘r’) . So, you can read a line from STDIN like this: $strLine = trim(fgets(STDIN));. STDIN is already defined for you by PHP CLI!
* PHP CLI does not change the current directory to the directory of the executed script. The current directory for the script would be the directory where your type PHP CLI command.
* There are number of USEFUL options avaiable for PHP CLI. Which will allow you to get some valuable information about you php setup, your php script or run it in defferent modes.
* In PHP 5 there were some changes in CLI and CGI filenames. In PHP 5, the CGI version was renamed to php-cgi.exe (previously php.exe) and the CLI version now sits in the main directory (previously cli/php.exe).
* In PHP 5 it was also introduced a new mode: php-win.exe. This is equal to the CLI version, except that php-win doesn’t output anything and thus provides no console (no “dos box” appears on the screen). This behavior is similar to PHP GTK.

More information: http://www.php-cli.com/

Cấu hình php.ini trong Windows

Sau khi cài đặt Apache, download PHP (giải nén), tạo file info.php trong APACHE_DIR/htdocs/

<?php
phpinfo();
?>

Để chạy được http://localhost/info.php cần thêm vào APACHE_DIR/conf/httpd.conf hai dòng dạng như  sau(tùy thuộc vào phiên bản sử dụng)

LoadModule php5_module C:/php/php5apache2_2.dll
AddType application/x-httpd-php .php

Restart Apache là ta đã load được trang info.php trên trình duyệt. Để xác định đường dẫn tuyệt đối cho file php.ini mà Apache sẽ load khi chạy PHP, ta phải thêm vào một dòng nữa

PHPIniDir C:/php/

* Để chạy MySQL, phải copy libmysql.dll vào thư mục bin của Apache
* Tham khảo thêm tại http://vn.php.net/install.windows.manual

Liệt kê các bản ghi trong BDB với PHP

Khi sử dụng cơ sở dữ liệu Berkeley (BDB), bạn có dùng PHP để làm việc. Dưới đây là ví dụ code mẫu liệt kê toàn bộ các bản ghi.

<?php
$db4 = new Db4();
$db4->open(null, ‘/var/tmp/aaa.db’, ‘mydata’);

$db4->put(“hjx”, “hjx”);
$db4->put(“hjx”, “hjx4”);
$db4->put(“2”, “hjx”);

$dbc = $db4->cursor();

$key = null;
$data = null;

while($dbc->get($key, $data, DB_NEXT)==0) {
print $key.’ – ‘.$data.'<br/>’;
}

$dbc->close();
$db4->close();
?>

Từ khóa: Berkeley database, PHP, API, DB_NEXT, cursor

isset vs empty in PHP

It might be confusing as far as I red on many forums that fails to explain the diffrence between isset() and empty().
So let me put it this way :

isset() checks if a variable has a value including ( Flase , 0 , or Empty string) , But not NULL.
Returns TRUE if var exists; FALSE otherwise.

On the other hand the empty() function checks if the variable has an empty value empty string , 0, NULL ,or False. Returns FALSE if var has a non-empty and non-zero value.

Comparison example on both isset() and empty()

<?php
$var = 0;

// Evaluates to true because $var is empty
if (empty($var)) {
echo ‘$var is either 0, empty, or not set at all’;
}

// Evaluates as true because $var is set
if (isset($var)) {
echo ‘$var is set even though it is empty’;
}
?>