Posts Tagged ‘functions’
Thursday, July 24th, 2008
PHP is a scripting language that is often imbedded into HTML to add functions HTML alone can’t do. PHP allows you to collect, process and utilize data to create a desired output. In short, it let’s you interact with your pages.
PHP is able to preform a number of tasks including printing data, making numeric calculations (such as addition or multiplication), making comparisons (which is bigger, are they equal, etc) and making simple boolean choices. From this you can create more complex loops and functions to make your page generate more specialized data.
Tags: bigger, calculations, functions, html, loops, multiplication, numeric, output, PHP, scripting language
Posted in PHP, tricks | No Comments »
Thursday, July 24th, 2008
If your problem is with a specific MySQL expression or function, you can perform a timing test by invoking the BENCHMARK() function using the mysql client program. Its syntax is BENCHMARK(loop_count,expression). The return value is always zero, but mysql prints a line displaying approximately how long the statement took to execute
Tags: approximately, benchmark, built, command, functions, MySQL, statement, tested
Posted in MySQL, tricks | No Comments »
Tuesday, July 15th, 2008
<usgov.google.com>: I use FirstGov.gov as a portal for all things dealing with the US government, and FedStats.gov. Google now offers a site with links to government news and search functions.
Tags: functions, google, government, links, news, offers, Search, U.S
Posted in SEO, google, tricks | No Comments »
Tuesday, July 15th, 2008
This one really surprised me. I expected these functions to be faster. Both these functions allow you to convert an array into a string. implode is the opposite of explode and join allows you to join the items in an array with a ‘glue’ string. In this case the array had 100 items.
implode vs join
implode: 47.2712550163 seconds
join: 50.1287050247 seconds
Time saved: 2.85745000839 seconds; 5.70022705949%
5% doesn’t seem like all that much but 2 microseconds shouldn’t be ignored.
Tags: explode, faster, functions, glue, implode, items, join, PHP, seconds, String
Posted in PHP, tricks | No Comments »
Monday, July 14th, 2008
So far all the functions have been running fast anyway so the difference haven’t been that significant but things are starting to get interesting with the run time jumping to 20-24 microseconds to write a short string to a file.
fwrite vs fputs
fwrite: 24.9826359749 seconds
fputs: 20.1990799904 seconds
Time saved: 4.7835559845 seconds; 23.6820488199%
With a difference of over 4 microseconds you could run both fputs and floatval in the time it takes to run fwrite. The difference is over 23% which clearly makes fputs the better function. It’s shorter as well.
Tags: Anyway, difference, File, functions, interesting, microseconds, running, shorter, significant
Posted in PHP, tricks | No Comments »
Monday, July 14th, 2008
Tidying up a string breaks the pattern. Maybe my analysis was a little premature.
chop vs rtrim
chop: 4.73731994629 seconds
rtrim: 4.41647195816 seconds
Time saved: 0.320847988129 seconds; 7.26480301852%
rtrim is a little faster and these functions take longer at a little over 4 microseconds.
Tags: chop, faster, functions, little, microseconds, PHP, rtrim
Posted in PHP, tricks | No Comments »
Monday, July 14th, 2008
Next it is time to try some validation with is_int and is_integer.
is_int vs is_integer
is_int: 3.11394786835 seconds
is_integer: 3.42630600929 seconds
Time saved: 0.312358140945 seconds; 9.11646946006%
The difference is smaller this time but again both functions are executing in a little over 3 microseconds. is_int is faster than is_integer so again the shorter function name wins. Is this going to be a pattern?
Tags: again, difference, functions, integer, microseconds, pattern, PHP
Posted in PHP, tricks | No Comments »
Monday, July 14th, 2008
First up are the sizeof and count functions. They can both be used to count the number of items in an array but does one do it better?
sizeof vs count
sizeof: 3.75928902626 seconds
count: 3.33035206795 seconds
Time saved: 0.428936958313 seconds; 12.8796280262%
The evidence says yes. The count function was over 12% faster in this test. Both functions are fast though taking 3-4 microseconds to count an array with 100,000 items. You might think it isn’t worth it but remember count is also a character shorter. Not only is it faster to run but it is also faster to type!
Tags: array, character shorter, count, evidence, fast, faster, functions, number, PHP, remember, sizeof
Posted in PHP, tricks | No Comments »
Tuesday, July 1st, 2008
A variable variable looks like this: $$var
So, if $var = ‘foo’ and $foo = ‘bar’ then $$var would contain the value ‘bar’ because $$var can be
thought of as $’foo’ which is simply $foo which has the value ‘bar’.
Variable variables sound like a cryptic a useless concept, but they can be useful sometimes. For
example, if we have a configuration file consisting of configuration directives and values in this
format:
foo=bar
abc=123
Then it is very easy to read this file and create corresponding variables:
<?php
$fp = fopen(’config.txt’,'r’);
while(true) {
$line = fgets($fp,80);
if(!feof($fp)) {
if($line[0]==’#’ || strlen($line)<2) continue;
list($name,$val)=explode(’=',$line,2);
$$name=trim($val);
} else break;
}
fclose($fp);
?>
Along the same lines as variable variables, you can create compound variables and variable
functions.
<?php
$str = ‘var’;
$var_toaster = “Hello World”;
echo ${$str.’_toaster’};
$str(); // Calls a function named var()
${$str.’_abc’}(); // Calls a function named var_abc()
?>
Tags: configuration, consisting, corresponding, cryptic, easy, example, format, functions, PHP, read, trim, useful, values, variable, variables
Posted in PHP | 1 Comment »
Monday, June 30th, 2008
Problem
You need PHP’s built-in ftp functions for the ultra-cool script you are writing, but your service
provider does not have PHP compiled with the –enable-ftp option.
Solution
If you have a shell account on a system with the same operating system as your web server, grab the
PHP source tarball and build using:
–with-apxs –enable-ftp=shared
You can check which flags your provider used by putting a phpinfo() call in a script on your server.
<?phpinfo()?>
Once compiled, you will find a “modules/ftp.so” file which you can copy to your web server and
enable either by putting:
extension=ftp.so
in your php.ini file or by adding this to the top of your script:
<?php dl(”ftp.so”) ?>
Tags: Adding, compiled, cool, extension, flags, FTP, functions, PHP, phpinfo, problem, provider, Script, server, service, solution
Posted in PHP | No Comments »