Jaslabs: High performance Software

High Performance Software

Archive for March, 2006

What would you like to see in PHP 6?

By Justin Silverton

In November of 2005, the major developers working on the core of PHP met up in Paris and discussed various additions that would possibly make version 6.

The attendees

Marcus Börger (SOMABO)
Wez Furlong (OmniTI)
Rasmus Lerdorf (Yahoo!)
Derick Rethans (eZ systems)
Dmitry Stogov (Zend)
Zeev Suraski (Zend)
Jani Taskinen
Andrei Zmievski (Yahoo!)

The following are a list of the top 5 additions I think should be made (in no particular order):

1) Filename Encoding

issue: Files on a file system can have names encoded in different character sets. For example Windows can make use of it’s UTF16 based filename API, while on Linux it simply depends on the application

2) optimizing []

issue: Currently using the [] operator to select an arbitrary character is very slow as PHP needs to start scanning the string from the start.

3) register_globals

issue: This causes many security issues and should be removed completly. I still encouter PHP scripts that make use of the this feature.

4) 64-bit integer

issue: With 64-bit processing in the horizen, this will come in handy for future (and some present) apps.

5) built-in opcode cache

issue: When an opcode caching system is installed (such as zend or ion cube), script execution time is much better.

 

A list of everything discussed can be found Here

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati
2 comments

patTemplate and php

by Justin Silverton

What is patTemplate?

patTemplate is a set of classes that allow you to separate your php code from design/output, making your code easier to maintain. To identify a certain part of the page as a template, patTemplate uses XML tags to assign a template a unique name and a various attributes (see patTemplate Tags and attributes for a list of all tags and attributes). When parsing a template, the parser divides the page in several chunks and treats them as separate templates. By using patTemplate’s API you can hide, display or repeat a certain template.

Template Types

OddEven
you can assign to subtemplates () to this type: Odd and Even, which will be alternated, when the template is repeated

Condition
you can assign as many subtemplate as you like and you have to assign one variable which will be used to compare it with the specified conditions of the subtemplates. This allows you to define different HTML code for each value your condition variable may have. There are two special conditions you can use: default, which is similar to the default in a switch/case statement and empty which will be displayed, when there is no value assigned to the condition variable.

SimpleCondition
A situation that often arises is that an error message has to be displayed if an error occured. Normally you would set the error message if it exists or hide the template using setAttribute() if there is none. By using a template of type SimpleCondition you can assign a list of variables (using the requiredvars) that have to be set if the template has to be displayed. If one of these variables is not set, the template will not be visible.

Variables

patTemplate supports variables similar to programming languages. There can be local variables (assigned by addVar) and global variables (assigned by addGlobalVar).

Global variables can only be scalar variables (strings, integers,…) and local variables can also be arrays. If you assign an array to a variable, the template where you assigned the variable will be repeated.

If a condition template uses the useglobals=”yes” attribute global variables will be used if no local variables are set.

Variables have to be written in uppercase and may only contain chars, numbers or the underscore (_). Variables are always enclosed in {}.

Variables will be replaced with their values when parsing a template.

There is one predefined variable you can use when you want to enumerate a template that is automatically repeated: just place the variable PAT_ROW_VAR in your template, where you’d like to enumeration to appear (can also be used for javascript mouseovers).

How to install

There are two ways to install phptemplate

1) through pear

Download the pear package and execute: ” pear install patTemplate-3.0.1.tgz”

2) Manually

A) Download the .zip or .tgz manual package from the patTemplate website
B) Download the patError class

Example of usage

Example of usage

error_reporting( E_ALL );

//require the error reporting class
require_once( ‘pat/patErrorManager.php’ );
//require the main template class
require_once ‘../patTemplate.php’;
//create a new instance
$tmpl = &new patTemplate();
//set the directory of where to find the templates
$tmpl-&gtsetRoot( ‘templates’ );
//use this file for your template
$tmpl-&gtreadTemplatesFromInput( ‘example_api_addvar.tmpl’ );
//add a variable for displaying
$tmpl-&gtaddVar( ‘template1′, ‘argh’, array( ‘one’, ‘two’, ‘repeat it.’ ) );
//actually display it
$tmpl-&gtdisplayParsedTemplate();

Why use this template system?

There are many template systems available for PHP. I have recently started using this one over the others because it is simple, has less overhead, and is much easier for a person designing your pages to read and understand.

Download

patTemplate can be downloaded Here

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati
No comments

using sqlite and php

by Justin Silverton

What is SQLite?

SQLite is a small library that implements a self-contained, embeddable, zero-configuration SQL database engine. Features include:

  • Transactions are atomic, consistent, isolated, and durable (ACID) even after system crashes and power failures.
  • Zero-configuration - no setup or administration needed.
  • Implements most of SQL92. (Features not supported)
  • A complete database is stored in a single disk file.
  • Database files can be freely shared between machines with different byte orders.
  • Supports databases up to 2 terabytes (241 bytes) in size.
  • Sizes of strings and BLOBs limited only by available memory.
  • Small code footprint: less than 250KiB fully configured or less than 150KiB with optional features omitted.
  • Faster than popular client/server database engines for most common operations.
  • Simple, easy to use API.
  • TCL bindings included. Bindings for many other languages available separately.
  • Well-commented source code with over 95% test coverage.
  • Self-contained: no external dependencies.
  • Sources are in the public domain. Use for any purpose.

Why would I need this?

If you want to have the advantages of a SQL database, without having to install a separate system such as mysql or postgres. It has also been demonstrated to work on websites getting more than 1,000,000 hits per day.

lots of good performance info can be found here
project page can be found here

Installing

Read the INSTALL file, which comes with the package. Or just use the PEAR installer with pear install sqlite. SQLite itself is already included, You do not need to install any additional software.

Windows users may download the DLL version of the SQLite extension here: (php_sqlite.dll).

In PHP 5, the SQLite extension and the engine itself are bundled and compiled by default. However, since PHP 5.1.0 you need to manually activate the extension in php.ini (because it is now bundled as shared). Moreover, since PHP 5.1.0 SQLite depends on PDO it must be enabled too, by adding the following lines to php.ini (in order):

extension=php_pdo.dll
extension=php_sqlite.dll 
On Linux or Unix operating systems, if you build PDO as a shared extension, you must build SQLite as a shared extension using the –with-sqlite=shared configure option. 

SQLite 3 is supported through PDO SQLite.

Windows installation for unprivileged accounts: On Windows operating systems, unprivileged accounts don’t have the TMP environment variable set by default. This will make sqlite create temporary files in the windows directory, which is not desirable. So, you should set the TMP environment variable for the web server or the user account the web server is running under. If Apache is your web server, you can accomplish this via a SetEnv directive in your httpd.conf file. For example:

SetEnv TMP c:/temp
If you are unable to establish this setting at the server level, you can implement the setting in your script:

putenv(’TMP=C:/temp’);
The setting must refer to a directory that the web server has permission to create files in and subsequently write to and delete the files it created. Otherwise, you may receive the following error message: malformed database schema - unable to open a temporary database file for storing temporary tables

Using SQlite

The following is a simple example that can get you started with SQLite:

&lt?php
// create new database
$db = sqlite_open(”db.sqlite”);

//create a table named test
// sqlite_query($db , “CREATE TABLE test (id INTEGER PRIMARY KEY, name CHAR(128))”);

// insert sample data
sqlite_query($db, “INSERT INTO test (name) VALUES (’Imuvalue’)”);

// execute query
$result = sqlite_query($db, “SELECT * FROM test”);
// iterate through the retrieved rows
while ($row = sqlite_fetch_array($result))
print_r($row);
// close database connection
sqlite_close($db);
?&gt

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati
No comments

using php and postgres

By Justin Silverton

This is a simple guide on how to start connecting to a postgres database with php. I also assume that you already have a database setup with a table/tables.

Making a connection to the database

First, make sure the postgres module is enabled in php. This can done by looking through your php.ini file and ucommenting it in the modules section.

in windows, the line is: “extension=php_pgsql.dll”
in *nix, it is: extension=”php_pgsql.so”

next, a statement needs to setup for the actual connect.

$my_connection = pg_connect(”user=postgres dbname=phphh”);

pg_connect() takes a single argument, a connection string, which consists of name=value pairs.

Other valid connection string names include:

host - an optional host name for the database
port - an optional port number for the database
password - a password for the user you are connecting as
options - options to pass to the backendtty - a UNIX terminal path to send debugging information to

Unless you have customised your PostgreSQL installation, you should not need to use these.

The pg_connect() function returns a database connection handle which should be used with the PostgreSQL utility functions. On error, it returns FALSE.

Queries

$query_resource = pg_exec($my_connection,”INSERT INTO mytable VALUES(’value1′,’value2′,’value3′);”);

if(!$query_resource)
exit(”could not insert into database”);

$query_resource = pg_exec($my_connection,”SELECT * FROM mytable;”);

if(!$query_resource)
exit(”could not selectfrom database”);

Other Helpful functions

This is a list of other helpful functions that can be used to get data.

pg_fetch_row() - retrieve a single row
setpg_fetch_array() - retrieve a single row as an array
pg_fetch_object() - retrieve a single row as an object
pg_result() - retrieve a single field
pg_numrows() - number of rows returned by the query
pg_numfields() - number of fields per row returned by query
pg_fieldnum() - the numeric number of a given field
pg_fieldname() - the name of a given field

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati
No comments

5 postgres tips

By Justin Silverton

Here are 5 postgres tips to follow my “5 mysql tips” post. These are known issues with versions 7.3 and below.

1) Object names that aren’t in quotes are assumed to be lower-case

example: Select column1 FROM table1 and SELECT COLUMN1 FROM table1 both will return results from column1. SELECT “COLUMN1″ FROM table1 will be viewed as a diffrent column by postgres.

2) count(*) is very slow with large tables

This is because rather than an indexed scan, postgres scans the entire table to determine the count.

3) Min and Max are both slow with large tables

This is fixed in version 8.0, but there is a work-around that I have used in previous versions:

min equivalent: SELECT column1 FROM table1 ORDER BY column1 DESC LIMIT 1
max equivalent: SELECT column1 FROM table1 ORDER BY column1 ASC LIMIT 1

4) unicode values

unicode defaults to UTF-8

5) integers used in calculations that result in a value that is greater that 32-bits will end up as 0.

to fix the issue, either get postgres version 8.0 or cast one of your values as a 64 bit value (select integer::INT8)

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati
1 comment

« Previous PageNext Page »